【问题标题】:How to remove _source from search results in Tire gem for elasticsearch如何从轮胎 gem 的搜索结果中删除 _source 以进行弹性搜索
【发布时间】:2012-09-22 00:33:06
【问题描述】:

我正在 Elasticsearch 中索引附件(通过 Tire gem),它们非常大。无论出于何种原因,我没想到会这样,搜索速度很慢。这似乎是因为 Tire (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
  ...

【问题讨论】:

    标签: ruby-on-rails elasticsearch tire


    【解决方案1】:

    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
    

    【讨论】:

    • 效果很好!谢谢!但是,传递多个字段的语法是什么(或者可能是排除字段的“除外”选项)?我尝试过fields: ['title', 'id'] 等尝试,但均未成功。我知道 ES 想要一个数组,但 Tire 似乎不想传递它……我错过了什么?
    • 知道了:fields: 'title,id'。我猜这个空间并没有从与 ES 通信的 URL 中删除。
    • 刚刚“发现”:include_in_all =&gt; false。这可以在映射中使用以消除所有结果中的 :attachment 吗?比如:indexes :attachment, :type =&gt; 'attachment', :include_in_all =&gt; false, ...?!?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    相关资源
    最近更新 更多