ElasticSearch apply limit on bucket results

229 Views Asked by At

I am in a situation where I have applied limit for the ElasticSearch results but it's not working for me. I have gone through the ES guide below is my code:

module Invoices
    class RestaurantBuilder < Base
      def query(options = {})
        buckets = {}
        aggregations = {
          orders_count: { sum: { field: :orders_count } },
          orders_tip: { sum: { field: :orders_tip } },
          orders_tax: { sum: { field: :orders_tax } },
          monthly_fee: { sum: { field: :monthly_fee } },
          gateway_fee: { sum: { field: :gateway_fee } },
          service_fee: { sum: { field: :service_fee } },
          total_due: { sum: { field: :total_due } },
          total: { sum: { field: :total } }
        }

        buckets_for_restaurant_invoices buckets, aggregations, options[:restaurant_id]

        filters = []
        filters << time_filter(options)

        query = {
          query: { bool: { filter: filters } },
          aggregations: buckets,
          from: 0,
          size: 5
        }
        query
      end

      def buckets_for_restaurant_invoices(buckets, aggregations, restaurant_id)
        restaurant_ids(restaurant_id).each do |id|  
          buckets[id] = {
            filter: { term: { restaurant_id: id } },
            aggregations: aggregations
          }
        end
      end

      def restaurant_ids(restaurant_id)
        if restaurant_id
          [restaurant_id]
        else
          ::Restaurant.all.pluck :id
        end
      end
    end
end

the restaurant_ids function returns approx 5.5k restaurants so in this case i got an error "circuit_breaking_exception","reason":"[request] Data too large, data for [] would be [622777920/593.9mb], which is larger than the limit of [622775500/593.9mb]". That's why I want to apply some limit so that I can get only a few hundreds of records at a time.

Could anyone guide me where I am doing wrong?

1

There are 1 best solutions below

1
On

The way to limit the amount of data to avoid this error is to configure the indices.breaker.request.limit.