Searchkick: Sub-aggregation doesn't work

Created on 23 Aug 2017  Â·  3Comments  Â·  Source: ankane/searchkick

Does searchkick support sub-aggregation? Or did I make a mistake below?

I'm trying to get sub-aggregation of pricing under each pricing unit (e.g. weekly, month) aggregation in the search result by:

Product.search '*',   "aggs": {
          "pricing_options.unit": {
            "terms": {
              "field": "pricing_options.unit"
            },
            "aggs": {
              "pricing_options.unit_price": {
                "histogram": {
                  "field": "pricing_options.unit_price",
                  "interval": 300,
                  "min_doc_count": 1
                }
              }
            }
          }
        }

The above aggregation only returns 1 level of aggregation and sub-aggregation are completely missing:

  "aggregations": {
    "pricing_options.unit": {
      "doc_count": 9,
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "week",
          "doc_count": 8
        },
        {
          "key": "month",
          "doc_count": 3
        },
        {
          "key": "day",
          "doc_count": 1
        },
        {
          "key": "year",
          "doc_count": 1
        }
      ]
    }
  }

But what I really want is:

  "aggregations": {
    "pricing_options.unit": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "week",
          "doc_count": 8,
          "pricing_options.unit_price": {
            "buckets": [
              {
                "key": 0,
                "doc_count": 4
              },
              {
                "key": 300,
                "doc_count": 4
              },
              {
                "key": 600,
                "doc_count": 2
              },
              {
                "key": 900,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "month",
          "doc_count": 3,
          "pricing_options.unit_price": {
            "buckets": [
              {
                "key": 0,
                "doc_count": 2
              },
              {
                "key": 300,
                "doc_count": 2
              },
              {
                "key": 600,
                "doc_count": 1
              },
              {
                "key": 4500,
                "doc_count": 1
              },
              {
                "key": 45900,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "day",
          "doc_count": 1,
          "pricing_options.unit_price": {
            "buckets": [
              {
                "key": 0,
                "doc_count": 1
              },
              {
                "key": 300,
                "doc_count": 1
              },
              {
                "key": 600,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "year",
          "doc_count": 1,
          "pricing_options.unit_price": {
            "buckets": [
              {
                "key": 4500,
                "doc_count": 1
              },
              {
                "key": 45900,
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }

All 3 comments

Hey :) I just had the same issue, and for what I understood by its source code it doesn't. I think @ankane could confirm that :)

https://github.com/ankane/searchkick/blob/3dc4776bb69b5f2c59d29f48fd7edf618d887ee9/lib/searchkick/query.rb#L642

What I'm doing right now to execute sub-aggregations, is executing arbitrary JSON through the body_options parameter (documented here: https://github.com/ankane/searchkick#advanced-search).

Example:

Offer.search('something',
  {
  body_options: {
    aggs: {
      ranked_cheapest_offers: {
        terms: {
          field: :product_id
        },
        aggs:  {
          offers: {
            top_hits: {
              sort:    { product_price_ranking: :asc },
              size:    1,
              _source: :_id
            }
          }
        }
      }
    }
  }
}
)

And you can retrieve the aggregations hash through the Searchkick::Results#aggs method. I'm extracting all keys I want using Hashie#deep_select. Hope that helps :)

Wow, thanks very much Tiago. The override does work!

From: Tiago Amaro notifications@github.com
Reply-To: ankane/searchkick reply@reply.github.com
Date: Thursday, 24 August 2017 at 12:47 am
To: ankane/searchkick searchkick@noreply.github.com
Cc: Carol Liu carol.liu@rea-group.com, Author author@noreply.github.com
Subject: Re: [ankane/searchkick] Sub-aggregation doesn't work (#976)

Hey :) I just had the same issue, and for what I understood by its source code it doesn't. I think @ankanehttps://github.com/ankane could confirm that :)

https://github.com/ankane/searchkick/blob/3dc4776bb69b5f2c59d29f48fd7edf618d887ee9/lib/searchkick/query.rb#L642

What I'm doing right now to execute sub-aggregations, is executing arbitrary JSON through the body_options parameter (documented here: https://github.com/ankane/searchkick#advanced-search).

Example:

Offer.search('something',

{

body_options: {

aggs: {

  ranked_cheapest_offers: {

    terms: {

      field: :product_id

    },

    aggs:  {

      offers: {

        top_hits: {

          sort:    { product_price_ranking: :asc },

          size:    1,

          _source: :_id

        }

      }

    }

  }

}

}

}

)

And you can retrieve the aggregations hash through the Searchkick::Results#aggs method. I'm extracting all keys I want using Hashie#deep_select. Hope that helps :)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/ankane/searchkick/issues/976#issuecomment-324359232, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAsT4Tk4ASGcQSD5BaeuZ066u348h9h1ks5sbDuEgaJpZM4O_edq.

Thanks @tiagoamaro 💯 Confirmed that body_options is the best way to do sub-aggregations (the aggs option doesn't support them).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jakcharlton picture jakcharlton  Â·  5Comments

namila picture namila  Â·  4Comments

jiujiubad picture jiujiubad  Â·  4Comments

cubaraphael picture cubaraphael  Â·  3Comments

mehulkar picture mehulkar  Â·  5Comments