【问题标题】:Nested Query with Date Range具有日期范围的嵌套查询
【发布时间】:2021-05-23 02:50:21
【问题描述】:

我想知道是否有人可以确认我构造的查询是否正确。

我有一个 User 映射,其中 Transaction 嵌套在用户中,我正在寻找在某个日期之前购买了特定商品的用户。此外,在Transaction 中,我还有line_items,这也是一个嵌套字段。

最初,我构造了以下查询,根据返回的数据,我断定该查询可能不正确。

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "nested": {
                                "path": "transaction.line_items",
                                "query": {
                                    "bool": {
                                        "must": {
                                            "match": {
                                                "transaction.line_items.barcode": {
                                                    "query": "abc123xyz"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "range": {
                                "transaction.timestamp": {
                                    "from": null,
                                    "include_lower": true,
                                    "include_upper": true,
                                    "to": "2021-05-14T00:00:00+02:00"
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
}

然后我更新了我的查询,现在根据返回的结果我认为查询是正确的。但是,为了避免确认偏差,我想知道是否有人可以解释为什么第二个查询是正确的(假设是正确的)。

{
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "path": "transaction",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "transaction.timestamp": {
                                                "from": null,
                                                "include_lower": true,
                                                "include_upper": true,
                                                "to": "2021-05-16T00:00:00+02:00"
                                            }
                                        }
                                    },
                                    {
                                        "nested": {
                                            "path": "transaction.line_items",
                                            "query": {
                                                "bool": {
                                                    "must": {
                                                        "match": {
                                                            "transaction.line_items.barcode": {
                                                                "query": "abc123xyz"
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

【问题讨论】:

    标签: elasticsearch nested range-query


    【解决方案1】:

    ES 中的每个嵌套文档都存储为单独的文档。

    假设你有一个文件—— A - [b,d] 作为嵌套字段

    如果根据您的第一次尝试 -> 日期查询仅匹配 d,并且条形码查询仅匹配 b。然后将返回 A。

    但是对于您的第二次尝试,两个查询必须匹配同一个嵌套文档,并且基于此,只会返回文档。在我们的例子中。第一次尝试不会返回 A。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 2017-06-23
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多