Commit ad738dea49c5013d69b85ec8545a6817a2b16315

Authored by Andrew Kane
1 parent 420f81e7

Test aggs

Showing 2 changed files with 16 additions and 1 deletions   Show diff stats
lib/searchkick/relation.rb
... ... @@ -5,7 +5,7 @@ module Searchkick
5 5 attr_reader :klass, :term, :options
6 6  
7 7 def_delegators :execute, :map, :each, :any?, :empty?, :size, :length, :slice, :[], :to_ary,
8   - :records, :results, :suggestions, :each_with_hit, :with_details, :aggregations, :aggs,
  8 + :records, :results, :suggestions, :each_with_hit, :with_details, :aggregations,
9 9 :took, :error, :model_name, :entry_name, :total_count, :total_entries,
10 10 :current_page, :per_page, :limit_value, :total_pages, :num_pages,
11 11 :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?,
... ... @@ -222,6 +222,20 @@ module Searchkick
222 222 self
223 223 end
224 224  
  225 + def aggs(*args)
  226 + if args.empty?
  227 + execute.aggs
  228 + else
  229 + aggs!(*args)
  230 + end
  231 + end
  232 +
  233 + # TODO merge options
  234 + def aggs!(*args)
  235 + options[:aggs] = args
  236 + self
  237 + end
  238 +
225 239 # same as Active Record
226 240 def inspect
227 241 entries = results.first(11).map!(&:inspect)
... ...
test/aggs_test.rb
... ... @@ -13,6 +13,7 @@ class AggsTest < Minitest::Test
13 13  
14 14 def test_basic
15 15 assert_equal ({1 => 1, 2 => 2}), store_agg(aggs: [:store_id])
  16 + assert_equal ({1 => 1, 2 => 2}), buckets_as_hash(Product.search("Product", relation: true).aggs(:store_id).aggs["store_id"])
16 17 end
17 18  
18 19 def test_where
... ...