Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add indices_boost option support #283

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

@ankane ankane Jul 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to a section about searching multiple indexes? (maybe right above the reference section)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I can take this one. Thanks @exAspArk!


```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