Skip to content

Commit

Permalink
Merge pull request #283 from exAspArk/indices_boost
Browse files Browse the repository at this point in the history
add indices_boost option support
  • Loading branch information
ankane authored Jul 26, 2016
2 parents 070621a + 92e557a commit 9c1a0ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/searchkick/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions test/boost_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c1a0ff

Please sign in to comment.