2017-04-20 11 views
0

ネストされたドキュメントで2つの名前を持つドキュメントを取得しようとしていますが、must句は「AND」ではなく「OR」として機能しています。ここで は一例です:同じネストされたフィールドの異なる値を一致させるためのElasticsearchクエリ

マッピング:

curl -XPUT "http://localhost:9200/my_index" -d ' 
{ 
    "mappings": { 
    "blogpost": { 
     "properties": { 
     "comments": { 
      "type": "nested", 
      "properties": { 
      "name": { "type": "keyword" }, 
      "age":  { "type": "short" } 
      } 
     } 
     } 
    } 
    } 
}' 

インデックス3つの文書:

curl "http://localhost:9200/my_index/blogpost/1" -d ' 
{ 
    "title": "doc1", 
    "comments": [ 
    { 
     "name": "John Smith", 
     "age":  28 
    }, 
    { 
     "name": "Alice White", 
     "age":  31 
    } 
    ] 
} 
' 

curl "http://localhost:9200/my_index/blogpost/2" -d ' 
{ 
    "title": "doc2", 
    "comments": [ 
    { 
     "name": "Luther Lawrence", 
     "age":  21 
    }, 
    { 
     "name": "Alice White", 
     "age":  19 
    } 
    ] 
} 
' 

curl "http://localhost:9200/my_index/blogpost/3" -d ' 
{ 
    "title": "doc3", 
    "comments": [ 
    { 
     "name": "Tadhg Darragh", 
     "age":  22 
    }, 
    { 
     "name": "Alice White", 
     "age":  31 
    }, 
    { 
     "name": "Lorene Hicks", 
     "age":  44 
    } 
    ] 
} 
' 

私は同じ文書に"Alice White""John Smith"comments.nameを持っている文書を探しています、上記のデータを使用してid 1という文書のみが一致します。私はこのクエリーで試しました:

curl "http://localhost:9200/my_index/blogpost/_search" -d ' 
{ 
    "_source": { "include": "title" }, 
    "query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
     "bool": { 
      "must": [ 
      { "terms": { "comments.name": ["John Smith", "Alice White"] } } 
      ] 
     } 
     } 
    } 
    } 
} 
' 

すべての文書に「John Smith」または「Alice White」があるため、すべての文書と一致します。 が離れた2試合query.nested.query.bool.must[].termsを持つようにこのクエリの改善、値ごとに1人のマッチャー:

curl "http://localhost:9200/my_index/blogpost/_search" -d ' 
{ 
    "_source": { "include": "title" }, 
    "query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
     "bool": { 
      "must": [ 
      { "term": { "comments.name": "John Smith" } }, 
      { "term": { "comments.name": "Alice White" } } 
      ] 
     } 
     } 
    } 
    } 
} 
' 

だから、私の質問は"Alice White""John Smith"と文書のみを一致させるために、クエリを構築する方法、ありますか?

ps。 example here

答えて

1
{ 
    "_source": { 
    "include": "title" 
    }, 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "nested": { 
      "path": "comments", 
      "query": { 
       "bool": { 
       "must": [ 
        { 
        "terms": { 
         "comments.name": [ 
         "John Smith" 
         ] 
        } 
        } 
       ] 
       } 
      } 
      } 
     }, 
     { 
      "nested": { 
      "path": "comments", 
      "query": { 
       "bool": { 
       "must": [ 
        { 
        "terms": { 
         "comments.name": [ 
         "Alice White" 
         ] 
        } 
        } 
       ] 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

は、すべての名前のために1つのネストされたブロックを追加してスクリプトを落としたが、おかげで非常に冗長ですが、問題を解決します。 –

+1

冗長かもしれませんが、それはそれを行う方法です。 –

関連する問題