Skip to content Skip to sidebar Skip to footer

Try To Query And Aggregate In ElasticSearch But Aggregrating Not Working - Elasticsearch.js Client

I'm trying to query my dataset for two purposes: Match a term (resellable = true) Order the results by their price lowest to highest Data set/doc is: 'data' : { 'rese

Solution 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.


Post a Comment for "Try To Query And Aggregate In ElasticSearch But Aggregrating Not Working - Elasticsearch.js Client"