-
Notifications
You must be signed in to change notification settings - Fork 52
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 federated search #393
Merged
Merged
Add federated search #393
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require_relative 'multi_search/result' | ||
require_relative 'multi_search/multi_search_result' | ||
require_relative 'multi_search/federated_search_result' | ||
|
||
module Meilisearch | ||
module Rails | ||
|
@@ -12,23 +13,60 @@ def multi_search(searches) | |
normalize(options, index_target) | ||
end | ||
|
||
MultiSearchResult.new(searches, client.multi_search(search_parameters)) | ||
MultiSearchResult.new(searches, client.multi_search(queries: search_parameters)) | ||
end | ||
|
||
def federated_search(queries:, federation: {}) | ||
if federation.nil? | ||
Meilisearch::Rails.logger.warn( | ||
'[meilisearch-rails] In federated_search, `nil` is an invalid `:federation` option. To explicitly use defaults, pass `{}`.' | ||
) | ||
|
||
federation = {} | ||
end | ||
|
||
queries.map! { |item| [nil, item] } if queries.is_a?(Array) | ||
|
||
cleaned_queries = queries.filter_map do |(index_target, options)| | ||
model_class = options[:scope].respond_to?(:model) ? options[:scope].model : options[:scope] | ||
index_target = options.delete(:index_uid) || index_target || model_class | ||
|
||
strip_pagination_options(options) | ||
normalize(options, index_target) | ||
end | ||
|
||
raw_results = client.multi_search(queries: cleaned_queries, federation: federation) | ||
|
||
FederatedSearchResult.new(queries, raw_results) | ||
end | ||
|
||
private | ||
|
||
def normalize(options, index_target) | ||
index_target = index_uid_from_target(index_target) | ||
|
||
return nil if index_target.nil? | ||
|
||
options | ||
.except(:class_name, :scope) | ||
.merge!(index_uid: index_uid_from_target(index_target)) | ||
.merge!(index_uid: index_target) | ||
end | ||
|
||
def index_uid_from_target(index_target) | ||
case index_target | ||
when String, Symbol | ||
index_target | ||
else | ||
index_target.index.uid | ||
when Class | ||
if index_target.respond_to?(:index) | ||
index_target.index.uid | ||
else | ||
Meilisearch::Rails.logger.warn <<~MODEL_NOT_INDEXED | ||
[meilisearch-rails] This class was passed to a multi/federated search but it does not have an #index: #{index_target} | ||
[meilisearch-rails] Are you sure it has a `meilisearch` block? | ||
MODEL_NOT_INDEXED | ||
|
||
nil | ||
end | ||
end | ||
end | ||
|
||
|
@@ -44,6 +82,20 @@ def paginate(options) | |
options[:page] ||= 1 | ||
end | ||
|
||
def strip_pagination_options(options) | ||
pagination_options = %w[page hitsPerPage hits_per_page limit offset].select do |key| | ||
options.delete(key) || options.delete(key.to_sym) | ||
end | ||
|
||
return if pagination_options.empty? | ||
|
||
Meilisearch::Rails.logger.warn <<~WRONG_PAGINATION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These warnings will be very helpful! |
||
[meilisearch-rails] Pagination options in federated search must apply to whole federation. | ||
[meilisearch-rails] These options have been removed: #{pagination_options.join(', ')}. | ||
[meilisearch-rails] Please pass them after queries, in the `federation:` option. | ||
WRONG_PAGINATION | ||
end | ||
|
||
def pagination_enabled? | ||
Meilisearch::Rails.configuration[:pagination_backend] | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is necessary due to this change in ms-ruby: meilisearch/meilisearch-ruby@055f7d0