2016-11-21 24 views
0

多個嵌套的文件我有一個包含嵌套文檔的陣列的文件。如果文檔包含所有指定的嵌套文檔,我有要求返回匹配。匹配的文件,如果它包含在elasticsearch

這裏是映射的相關部分:

"element": { 
    "dynamic": "false", 
    "properties": { 
    "tenantId": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "fqn": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "id": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "name": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "type": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "location": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "tags": { 
     "type": "nested", 
     "properties": { 
     "id": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "dataSourceId": { 
      "type": "long", 
      "index": "not_analyzed" 
     }, 
     "name": { 
      "type": "string", 
      "index": "not_analyzed" 
     }, 
     "value": { 
      "type": "string", 
      "index": "not_analyzed" 
     } 
     } 
    } 
    } 
} 

我們的目標是能夠返回包含所有的標籤列表(雖然元素被允許含有超出了搜索其他標記元素需求)。

這是我到目前爲止有:

{ 
    "query": { 
    "bool": { 
     "filter": { 
      "nested": { 
      "path": "tags", 
      "query": { 
       "bool": { 
        "must": [ 
         { 
         "bool": { 
          "must":{ 
          "term": { "tags.name": "name1" }, 
          "term": { "tags.value": "value1" } 
          } 
         } 
         }, 
         { 
         "bool": { 
          "must":{ 
          "term": { "tags.name": "name2" }, 
          "term": { "tags.value": "value2" } 
          } 
         } 
         } 
        ] 
       } 
      } 
      } 
     } 
    } 
    } 
} 

這種方法的問題是,它返回0命中多個標籤值(它工作正常單個值)。我相信這是因爲查詢要求標籤有多個名稱和值才能匹配,這顯然不會發生。有誰知道如何查詢包含所有標籤列表的元素?

編輯:這是使用elasticsearch 5.0

+0

你想試試嗎? https://www.elastic.co/guide/en/elasticsearch/guide/master/_finding_multiple_exact_values.html – JamesKn

+0

@JamesKn我研究了術語查詢,但我的理解是,它們是對值的隱式「或」操作。我需要一個「和」操作。 –

回答

2

我們計算出來了。答案是創建兩個嵌套查詢,而不是將兩個子句用於相同的嵌套查詢。

{ 
"query":{ 
    "bool":{ 
    "must":[{ 
     "nested":{ 
      "path":"tags", 
      "query":{ 
       "bool":{ 
       "must":[ 
        {"term":{"tags.name":"name1"}}, 
        {"term":{"tags.value":"value1"}} 
       ] 
       } 
      } 
     } 
    }, 
    { 
     "nested":{ 
      "path":"tags", 
      "query":{ 
       "bool":{ 
       "must":[ 
        {"term":{"tags.name":"name2"}}, 
        {"term":{"tags.value":"value2"}} 
       ] 
       } 
      } 
     } 
    }] 
    } 
} 
} 
+0

出現這個問題,你的回答讓我很頭疼,謝謝! – BenM

相關問題