3

次のコードがあり、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
4

2 に答える 2

3

あなたはこのようにすることができます

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?
于 2012-04-09T12:30:15.103 に答える
0

@Karmiが述べたように、ここにリンクの説明を入力してください

こんにちは、ええ、あなたのアプローチは1つのようです。いくつかのこと:* Luceneクエリ構文(ブースト、範囲など)を使用する場合を除いて、テキストクエリを使用するのがおそらく最善です。*はい、フィルターはクエリよりもパフォーマンスが高く、例のactive=trueはフィルタに最適です。ただし、クエリ、フィルター、ファセット間の相互作用に注意してください。クエリという用語の定義は正しくありませんが、次のようになります。

term :author, params[:author]
于 2012-04-09T22:20:27.920 に答える