ruby-on-rails - 仅当资源具有特定属性时如何过滤搜索集合?

标签 ruby-on-rails ruby search elasticsearch tire

我正在使用 Elasticsearch 轮胎 在搜索中。

我创建了 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

案例是只返回来自应用程序的项目 1213 .

所以我尝试使用 search.filter :and, must: {term: {app_id: [12, 13]}} 过滤所有结果但只有 questions , linksanswersapp_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

最佳答案

一个好的模式可能是使用 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

关于ruby-on-rails - 仅当资源具有特定属性时如何过滤搜索集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13940005/

相关文章:

css - 如何在具有 Mechanize 的下一个 sibling 中仅选择一种类型的第一个节点? ( ruby )

ruby - 安装带有 Rails : "Could not find "Nokogiri. 的 Nokogiri Gem 的问题 .."

ruby-on-rails - 在 Rails Controller 中,在 'map.do' 循环中创建新索引失败,但在 Ruby 中没问题

ruby-on-rails - ruby 中 CSV.open( ) 的第二个参数/参数是什么?

ruby-on-rails - 如何将参数传递给 ruby​​ 中的 rake 任务

iphone - 如何在 iBooks 上搜索桌面 UI

swift - 过滤联系人返回混合联系信息 - Swift

search - 谷歌搜索结果在 iframe 替代

ruby-on-rails - rails : Using CanCan to assign multiple roles to Users for each organization they belong to?

ruby-on-rails - RSpec 测试调用 ActiveMailer 模型