2017-08-16 141 views
0

试图在使用bool和必须查询时获取python-elasticsearch或curl以返回适当的结果。Elasticsearch必须使用多个条件进行查询

我知道文档在我的查询中,我可以用'should'找到它;但是,我需要满足这两个标准,所以我尝试使用'must'执行相同的查询。当我使用“必须”时,相关文档不会被返回。

随着是否应

$ 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" : [ ] } } 
+1

很确定这是因为'project'字段被分析过(即它具有'text'类型)。你可以切换到使用'match'查询,或者使用'term',但是用小写而不是'AMQ'搜索'amq'。 – Val

+0

添加到Val的注释上面,或者你可以改变你的从文本类型到关键字的映射键入然后它将匹配条款作为其确切的价值 – Rahul

回答

0

变化

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

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

或者我们可以用匹配替换词并使用'AMQ'。

{"match": {"project": "AMQ"}} 
+0

虽然这将解决这个特定问题,[this](https://stackoverflow.com/questions/45077406/elasticsearch-multiple-exact-search-on-field-returns-没有结果/ 45103907#45103907)可能是一个有趣的阅读。 – Slomo

相关问题