Find Tires / Elicsearch to Combine

I have the following code and I am trying to use ElasticSearch to query it.

It works when I execute Book.search (: q => 'Foo'), but it doesn't work when I execute Book.search (: author => 'Doctor'). In my database I have an entry named "Barz, Foo, Doctor"

I'm not sure if I should use terms or terms in my query because I am breaking the name using snowball. I tried with conditions and then I get an error. With the term I am not getting any results.

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

      

+3


source to share


2 answers


You can do it like this:

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

      

OR



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

      

OR



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

      

+3


source


As @Karmi said enter link here

Hi yes your approach seems to be one. A couple of things: * if you don't want to use the Lucene query syntax (boost, ranges, etc.) it might be best to use a text query, * yes, filters are more efficient, then queries active = true in your example is fine for filters. Beware of interactions between queries, filters, and facets. Your definition of the term query is wrong, but it should be:



term :author, params[:author]

      

0


source







All Articles