尝试在 ElasticSearch 中查询和聚合,但聚合不起作用 - elasticsearch.js 客户端

2024-05-14

我尝试查询我的数据集有两个目的:

  1. 匹配一个术语(可转售 = true)
  2. 按价格对结果进行排序 最低到最高

数据集/文档是:

"data" : {
            "resellable" : true,
            "startingPrice" : 0,
            "id" : "4emEe_r_x5DRCc5",
            "buyNowPrice" : 0.006493, //Changes per object
            "sub_title" : "test 1",
            "title" : "test 1",
            "category" : "Education",
      
          }

//THREE OBJECTS WITH THE VALUES OF 0.006, 0.7, 1.05 FOR BUYNOWPRICE

我有三个不同的对象buyNowPrice

使用 agg 查询是:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "data.resellable": true
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 5,
    "aggs": {
        "lowestPrice": {
            "terms": {
                "field": "data.buyNowPrice",
                "order": {
                    "lowest_price": "desc"
                }
            },
            "aggs": {
                "lowest_price": {
                    "min": {
                        "field": "data.buyNowPrice"
                    }
                },
                "lowest_price_top_hits": {
                    "top_hits": {
                        "size": 5,
                        "sort": [
                            {
                                "data.buyNowPrice": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

查询工作正常,结果是 3 个对象resellable = true

问题是,聚合没有根据最低立即购买价格来组织结果。

每个结果中,buyNowPrice 的顺序是:1.06、0.006、0.7 - 顺序不正确。

切换到desc没有影响,所以我不相信聚合正在运行?

EDIT:

使用下面的建议我的查询现在看起来像:

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "data.resellable": true
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 5,
    "aggs": {
        "lowestPrice": {
            "terms": {
                "field": "data.buyNowPrice",
                "order": {
                    "lowest_price": "asc"
                }
            },
            "aggs": {
                "lowest_price": {
                    "min": {
                        "field": "data.buyNowPrice"
                    }
                },
                "lowest_price_top_hits": {
                    "top_hits": {
                        "size": 5
                    }
                }
            }
        }
    }
}

查询结果为:

  total: { value: 3, relation: 'eq' },
  max_score: 0.2876821,
  hits: [
    {
      _index: 'education',
      _type: 'listing',
      _id: '4emEe_r_x5DRCc5', <--- buyNowPrice of 0.006
      _score: 0.2876821,
      _source: [Object]
    },
    {
      _index: 'education',
      _type: 'listing',
      _id: '4ee_r_x5DRCc5', <--- buyNowPrice of 1.006
      _score: 0.18232156,
      _source: [Object]
    },
    {
      _index: 'education',
      _type: 'listing',
      _id: '4444_r_x5DRCc5', <--- buyNowPrice of 0.7
      _score: 0.18232156,
      _source: [Object]
    }
  ]
}

EDIT 2:

删除查询resellable = true聚合将正确排序并按正确的顺序返回项目。但如果包含可转售查询,则事实并非如此。

我假设这与_score属性覆盖 agg 的排序?如何解决这个问题


您可以使用桶排序聚合 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html这是一个父管道 聚合,对其父多桶的桶进行排序 聚合。零个或多个排序字段可以与 相应的排序顺序。

添加工作示例(使用问题中给出的相同索引数据)、搜索查询和搜索结果

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "data.resellable": true
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 5,
  "aggs": {
    "source": {
      "terms": {
        "field": "data.buyNowPrice"
      },
      "aggs": {
        "latest": {
          "top_hits": {
            "_source": {
              "includes": [
                "data.buyNowPrice",
                "data.id"
              ]
            }
          }
        },
        "highest_price": {
          "max": {
            "field": "data.buyNowPrice"
          }
        },
        "bucket_sort_order": {
          "bucket_sort": {
            "sort": {
              "highest_price": {
                "order": "desc"
              }
            }
          }
        }
      }
    }
  }
}

搜索结果:

"buckets": [
        {
          "key": 1.0499999523162842,
          "doc_count": 1,
          "highest_price": {
            "value": 1.0499999523162842
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 1.05          <-- note this
                    }
                  }
                }
              ]
            }
          }
        },
        {
          "key": 0.699999988079071,
          "doc_count": 1,
          "highest_price": {
            "value": 0.699999988079071
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "2",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 0.7         <-- note this
                    }
                  }
                }
              ]
            }
          }
        },
        {
          "key": 0.006000000052154064,
          "doc_count": 1,
          "highest_price": {
            "value": 0.006000000052154064
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "1",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 0.006         <-- note this
                    }
                  }
                }
              ]
            }
          }
        }
      ]

更新1:

如果您将搜索查询修改为:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "data.resellable": true
          }
        }
      ]
    }
  },
  "aggs": {
    "lowestPrice": {
      "terms": {
        "field": "data.buyNowPrice",
        "order": {
          "lowest_price": "asc"        <-- change the order here 
        }
      },
      "aggs": {
        "lowest_price": {
          "min": {
            "field": "data.buyNowPrice"
          }
        },
        "lowest_price_top_hits": {
          "top_hits": {
            "size": 5
          }
        }
      }
    }
  }
}

也运行上面的搜索查询,您将获得所需的结果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在 ElasticSearch 中查询和聚合,但聚合不起作用 - elasticsearch.js 客户端 的相关文章

随机推荐

  • 具有透明背景的 Swift 模态视图控制器 [重复]

    这个问题在这里已经有答案了 我知道这个话题很受欢迎 但我在编程语言中遇到了一些问题 事实是我仍然不明白我把代码放在哪里 好吧 我就来说说整个案子 我正在尝试制作一个与正常情况稍有不同的模态 Swift 通过单击按钮 ViewControll
  • 如何阻止与 RSpec 和 Capybara 的外部连接?

    在我的 Rails 项目中 我想编写非理想条件的测试 例如缺乏互联网连接或超时 例如 我正在使用 gem 来联系 API 并且希望确保在我的应用程序和外部 API 之间存在连接问题时能够正确处理错误 我已经可以通过用录像机制作固定装置并从
  • 多处理时如何获取每个进程ID

    我有一些问题 因为我是 Python 和 Pyside 的新手 我有N个进程同时运行 由于这些进程需要一些时间才能完成其工作 因此最终用户可能想要取消特定进程 因此 我需要一种方法来了解进程的 ID 以便将此功能添加到程序中 有一个answ
  • 查询 dns 别名

    我找到了一些code http msdn microsoft com en us library system net dns gethostbyaddress VS 71 aspx来自 msdn 站点 下面包含代码 看起来它将返回给定服务
  • 使用反射获取通用 IDictionary 的值

    我有一个实现的实例IDictionary
  • R 中的 as.numeric 有什么问题? [复制]

    这个问题在这里已经有答案了 gt X864291X8X74 1 8 0000000000 9 0000000000 10 0000000000 6 0000000000 8 0000000000 10 Levels 0 0000000000
  • Foreach Ajax Json - Jquery

    谁能帮我 我有一个数组 stars Chris Pine Keira Knightley Kevin Costner 我想做的是 foreach star 我想将输入附加到 div 和 foreach star 它们在输入中具有值 这就是我
  • 更新\插入数据从grafana到mysql

    可以从grafana更新数据或插入数据到mysql 我需要使用 UI 在 mysql 中插入 更新信息 现在我已经在使用grafana 所以想知道是否有任何方法可以使用grafana来更新或插入信息 没有用于获取用户输入并将该数据插入 My
  • 如何使用继承来建模 RESTful API?

    我有一个需要通过 RESTful API 公开的对象层次结构 但我不确定我的 URL 应该如何构建以及它们应该返回什么 我找不到任何最佳实践 假设我有从动物继承的狗和猫 我需要对狗和猫进行CRUD操作 我还希望能够对一般动物进行手术 我的第
  • 找不到 DBI.pm

    我正在尝试启动这个脚本 usr bin perl use DBI my dbh DBI gt connect dbi Oracle host lonlin2 sid TIMFX1AD port 1524 xbsesdbo xbsesdbo1
  • 如何设置 JSDoc 教程标题?

    我正在使用 JSDoc 教程 如中所述http usejsdoc org about tutorials html http usejsdoc org about tutorials html 我无法获取配置工作选项 我还没有在网上找到该主
  • 无论 DataAnnotations 属性如何,ModelState.IsValid 始终为 true

    我在 Visual Studio 2015 中使用新的 MVC6 框架 突然我所有的数据注释都停止工作 所有这些 无需我更改代码 public sealed class RegisterUser Required ErrorMessage
  • 如何创建一个没有边框且只能通过手柄调整大小的 WPF 窗口?

    如果你设置ResizeMode CanResizeWithGrip 在 WPF 上Window然后右下角会出现一个调整大小的夹点 如下 如果你设置WindowStyle None 标题栏也会消失 但灰色斜边仍然保留 直到您设置ResizeM
  • R Data.Table 创建带有条件的变量

    我需要在下面的数据集中创建一个新变量 A X a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 The newvar如果X等于 2 5 7 或 9 否则 newvar应该是 0 Code dt1 lt dat
  • 在Maven中生成Version.java文件

    我有一个使用 Ant 脚本构建的 Java 项目 我正在尝试将项目转换为 Maven 其中一项任务生成一个名为 Version java 的 Java 源文件 其中包含编译时间戳的静态字符串表示形式 如下所示 package com foo
  • 如何使用 htaccess 重定向 html 扩展?

    目前 这两个链接显示同一页面 http www example com podcast episode html http www example com podcast episode html http www example com
  • Jackson JSON + Java 泛型

    我正在尝试将以下 JSON 反序列化 映射到List
  • Java将字符串解析为double

    如何解析字符串中的这个 Double 00034800 变成 Double 值 最后两位数字实际上是小数点 所以我正在寻找的结果是348 00 是否有这样的格式可以与十进制格式一起使用 Well String s 00034800 doub
  • 如何查询多个链接服务器?

    链接一些 SQL Server 2008 服务器 实例后 我想对这些服务器进行更通用的查询 我知道我必须像这样指定查询的命运 select from SRV INSTANCE dbname dbo foo 但是 我会针对多个链接服务器运行此
  • 尝试在 ElasticSearch 中查询和聚合,但聚合不起作用 - elasticsearch.js 客户端

    我尝试查询我的数据集有两个目的 匹配一个术语 可转售 true 按价格对结果进行排序 最低到最高 数据集 文档是 data resellable true startingPrice 0 id 4emEe r x5DRCc5 buyNowP