【发布时间】:2017-09-17 08:47:58
【问题描述】:
我正在尝试在嵌套文档中获取具有两个名称的文档,但是 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" 的文档,使用上述数据只有文档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”。
改进此查询以具有两个单独的匹配 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" 和 "John Smith" 的文档?
ps。删除了带有 example here 的脚本
【问题讨论】:
标签: elasticsearch bigdata