我试图获取嵌套文档中有两个名称的文档,但是must
子句正在" OR"而不是" AND"。
这是一个例子:
映射:
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
}
]
}
'
我在同一文档中查找comments.name
"Alice White"
和 "John Smith"
的文档,仅使用上述数据{{1会匹配。我尝试了这个查询:
id 1
它与所有文件相匹配,因为所有文件都有" John Smith"或" Alice White"。
改进此查询以使两个分开的匹配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"] } }
]
}
}
}
}
}
'
,每个值匹配一个匹配器:
query.nested.query.bool.must[].terms
所以,我的问题是,如何构建查询以仅匹配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"
的文档?
PS。删除了example here
的脚本答案 0 :(得分: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"
]
}
}
]
}
}
}
}
]
}
}
}