首页 > 解决方案 > 尝试在 ElasticSearch 中查询和聚合,但聚合不起作用 - elasticsearch.js 客户端

问题描述

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

  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将正确排序并以正确的顺序返回项目。但是,如果包含对可转售的查询,则不会。

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

标签: javascriptnode.jselasticsearchelasticsearch-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
          }
        }
      }
    }
  }
}

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


推荐阅读