elasticsearch - 在 Elasticsearch 中使用 'OR' 或 'AND' 组合多个查询

标签 elasticsearch

我有两个不同的查询,我想将它们与中间的“OR”/“AND”组合起来。我该怎么做?

例如,对于给定的查询 我只想在 elasticsearch 中运行 Query1 或 Query2。

查询 1:

{
   "query": {
      "filtered": {
         "query": { 
            "query_string":{  
               "query":"Batman",
               "default_operator":"AND",
               "fields"::[  
                  "Movies._all"
               ]
            }
         },
         "filter": {
            "bool": {
               "must": [  
                  {  
                     "query":{  
                        "filtered":{  
                           "filter":{  
                              "and":[  
                                 {  
                                    "term":{  
                                       "cast.firstName":"Christian "
                                    }
                                 },
                                 {  
                                    "term":{  
                                       "cast.lastName":"Bale"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

查询2:

{
   "query": {
      "filtered": {
         "query": { 
            "query_string":{  
               "query":"Dark Knight",
               "default_operator":"AND",
               "fields"::[  
                  "Movies._all"
               ]
            }
         },
         "filter": {
            "bool": {
               "must": [  
                  {  
                     "query":{  
                        "filtered":{  
                           "filter":{  
                              "and":[  
                                 {  
                                    "term":{  
                                       "director.firstName":"Christopher"
                                    }
                                 },
                                 {  
                                    "term":{  
                                       "director.lastName":"Nolan"
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

最佳答案

您需要使用 bool query 像下面这样的东西可以正常工作 -

{
  "query" : {
    "bool" : { 
      "must" : [
        { // Query1 },
        { // Query2}
      ]
    }
  }
}

对 AND 使用 must,对 OR 使用 should

关于elasticsearch - 在 Elasticsearch 中使用 'OR' 或 'AND' 组合多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596980/

相关文章:

elasticsearch - 有没有办法通过 Elasticsearch 查询故意返回空值?

elasticsearch - 如何确定我的 Elasticsearch 服务器的内存需求

Tornado 中具有多个进程的异步连接池

linux - 在Linux上运行elasticsearch查询

nginx - Kibana 4,Logstash 仪表板 : how do I require Nginx authentication when saving but allow anonymous views?

javascript - 如何在没有 Kibana 的情况下从 AWS 中的 ElasticSearch 访问数据?

具有多个过滤器的 ElasticSearch

elasticsearch - 我如何使用 ElasticSearch 和 Kibana 比较 2 个时间段?

elasticsearch - 查询构建器在Elastic Search JAVA高级重置客户端中找不到匹配项

elasticsearch - 如何根据相关性对 Elastic Search 查询结果进行排序?