【问题标题】:Try to query and aggregate in ElasticSearch but aggregrating not working - elasticsearch.js client尝试在 ElasticSearch 中查询和聚合,但聚合不起作用 - elasticsearch.js 客户端
【发布时间】:2021-01-29 12:26:35
【问题描述】:

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

  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 的对象

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

每个结果,buyNowPrice 的顺序是:1.06、0.006、0.7 - 没有正确排序。

切换到desc 没有任何影响,所以我根本不相信 agg 正在运行?

编辑:

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

{
    "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]
    }
  ]
}

编辑 2:

删除对resellable = true 聚合的查询将正确排序并以正确的顺序返回项目。但如果包含对可转售的查询,则不会。

我假设这与覆盖 agg 排序的 _score 属性有关?这将如何解决

【问题讨论】:

    标签: javascript node.js elasticsearch elasticsearch-aggregation


    【解决方案1】:

    您可以使用作为父管道的bucket sort aggregation 对其父多桶的桶进行排序的聚合 聚合。可以指定零个或多个排序字段 对应的排序顺序。

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

    搜索查询:

    {
      "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
              }
            }
          }
        }
      }
    }
    

    同时运行上述搜索查询,您将获得所需的结果。

    【讨论】:

    • 感谢您的回答。但是,当我直接将您的答案复制并粘贴到我的查询中时,我得到 0 个结果/点击。当我将size: 0 更改为 5 时,我得到了相同的点击,但同样,没有排序。为什么你有大小:0?还有什么是热门歌曲的来源键?我需要返回整个对象,而不仅仅是 ID 和 buyNowPrice
    • @Zach "size":0 只是为了让我没有得到点击结果,我只能关注聚合部分,但您可以将其更改为 "from": 0, "size": 5, (正如您在您的搜索查询
    • 我确实将其更改为 size: 5,结果未按您的结果排序。它们与以前完全相同,无序 - 1.05、0.006、0.7。另外,您的 top_hits 中的源密钥是什么?我需要返回整个 doc 对象,而不仅仅是两个值
    • @Zach 你能试试其他修改过的查询(我现在更新了),如果这解决了你的问题,请告诉我? _source 参数用于选择返回源的哪些字段。我在本地尝试了上述两个查询,它们以正确的顺序给了我搜索结果。
    • 同时使用您的第一个查询和第二个更新返回相同的结果:total: { value: 3, relation: 'eq' }, max_score: 0.2876821, hits: [ { _index: 'education', _type: 'listing', _id: '4emEe_r_x5DRCc5', &lt;-- 1.05 _score: 0.2876821, }, { _index: 'education', _type: 'listing', _id: '4ee_r_x5DRCc5', &lt;-- 0.006 _score: 0.18232156, }, { _index: 'education', _type: 'listing', _id: '4444_r_x5DRCc5', &lt;-- 0.7 _score: 0.18232156, } ] }
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2014-11-06
    • 2022-01-18
    • 2019-12-06
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多