Commit e270c44bfcf1a6d7329a06d88b6ee6efad51f7b8
Exists in
master
and in
21 other branches
Merge pull request #454 from meetrajesh/master
[Facets] Support overriding field in facet search
Showing
2 changed files
with
9 additions
and
3 deletions
Show diff stats
lib/searchkick/query.rb
@@ -301,7 +301,7 @@ module Searchkick | @@ -301,7 +301,7 @@ module Searchkick | ||
301 | else | 301 | else |
302 | payload[:facets][field] = { | 302 | payload[:facets][field] = { |
303 | terms: { | 303 | terms: { |
304 | - field: field, | 304 | + field: facet_options[:field] || field, |
305 | size: size | 305 | size: size |
306 | } | 306 | } |
307 | } | 307 | } |
test/facets_test.rb
@@ -21,6 +21,12 @@ class TestFacets < Minitest::Test | @@ -21,6 +21,12 @@ class TestFacets < Minitest::Test | ||
21 | assert_equal ({1 => 1}), store_facet(facets: {store_id: {where: {in_stock: true}}}) | 21 | assert_equal ({1 => 1}), store_facet(facets: {store_id: {where: {in_stock: true}}}) |
22 | end | 22 | end |
23 | 23 | ||
24 | + def test_field | ||
25 | + assert_equal ({1 => 1, 2 => 2}), store_facet(facets: {store_id: {}}) | ||
26 | + assert_equal ({1 => 1, 2 => 2}), store_facet(facets: {store_id: {field: "store_id"}}) | ||
27 | + assert_equal ({1 => 1, 2 => 2}), store_facet({facets: {store_id_new: {field: "store_id"}}}, "store_id_new") | ||
28 | + end | ||
29 | + | ||
24 | def test_limit | 30 | def test_limit |
25 | facet = Product.search("Product", facets: {store_id: {limit: 1}}).facets["store_id"] | 31 | facet = Product.search("Product", facets: {store_id: {limit: 1}}).facets["store_id"] |
26 | assert_equal 1, facet["terms"].size | 32 | assert_equal 1, facet["terms"].size |
@@ -78,8 +84,8 @@ class TestFacets < Minitest::Test | @@ -78,8 +84,8 @@ class TestFacets < Minitest::Test | ||
78 | 84 | ||
79 | protected | 85 | protected |
80 | 86 | ||
81 | - def store_facet(options) | ||
82 | - Hash[Product.search("Product", options).facets["store_id"]["terms"].map { |v| [v["term"], v["count"]] }] | 87 | + def store_facet(options, facet_key="store_id") |
88 | + Hash[Product.search("Product", options).facets[facet_key]["terms"].map { |v| [v["term"], v["count"]] }] | ||
83 | end | 89 | end |
84 | 90 | ||
85 | end | 91 | end |