【问题标题】:Tire gem: Use only to generate query instead of executing the queryTire gem:仅用于生成查询而不是执行查询
【发布时间】:2013-12-24 13:10:23
【问题描述】:

我正在使用轮胎 gem 在我的数据库中查询弹性搜索。现在,我有一个模型ContentElastic,它连接到索引posts 和映射post。我将elasticsearch用作单个数据库,而不是作为另一个数据库的搜索引擎。所以,模型ContentElastic 是这样的:

class ContentElastic
  include Tire::Model::Persistence
  include Tire::Model::Search
  include Tire::Model::Callbacks

  index_name 'posts'
  document_type 'post'

  # fields
  property :id
  property :type, :type => "string"
  property :username 
  property :name 
  property :datasource 
  property :content 
  property :language_id, :type => "long" 
  property :geotag 
  property :geo_enabled, :type => "boolean"
  property :language_confidence, :type => "long"
  property :tweet_id
  ...

end

现在,我写了一个这样的查询,从 elasticsearch 数据库中获取 10,000 条记录:

ContentElastic.search do
  fields wanted_fields if wanted_fields.present?

  query do
    filtered do
      query { string search_query } if search_query.present?
      #### Filters 
      range_filters.to_a.each do |rf|
        filter :range, rf if rf.present?
      end

      not_filters.to_a.each do |nf|
        filter :not, nf if nf.present?
      end

      or_filters.to_a.each do |of|
        filter :or, of if of.present?
      end

      and_filters.to_a.each do |af|
        filter :and, af if af.present?
      end

      filter :script, {:script => script} if script.present?

      terms_filters.to_a.each do |tf|
        filter :terms, tf if tf.present?
      end
    end
  end

  size 10000
end

查询工作正常,我们正在获得结果。问题在于,对于 10,000 条记录,转换为 ruby​​ 对象需要时间。有没有办法直接从轮胎声明中获取 elasticsearch 查询,并通过 RestClient 单独调用。

有没有办法在不执行轮胎查询的情况下获得弹性搜索查询。我在轮胎搜索块之后立即在轮胎中尝试了to_curl 方法,但似乎查询已经执行并且显示为Undefined method 'to_curl' for <#Tire::Collection>

【问题讨论】:

    标签: ruby-on-rails-3 tire


    【解决方案1】:

    我能够找到解决方法。我可以像这样调用Tire.search,而不是直接调用ContentElastic.search

    tire_block = Proc.new do
      fields wanted_fields if wanted_fields.present?
    
      query do
        filtered do
          query { string search_query } if search_query.present?
          #### Filters 
          range_filters.to_a.each do |rf|
            filter :range, rf if rf.present?
          end
    
          not_filters.to_a.each do |nf|
            filter :not, nf if nf.present?
          end
    
          or_filters.to_a.each do |of|
            filter :or, of if of.present?
          end
    
          and_filters.to_a.each do |af|
            filter :and, af if af.present?
          end
    
          filter :script, {:script => script} if script.present?
    
          terms_filters.to_a.each do |tf|
            filter :terms, tf if tf.present?
          end
        end
      end
    
      size 10000
    end
    
    t = Tire.search(ContentElastic.index.name, :type => ContentElastic.document_type, :size => limit, :from => from, &tire_block)
    

    这个轮胎调用没有执行查询,而是给出了轮胎搜索对象。然后,我可以像这样使用 RestClient 执行对应的弹性查询:

    post_url = t.url + (t.params.empty? ? '?' : t.params.to_s)
    response = RestClient.post(post_url, t.to_json.gsub("'",'\u0027'))
    

    响应将是来自 elasticsearch 的 json 字符串,我可以通过使用 JSON.parse 解析并获取适当的标签来使用这个 json_string。

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 2013-10-23
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多