【发布时间】:2019-12-02 23:47:15
【问题描述】:
我们的用例是有一个产品列表,并在嵌套字段中按客户编号显示最后购买日期。当客户搜索产品时,我们希望返回与其搜索词匹配的所有商品。如果有记录的话,inner_hits 应该只包含该客户的记录。因此,如果客户搜索玉米,他们可能会获得 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 个玉米项目。
当前结果
{
"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"
}
}
]
}
}
提前致谢!
【问题讨论】:
标签: elasticsearch