Rails elasticsearch aggregation

前端 未结 1 989
独厮守ぢ
独厮守ぢ 2021-02-09 16:33

Somehow I can\'t seem to get a response containing my aggregations...

Using curl it works as expected:

HBZUMB01$ curl -XPOST \"http://localhost:9200/cont         


        
相关标签:
1条回答
  • 2021-02-09 17:03

    I was also struggling with this, but now I found out how to get the aggregation results.

    If you're using the elasticsearch-rails with elasticsearch-model gems, when you run an aggregation on a model, you can get the buckets like in this example:

     agg = Model.search( 
         query: { match: { param: 'value' } }, 
         aggs: {my_aggregation_name: { terms: { field: :my_field} }} 
     )
    

    In your RoR code:

    agg.response["aggregations"]["my_aggregation_name"]["buckets"]
    

    From that, you'll get a result like this:

    [{"key"=>"banana",
      "doc_count"=>1963,
      "score"=>478.30920868573355,
      "bg_count"=>2152},
     {"key"=>"potato",
      "doc_count"=>1212,
      "score"=>315.68857496078505,
      "bg_count"=>1243}, ...]
    

    Then you can do whatever you want! Hope that helps!

    0 讨论(0)
提交回复
热议问题