简体   繁体   中英

How to remove _source from search results in Tire gem for elasticsearch

I'm indexing attachments in Elasticsearch (via Tire gem) and they're quite large. For whatever reason, and I wasn't expecting this, the search is dog slow. It appears to be because Tire (ES) is including the entire _source of the document in its search results. This is unnecessary but I can't figure out how to turn it off.

Communicating directly with ES one could include a partial_fields element to restrict it:

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

Anyone know how to exclude elements from Tire search?

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
  ...

There is no direct support for partial_fields in Tire yet.

Limit the response by using the fields option for the search method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM