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

a1o7rhls  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(408)

我尝试查询我的数据集有两个目的:
匹配术语(可转售=真)
按价格从低到高排序结果
数据集/文档是:

"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的排序?如何修复

gjmwrych

gjmwrych1#

您可以使用一个bucket sort聚合,它是一个父管道聚合,对其父多bucket聚合的bucket进行排序。可以指定零个或多个排序字段以及相应的排序顺序。
添加一个工作示例(使用问题中给出的相同索引数据)、搜索查询和搜索结果
搜索查询:

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

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

相关问题