Try to query and aggregate in ElasticSearch but aggregrating not working - elasticsearch.js client

走远了吗. 提交于 2021-02-10 05:13:47

问题


I'm trying to query my dataset for two purposes:

  1. Match a term (resellable = true)
  2. Order the results by their price lowest to highest

Data set/doc is:

"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

I have three objects of these with different buyNowPrice

Query with agg is:

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

The query works fine, and the results are 3 objects that have resellable = true

The issue is, the agg is not organizing the results based off the lowest buy now price.

Each result, the order of buyNowPrice is: 1.06, 0.006, 0.7 - which is not ordered properly.

Switching to desc has no affect, so I don't believe the agg is running at all?

EDIT:

Using the suggestion below my query now looks like:

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

With the results of the query being:

  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:

Removing the query for resellable = true the aggregation will sort properly and return the items in the proper order. But with the query for resellable included, it does not.

I'm assuming this has to do with the _score property overriding the sorting from agg? How would this be fixed


回答1:


You can use a bucket sort aggregation that is a parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation. Zero or more sort fields may be specified together with the corresponding sort order.

Adding a working example (using the same index data as given in the question), search query, and search result

Search Query:

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

Search Result:

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

Update 1:

If you modify your search query as :

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

Running the above search query also, you will get your required results.



来源:https://stackoverflow.com/questions/64364468/try-to-query-and-aggregate-in-elasticsearch-but-aggregrating-not-working-elast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!