【发布时间】:2020-02-15 16:22:30
【问题描述】:
如何对同一嵌套下的多个字段进行“必须”“匹配”查询?这是一个可重现的 ES 索引,其中“用户”字段被定义为“嵌套”类型。
PUT my_index
{
"mappings": {
"properties": {
"user": {
"type": "nested",
"properties": {
"firstname": {"type": "text"}
}
}
}
}
}
这里有 2 个文件:
PUT my_index/_doc/1
{
"user" : [
{
"firstname" : "John"
},
{
"firstname" : "Alice"
}
]
}
PUT my_index/_doc/2
{
"user" : [
{
"firstname" : "Alice"
}
]
}
对于此索引,如何查询同时存在“John”和“Alice”的文档?使用上面定义的索引,我希望得到 Document 1 但不是 Document 2。到目前为止,我已经尝试了以下代码,但它没有返回任何命中:
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{"match": {"user.firstname": "John"}},
{"match": {"user.firstname": "Alice"}}
]
}
}
}
}
}
【问题讨论】:
-
你用的是什么版本的elasticsearch?
-
我使用的是 Elasticsearch 版本 7.4.0
标签: elasticsearch