Я использую Elastic Search и Tire для поиска.
Я создал класс Search для поиска по всем моим моделям.
class Link < ActiveRecord::Base
...
tire.mapping do
indexes :id, :type => 'string', :index => :not_analyzed
indexes :app_id, :type => 'string', :index => :not_analyzed
indexes :title, analyzer: 'snowball', boost: 100
end
def to_indexed_json
to_json(
only: [:id, :app_id, :title]
)
end
end
class Search
def results
search = Tire.search [:questions, :answers, :links, :events] do
query { string "#{term}" }
end
search.results
end
end
Дело в том, чтобы вернуть только элементы из приложений 12
и 13
.
Поэтому я попытался отфильтровать все результаты, используя search.filter :and, must: {term: {app_id: [12, 13]}}
, но только questions
, links
и answers
имеют атрибут app_id
. И я хотел бы также вернуть все events
.
Как это сделать?
EDIT: На данный момент у меня работает что-то вроде ниже:
multi_search = Tire.multi_search do
search :without_app, indices: [:events, :past_events, :reviews] do
query { match :_all, "#{term}" }
end
search :with_app, indices: [:questions, :answers, :links, :service_providers] do
query do
filtered do
query { match :_all, "#{term}" }
filter :terms, app_id: app_ids
end
end
end
end
multi_search.results[:with_app].to_a + multi_search.results[:without_app].to_a
Tire
gem: github.com/karmi/tire/issues/551 - person tomekfranek   schedule 19.12.2012