elasticsearch - 在弹性搜索中,如何匹配对象数组中的多个对象?
问题描述
我在这里按照示例进行操作:
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
以下查询:
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "White" }}
]
}
}
}
}
}
符合预期的一条记录。假设我只想返回同时具有“John Smith”和“Alice White”作为用户的文档。
我努力了:
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.first": "Alice" }},
{ "match": { "user.last": "White" }},
{ "match": { "user.first": "John" }},
{ "match": { "user.last": "Smith" }}
]
}
}
}
}
}
但这会返回零结果。如何获取同时包含“Alice White”和“John Smith”的文档(应该与原始结果中返回的文档相同)?
解决方案
您应该使用bool 查询来组合多个子句。使用您的语法,您正在搜索在同一字段中同时包含 Alice 和 John 作为值的文档。尝试,确实:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "White"
}
}
]
}
}
}
},
{
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "John"
}
},
{
"match": {
"user.last": "Smith"
}
}
]
}
}
}
}
]
}
}
}
推荐阅读
- node.js - 如何从 Buffer.from 获得与 fs.readFile 相同的输出?
- android - 我想请求一个可变地址
- flutter - 我想在我的颤振应用程序中添加一个应用程序抽屉,但在脚手架之外
- python - Tensorflow 1.14:tf.numpy_function 在映射时会变形吗?
- django - 如何在 Django 模型上从第二个表执行 INNER JOIN 和 WHERE 并返回 JSON?
- javascript - Nodejs:使用sigint退出之前的json api调用?
- sql - 如何使用 WHERE 子句对分区进行 RANK()
- javascript - ThreeJS - 2D 像素到 3D 坐标的转换
- vue.js - Sails.js 在执行云操作时更改内容 onClick
- c# - 为什么只显示一行?即为什么它只读取文本文件的第一行?