如何从Tire gem中的搜索结果中删除_source for elasticsearch

时间:2012-09-22 00:33:06

标签: ruby-on-rails elasticsearch tire

我正在索引Elasticsearch中的附件(通过 Tire gem)并且它们非常大。无论出于何种原因,我没有想到这一点,搜索速度很慢。这似乎是因为轮胎(ES)在其搜索结果中包含了文档的整个_source。这是不必要的,但我无法弄清楚如何关闭它。

直接与ES通信可以包含partial_fields元素来限制它:

"partial_fields" : {
  "no_PDFs" : {
    "exclude" : ["attachment", "reports.attachment"]
  }
}

任何人都知道如何从轮胎搜索中排除元素?

class Report < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks
  ...
  tire.mapping do
    indexes :id, :type =>'integer'
    indexes :title
    indexes :attachment, :type => 'attachment', 
          :fields => {
          :author     => { :store => 'yes' },
          :attachment => { :term_vector => 'with_positions_offsets', :store => 'yes' },
          :date       => { :store => 'yes' }
    }
  end

  def self.search(params)
    tire.search do 
      query { string params[:query] } if params[:query].present? 
      highlight :attachment 
    end
  end
  ...

1 个答案:

答案 0 :(得分:2)

Tire中没有partial_fields的直接支持。

使用搜索方法的fields选项限制响应:

require 'tire'

Tire.index 'bigfields-test' do
  delete and create

  store title: 'Test 1', content: 'x'*100_000
  store title: 'Test 2', content: 'x'*100_000

  refresh
end

s = Tire.search 'bigfields-test', fields: 'title' do
  query { string 'test' }
end

p s.results