2015-05-21 41 views
0

如何查詢包含鍵值對「key1」 - 「value1」和「key2」 - 「value2」的文檔?我似乎無法找到任何文件。Elasticsearch:查詢包含兩個鍵值對的文檔

我試過下面的查詢,但即使應該有匹配的文檔它也沒有返回結果。替換必須與應該儘管工作,但然後當我把minimum_should_match = 100%,它也不會返回任何結果。

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "attributes", 
      "query": { 
       "bool": { 
       "must": [ 
        [ 
        { 
         "match_phrase": { 
         "attributes.key": "key1" 
         } 
        }, 
        { 
         "match_phrase": { 
         "attributes.value": "value1" 
         } 
        } 
        ], 
        [ 
        { 
         "match_phrase": { 
         "attributes.key": "key2" 
         } 
        }, 
        { 
         "match_phrase": { 
         "attributes.value": "value2" 
         } 
        } 
        ] 
       ] 
       } 
      } 
      } 
     }, 
     [ 
      { 
      "match_all": { 

      } 
      } 
     ] 
     ] 
    } 
    } 
} 

這是映射的樣子:

{ 
    "index_name": { 
     "mappings": { 
      "type_name": { 
       "properties": { 
        "attributes": { 
         "type": "nested", 
         "properties": { 
          "key": { 
           "type": "string", 
           "analyzer": "flat" 
          }, 
          "value": { 
           "type": "string", 
           "analyzer": "flat" 
          } 
         } 
        } 
       } 
      }, 
     } 
    } 
} 

非常感謝你的幫助。

回答

1

試試這個:

POST /test_index/_search 
{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "bool": { 
       "must": [ 
        { 
        "nested": { 
         "path": "attributes", 
         "query": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
             "attributes.key": "key1" 
            } 
           }, 
           { 
            "term": { 
             "attributes.value": "value1" 
            } 
           } 
           ] 
          } 
         } 
        } 
        }, 
        { 
        "nested": { 
         "path": "attributes", 
         "query": { 
          "bool": { 
           "must": [ 
           { 
            "term": { 
             "attributes.key": "key2" 
            } 
           }, 
           { 
            "term": { 
             "attributes.value": "value2" 
            } 
           } 
           ] 
          } 
         } 
        } 
        } 
       ] 
      } 
     } 
     } 
    } 
} 

這是我用來測試它(我只是用standard分析儀,因爲我不知道你flat分析儀看起來像代碼,所以你可能需要調整那):

http://sense.qbox.io/gist/13b7e2aa4d90bfb2f82787c6a00494ee3343e013

+0

是啊,這個工程。謝謝! – dyrim

0

我碰到這個跑遇到同樣的問題與彈性5,要稍微調整推薦的應答得到儘可能過濾查詢已被棄用,它的工作。 (沒有收到[查詢]註冊[過濾])。

隨着彈性5以下結構解決了這個問題對我來說:

{ 
    "query" : { 
     "bool" : { 
      "must" : [ 
       { 
        "nested" : { 
         "path": "attributes", 
         "query" : { 
          "bool" : { 
           "must" : [ 
            { "term" : { "attributes.key" : "key1" } }, 
            { "term" : { "attributes.value" : "value1" } } 
           ] 
          } 
         } 
        } 
       }, 
       { 
        "nested" : { 
         "path" : "attributes", 
         "query" : { 
          "bool" : { 
           "must" : [ 
            { "term" : { "attributes.key" : "key2" } }, 
            { "term" : { "attributes.value" : "value2" } } 
           ] 
          } 
         } 
        } 
       } 
      ] 
     } 
    } 
} 
相關問題