【问题标题】:Tire/Elasticsearch search for associationTire/Elasticsearch 搜索关联
【发布时间】:2012-04-21 17:35:41
【问题描述】:

我有以下代码,我正在尝试使用 ElasticSearch 来查询它。

当我执行 Book.search(:q=>'Foo') 时它可以工作,但是当我执行 Book.search(:author=>'Doctor') 时它不起作用。在我的数据库中,我有一个名称为“Barz, Foo, Doctor”的条目

我不确定在查询中是否应该使用术语或术语,因为我使用雪球破坏了名称。我尝试了条款,然后出现错误。在任期内我没有得到任何结果。

class Author < ActiveRecord::Base
    has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :author
  include Tire::Model::Search
  include Tire::Model::Callbacks
  mapping do
    indexes :title,
    indexes :description
    indexes :author,type: 'object', properties: {
         name: { type: 'multi_field',
                 fields: { name:  { type: 'string', analyzer: 'snowball' },
                           exact: { type: 'string', index: 'not_analyzed' } 
                  } 
          } }
  end

  def to_indexed_json
   to_json(:include=>{:author=>{:only=>[:name]}} )
  end

  def self.search(params = {})
    tire.search(load:true) do
      query do
       boolean do
        should { string params[:q] } if params[:q].present?
        should { term params[:author] } if params[:author].present?
       end
      end
      filter :term, :active=>true
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails lucene elasticsearch tire


    【解决方案1】:

    你可以这样做

    should { terms :author, [params[:author]]} if params[:author].present?
    

    should { term :author, params[:author]} if params[:author].present?
    

    should { string "author:#{params[:author]}"} if params[:author].present?
    

    【讨论】:

      【解决方案2】:

      正如@Karmi 所说enter link description here

      嗨,是的,你的方法似乎是一种。几件事: * 除非您想使用 Lucene 查询语法(增强、范围等),否则最好使用文本查询, * 是的,过滤器比查询性能更高,您的示例中的 active=true 非常适合过滤器。不过要注意查询、过滤器和构面之间的相互作用。 不过,您对术语查询的定义是不正确的——它应该是:

      term :author, params[:author]
      

      【讨论】:

        猜你喜欢
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多