轮胎/弹性搜索搜索关联



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

当我做Book.search(:q=>'Foo')时

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

不确定我是否应该在我的查询中使用术语或术语,因为我正在使用 snowball 破坏名称。我尝试使用术语,然后出现错误。对于学期,我没有得到任何结果。

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

你可以这样做

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?

如@Karmi所述,在此处输入链接描述

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

term :author, params[:author]

最新更新