gpt4 book ai didi

elasticsearch - 从第一个数组元素访问值-过滤

转载 作者:行者123 更新时间:2023-12-02 22:48:19 26 4
gpt4 key购买 nike

在度过了一个周末寻找答案之后,我决定向您寻求帮助。我有一个产品索引。变量是产品中对象的嵌套列表,我在进料索引时按库存数量和其他一些条件对其进行排序。
我正在尝试按 flex 响应中的第一个变量的值对产品进行排序(包括在查询中传递的过滤器)。这是我正在寻找的示例。
缩短的 flex 响应结构:

{
"id": 6346,
"name": "name",
"variants": [
{
"variant_id": 1,
"stock_quantity": 20,
"price": 9.9
},
{
"variant_id": 2,
"stock_quantity": 15,
"price": 20.9
},
{
"variant_id": 3,
"stock_quantity": 0,
"price": 16.9
}
]
}

让我们按价格gte 15筛选列表:
{
"id": 6346,
"name": "name",
"variants": [
{
"variant_id": 2,
"stock_quantity": 15,
"price": 20.9
},
{
"variant_id": 3,
"stock_quantity": 0,
"price": 16.9
}
]
}

现在,我尝试根据响应的第一个变体价格对产品进行排序。到目前为止,我尝试做什么?

按脚本排序
"sort": 
{
"_script": {
"order": "desc",
"type": "number",
"script": {
"source": "return params['_source']['variants'][0].gross_price"
}
}
}

在我开始过滤列表之前,这种方法一直有效。在variants.gross_price gte 15的情况下,脚本从源文档返回值:9.9,而不是我期望的20.9。我忽略了出于性能原因不建议使用ref源的事实。

依栏位排序
"sort": 
{
"variants.gross_price": {
"nested": {
"path": "variants",
"filter": {
"bool": {
"filter": [
{
"range": {
"variants.gross_price": {
"gt": "15"
}
[...]

我不知道为什么,但是通过这种方式,我无法从响应中得到第一个变体的价格,而是产品中最便宜的变体的价格-在这种情况下为16.9。

我还向变量中添加了标记“include_in_parent”,并尝试通过脚本中的“doc ['variants.gross_price']。values”获取值,但效果与第一个相同。

所以..我还能尝试做什么?

更新
我正在寻找6.5版本中的Max_children。

最佳答案

您可以尝试以下脚本。

"sort": {
"_script": {
"type": "number",
"script": {
"inline": "if(doc['variants.gross_price'].value > 15) {max = doc['variants.gross_price'].value;}, abs(max)",
"order" : asc
}
}
}

关于elasticsearch - 从第一个数组元素访问值-过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53814107/

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