2016-11-21 6 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用語のクエリを調べましたが、これらの値が暗黙の「または」演算であると私は理解しています。私は "and"操作が必要です。 –

答えて

2

私たちはそれを理解しました。答えは、同じネストされたクエリに2つの句を使用する代わりに、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

関連する問題