我正在Elasticsearch中索引附件(通过 Tire gem),它们很大。无论出于什么原因,我都没想到这一点,搜索速度很慢。似乎是因为 Tire (ES)在其搜索结果中包括了文档的整个_source。这是不必要的,但我不知道如何将其关闭。

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

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

有人知道如何从Tire搜索中排除元素吗?
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
  ...

最佳答案

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

10-08 04:22