【问题标题】:Elasticsearch Fails to query exactly for an array Field inside the DocumentElasticsearch 无法准确查询文档中的数组字段
【发布时间】:2016-09-03 14:03:31
【问题描述】:

我在名为“bank”的 elasticsearch 类型中有以下数据

[
    {
        "_id": "1",
        "name": "ICIC",
        "balance": "$2,574.27",
        "friends": [
            {
                "roleid": 0,
                "name": "Alana Shepard",
                "isactive": true
            },
            {
                "roleid": 1,
                "name": "Katheryn Hatfield",
                "isactive": false
            }
        ]
    },
    {
        "_id": "2",
        "name": "SBK",
        "balance": "$2,346.44",
        "friends": [
            {
                "roleid": 0,
                "name": "Hinton Kaufman",
                "isactive": true
            },
            {
                "roleid": 1,
                "name": "Miles Alford",
                "isactive": true
            }
        ]
    }
]

现在我尝试获取 friends.roleid = 1 和 friends.isactive=true 的文档。请求的DSL查询如下,

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "friends.roleid": {
                            "value": 1
                        }
                    }
                },
                {
                    "term": {
                        "friends.isactive": {
                            "value": true
                        }
                    }
                }
            ]
        }
    }
}

预期的结果是具有 _id = 2 的对象。但实际结果是 _id =1 和 _id = 2 。如果有人帮助找出 DSL 查询中的问题,我们将不胜感激。谢谢

【问题讨论】:

  • 您需要将您的friends 字段声明为nested 类型。在这里查看原因:elastic.co/guide/en/elasticsearch/guide/2.x/nested-objects.html
  • Val 如何在同一个请求中编写嵌套查询和普通 dsl 查询?
  • 您可以使用bool/must 将它们组合在一起,就像在嵌套查询中组合term 查询一样。
  • @Val 除了嵌套查询之外,我是否还需要编写另一个查询对象。因为当我在同一个嵌套查询中使用 bool/must 为“名称”指定术语查询时,搜索无法获取任何数据
  • 也许,用您的实际查询更新您的答案,我会在那里发表评论

标签: elasticsearch nested nest


【解决方案1】:

解决了将字段“朋友”作为嵌套对象的问题。而DSL查询重构如下,

{
    "query": {
        "nested": {
            "path": "friends",
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "friends.roleid": {
                                    "value": 1
                                }
                            }
                        },
                        {
                            "term": {
                                "friends.isactive": {
                                    "value": "true"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}

DSL 查询中的路径指定嵌套对象。 elasticsearch 文档参考https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html 会有很大帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2021-05-22
    相关资源
    最近更新 更多