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つのネストされたブロックを追加してスクリプトを落としたが、おかげで非常に冗長ですが、問題を解決します。 –
冗長かもしれませんが、それはそれを行う方法です。 –