0

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
  ...
karmi
  • 14,059
  • 3
  • 33
  • 41
Meltemi
  • 37,979
  • 50
  • 195
  • 293

1 Answers1

2

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
karmi
  • 14,059
  • 3
  • 33
  • 41
  • works great! thanks! But, what's the syntax for passing more than one field (or perhaps an 'except' option to exclude fields)? I've tried `fields: ['title', 'id']`, among others attempts, unsuccessfully. I know ES wants an array but Tire doesn't seem to want to pass it along... what'd I miss? – Meltemi Sep 24 '12 at 22:50
  • got it: `fields: 'title,id'`. i guess the space wasn't being stripped from URL communicating with ES. – Meltemi Sep 24 '12 at 23:03
  • just 'discovered' `:include_in_all => false`. Could this be used in the mapping to eliminate :attachment from all results? Something like: `indexes :attachment, :type => 'attachment', :include_in_all => false, ...` ?!? – Meltemi Sep 25 '12 at 00:57