0

検索にはElastic SearchTireを使用しています。

すべてのモデルを検索するための 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

どうすればそれを行うのに良いパターンになりますか?

編集: 今のところ、以下のようなものが私にとってはうまくいきます:

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
4

1 に答える 1

1

multi_search結果をセグメント化する場合は、次のメソッドを使用するのが適切なパターンです。

require 'tire'

Tire.index('questions') do
  delete
  store type: 'question', app_id: 1, title: 'Question test 1'
  store type: 'question', app_id: 2, title: 'Question test 2'
  store type: 'question', app_id: 3, title: 'Question test 3'
  refresh
end

Tire.index('links') do
  delete
  store type: 'link', app_id: 1, title: 'Link test 1'
  store type: 'link', app_id: 2, title: 'Link 2'
  store type: 'link', app_id: 3, title: 'Link test 3'
  refresh
end

Tire.index('events') do
  delete
  store type: 'event', title: 'Event test 1'
  store type: 'event', title: 'Event test 2'
  store type: 'event', title: 'Event 3'
  refresh
end

multi_search = Tire.multi_search do
  search :all, indices: [:questions, :links, :events] do
    query { match :_all, 'test' }
  end
  search :rest, indices: [:questions, :links] do
    query do
      filtered do
        query { all }
        filter :terms, app_id: [1, 2]
      end
    end
  end
end

puts "ALL:  " + multi_search.results[:all].map(&:title).inspect
puts '---'
puts "REST: " + multi_search.results[:rest].map(&:title).inspect
于 2012-12-19T08:53:34.100 に答える