如何返回列表中匹配多个字段的文档?
例:
每个文档都包含有关大学的信息。
字段之一是以下格式的学生列表:
"students": [
{"name":"John", "age":23},
{"name":"Joe", "age":65},
{"name":"John","age":12}
]
因此,文档1具有自己的学生列表,文档2具有自己的学生列表,依此类推。
我如何查询以退回所有有一个叫“John”的学生,年龄至少21岁的大学(文件)?
最佳答案
您应该在映射中将学生定义为嵌套字段,以便能够执行此类查询。
{
"mappings": {
"university": {
"properties": {
"students": {
"type": "nested"
}
}
}
}
}
然后您的查询非常简单
{
"query": {
"nested": {
"path": "students",
"query": {
"bool": {
"must": [
{ "match": { "students.name": "John" }},
{ "range": {"students.age": {"gte": 21}}}
]
}
}
}
}
}
如果有名叫John的学生年龄至少21岁,它将返回每个文档。
关于elasticsearch - 匹配列表中的多个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966309/