Another thing you can do is send the list of correct identifiers on the right to elastic so that they themselves filter the entries and then search for the remaining ones. We did not conduct tests, but still faster, but I think it is necessary, because elasticity is a search engine.
I will try to make an example using your classes + variables and our experience:
def search
@store_ids = Store.local(current_user).select(:id).pluck(:id)
@response = InventoryItem.search_by_store_ids('whatever', @store_ids)
end
And the model:
class InventoryItem
def self.search_by_store_ids(query, store_ids, options = {})
self.search_all(query, options.deep_merge({
query: {
filtered: {
filter: {
terms: {
store_id: store_ids
}
}
}
}
}))
end
def self.search_all(query, options = {})
self.__elasticsearch__.search(
{
query: {
filtered: {
query: {
match: { text: query },
},
filter: {},
strategy: 'leap_frog_filter_first'
}
}
}.deep_merge(options)
)
end
end
So you also need to index store_id.
Read more about filters here .
source
share