【问题标题】:Elasticsearch 'must' query with multiple criteriaElasticsearch“必须”查询多个条件
【发布时间】:2017-08-16 23:30:55
【问题描述】:

在使用 bool 和 must 查询时,尝试让 python-elasticsearch 或 curl 返回适当的结果。

我知道文档在我的查询中,我可以用“应该”找到它;但是,我需要同时满足这两个条件,所以我尝试使用“必须”进行相同的查询。当我使用“必须”时,相关文档不会返回。

应有

$ curl -XGET 'localhost:9200/polarion/subtype1/_search?pretty' -H 'Content-Type: application/json' -d'
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"project" : "AMQ"}},  
                 { "term" : {"test-type" : "functional"}}]
           }
         }
      }
   }
}
'

"hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "polarion",
        "_type" : "subtype1",
        "_id" : "AV3s-vbF8T4AI9Zj3GkM",
        "_score" : 1.0,
        "_source" : {
          "project" : "AMQ",
          "query" : "test-metrics",
          "test-type" : "functional",
          "2017-08-16" : 1916
        }
      },

必须

$ curl -XGET 'localhost:9200/polarion/subtype1/_search?pretty' -H 'Content-Type: application/json' -d'
{
   "query" : {
      "constant_score" : {
         "filter" : {
            "bool" : {
              "must" : [
                 { "term" : {"project" : "AMQ"}},  
                 { "term" : {"test-type" : "functional"}}]
           }
         }
      }
   }
}
'


{   "took" : 0,   "timed_out" : false,   "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0   },   "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]   } }

【问题讨论】:

  • 很确定这是因为分析了project 字段(即它具有text 类型)。您可以改用match 查询,或者坚持使用term,但搜索小写的amq 而不是AMQ
  • 添加到上面 Val 的评论中,或者您可以将您的映射从文本类型更改为关键字类型,然后它将匹配术语作为其确切值

标签: python elasticsearch


【解决方案1】:

改变

{"term": {"project": "AMQ"}}

{"term": {"project": "amq"}}

或者我们可以用匹配替换术语并使用“AMQ”。

{"match": {"project": "AMQ"}}

【讨论】:

  • 虽然这将解决这个特殊问题,但this 可能是一个有趣的阅读。
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2020-02-15
  • 1970-01-01
相关资源
最近更新 更多