From 92e557a0be22564ea0316d25fe27764956996322 Mon Sep 17 00:00:00 2001 From: exAspArk Date: Mon, 25 Jul 2016 22:52:16 +0300 Subject: [PATCH] Add indices_boost option support --- README.md | 6 ++++++ lib/searchkick/query.rb | 16 ++++++++++++++++ test/boost_test.rb | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 1807aa090..a99a48e68 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,12 @@ boost_where: {user_id: {value: 1, factor: 100}} # default factor is 1000 boost_where: {user_id: [{value: 1, factor: 100}, {value: 2, factor: 200}]} ``` +Boost indices when searching across more than one index + +```ruby +indices_boost: { Animal => 1, Product => 200 } +``` + [Conversions](#keep-getting-better) are also a great way to boost. ### Get Everything diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index e9d493e81..b232f61a4 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -345,6 +345,9 @@ def prepare # order set_order(payload) if options[:order] + # indices_boost + set_boost_by_indices(payload) + # filters filters = where_filters(options[:where]) set_filters(payload, filters) if filters.any? @@ -469,6 +472,19 @@ def set_boost_where(custom_filters, personalize_field) end end + def set_boost_by_indices(payload) + return unless options[:indices_boost] + + indices_boost = options[:indices_boost].each_with_object({}) do |(key, boost), memo| + index = key.respond_to?(:searchkick_index) ? key.searchkick_index.name : key + # try to use index explicitly instead of alias: https://github.com/elasticsearch/elasticsearch/issues/4756 + index_by_alias = Searchkick.client.indices.get_alias(index: index).keys.first + memo[index_by_alias || index] = boost + end + + payload[:indices_boost] = indices_boost + end + def set_suggestions(payload) suggest_fields = (searchkick_options[:suggest] || []).map(&:to_s) diff --git a/test/boost_test.rb b/test/boost_test.rb index 07588b18d..439077efa 100644 --- a/test/boost_test.rb +++ b/test/boost_test.rb @@ -141,4 +141,11 @@ def test_boost_by_distance_hash ] assert_order "san", ["San Francisco", "San Antonio", "San Marino"], boost_by_distance: {field: :location, origin: {lat: 37, lon: -122}, scale: "1000mi"} end + + def test_boost_by_indices + store_names ["Rex"], Animal + store_names ["Rexx"], Product + + assert_order "Rex", ["Rexx", "Rex"], {index_name: [Animal, Product], indices_boost: {Animal => 1, Product => 200}}, Store + end end