2017-08-16 552 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

相關問題