gpt4 book ai didi

elasticsearch - Elasticsearch -所有父级和匹配嵌套

转载 作者:行者123 更新时间:2023-12-03 02:35:23 24 4
gpt4 key购买 nike

我们的用例有一个产品列表,并且在嵌套字段中按客户编号显示了最后购买日期。当客户搜索产品时,我们希望返回与其搜索词匹配的所有商品。如果有记录,则inner_hits仅应包括该客户的记录。因此,如果客户搜索 Jade 米,他们可能会得到4个结果,但以前可能只购买了其中2个。因此,有2个文档没有内部匹配,而其他2个文档仅具有客户最后一次购买的1个文档的内部匹配。

大多数商品将在嵌套字段中加载数百笔客户购买的商品,因此当我们只考虑搜索客户的最后购买日期时,我们不想返回所有这些记录。

我们正在测试的当前查询

{
"_source": {
"includes": ["*"],
"excludes": ["c_purchases"]
},
"query": {
"bool": {
"must": [
{
"match": {
"i_description": "corn"
}
},
{
"nested" : {
"path" : "c_purchases",
"inner_hits": {
"_source": ["*"]
},
"query" : {
"match": {
"c_purchases.customernumber": "1111"
}
}
}
}
]
}
}
}

上面查询的问题是它只返回客户购买的物品。因此,在示例中,它将仅返回c_purchases中带有嵌套记录的2个与所有4个 Jade 米项目。

当前结果
{
"hits": {
"total": {
"value": 2
},
"hits": [
{
"_source": {
"i_description": "corn 1",
"i_code": "111111"
},
"inner_hits": {
"c_purchases": {
"hits": {
"total": {
"value": 1,
},
"hits": [
{
"_source": {
"customernumber": "100",
"lastordered": "2018-01-30T00:00:00",
}
}
]
}
}
}
},
{
"_source": {
"i_description": "corn 2",
"i_code": "222222"
},
"inner_hits": {
"c_purchases": {
"hits": {
"total": {
"value": 1,
},
"hits": [
{
"_source": {
"customernumber": "100",
"lastordered": "2018-01-30T00:00:00",
}
}
]
}
}
}
}
]
}
}

预期成绩
{
"hits": {
"total": {
"value": 2
},
"hits": [
{
"_source": {
"i_description": "corn 1",
"i_code": "111111"
},
"inner_hits": {
"c_purchases": {
"hits": {
"total": {
"value": 1,
},
"hits": [
{
"_source": {
"customernumber": "100",
"lastordered": "2018-01-30T00:00:00",
}
}
]
}
}
}
},
{
"_source": {
"i_description": "corn 2",
"i_code": "222222"
},
"inner_hits": {
"c_purchases": {
"hits": {
"total": {
"value": 1,
},
"hits": [
{
"_source": {
"customernumber": "100",
"lastordered": "2018-01-30T00:00:00",
}
}
]
}
}
}
},
{
"_source": {
"i_description": "corn 3",
"i_code": "333333"
}
},
{
"_source": {
"i_description": "corn 4",
"i_code": "444444"
}
}
]
}
}

提前致谢!

最佳答案

通过搜索可以解决此问题:

{
"_source": {
"includes": ["*"],
"excludes": ["c_purchases"]
},
"query": {
"bool": {
"must": {
"match": {
"i_description": "corn"
}
},
"should": {
"nested" : {
"path" : "c_purchases",
"inner_hits": {
"_source": ["*"]
},
"query" : {
"match": {
"c_purchases.customernumber": "1111"
}
}
}
}
}
}
}

关于elasticsearch - Elasticsearch -所有父级和匹配嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com