我正在尝试使用一个查询来获取两个查询的结果。
我必须为每个多字串查询请求运行两个查询,然后以编程方式将结果组合到我觉得不合适的代码中。
其中一个查询使用AND
运算符,其中一个查询使用OR
运算符。
这是第一个查询:
GET my_index/_search
{
"query": {
"bool": {
"must_not": [
{"exists": {"field": "deleted"}}
],
"must": [
{
"multi_match": {
"query": "refuse eating",
"fields": [
"title^3",
"desc^2",
"body"
],
"type": "best_fields",
"operator": "AND"
}
}
],
"filter": [
{"term": {"kind": "article"}},
{"term": {"status": "published"}}
]
}
}
}
这是第二个查询:GET my_index/_search
{
"query": {
"bool": {
"must_not": [
{"exists": {"field": "deleted"}}
],
"must": [
{
"multi_match": {
"query": "refuse eating",
"fields": [
"title^3",
"desc^2",
"body"
],
"type": "best_fields",
"operator": "OR"
}
}
],
"filter": [
{"term": {"kind": "article"}},
{"term": {"status": "published"}}
]
}
}
}
这个想法是在顶部处获得第一个查询的结果(使用AND
运算符的查询),然后在第一个结果下方获得第二个查询的结果(使用OR
运算符的查询)。有什么方法可以通过而不是两个单个查询来实现这一目标?
最佳答案
对于AND匹配,您可以利用bool/should
因子较高的boost
:
GET my_index/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "deleted"
}
}
],
"minimum_should_match": 1,
"should": [
{
"multi_match": {
"query": "refuse eating",
"fields": [
"title^3",
"desc^2",
"body"
],
"type": "best_fields",
"operator": "AND",
"boost": 2, <--- boost AND by 2 (or more)
"_name": "and-match"
}
},
{
"multi_match": {
"query": "refuse eating",
"fields": [
"title^3",
"desc^2",
"body"
],
"type": "best_fields",
"operator": "OR",
"_name": "or-match"
}
}
],
"filter": [
{
"term": {
"kind": "article"
}
},
{
"term": {
"status": "published"
}
}
]
}
}
}
关于elasticsearch - 如何结合 “AND”和 “OR”查询来获得两者的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101580/