【发布时间】:2021-01-29 12:26:35
【问题描述】:
我试图查询我的数据集有两个目的:
- 匹配一个字词(可转售 = true)
- 按价格排序结果 从最低到最高
数据集/文档是:
"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