Elastic Search / Tire: How do I match an association attribute?

I use Tire to find elasticity. In my application, I have 2 models; Price and product.

I am trying to search in the Price class and use the Product that belongs to the :name attribute for the search field. Right now, if I have a product called Product 1 and type "pro", "prod" or "duct", the results will not appear. But by typing “product” or “Product”, you will see the results. I believe the problem lies in my comparison. I looked at the request and it:

 ...localhost:3000/search/results?utf8=%E2%9C%93&query=product 

When I think it should be:

 ...localhost:3000/search/results?utf8=%E2%9C%93&query:product=product 

Judging by this question: ElasticSearch mapping does not work

I do not know how to make my params[:query] only product.name . I tried using: string params[:query], default_field: "product.name" , but that didn't work.

I do not want to use the _all field.

Here is my code:

Price.rb

  include Tire::Model::Search include Tire::Model::Callbacks def self.search(params) tire.search(load: true, page: params[:page], per_page: 20) do query do boolean do must { string params[:query] } if params[:query].present? must { term :private, false } end end sort do by :date, "desc" by :amount, "asc" end end end def to_indexed_json to_json( include: { product: { only: [:name] } } ) end mapping do indexes :id, type: "integer" indexes :amount, type: "string", index: "not_analyzed" indexes :date, type: "date", index: "not_analyzed" indexes :private, type: "boolean" indexes :product do indexes :name, type: "string", analyzer: "custom_analyzer" end end settings analysis: { analyzer: { custom_analyzer: { tokenizer: [ "whitespace", "lowercase" ], filter: [ "ngram_filter", "word_delimiter_filter" ], type: "custom" } }, filter: { ngram_filter: { type: "nGram", min_gram: 2, max_gram: 15 } }, filter: { word_delimiter_filter: { type: "word_delimiter", generate_word_parts: true, generate_number_parts: true, preserve_original: true, stem_english_possessive: true } } } 

Does anyone have any suggestions or know how to set the query field to only use the product name?

Thanks.

+4
source share
2 answers

This was associated with a big headache, so I ended up switching to Ransack , which was much easier to understand and use.

0
source

When you do

  query { string params[:query] } 

and the request does not indicate the field, you are looking for the _all magic field. This field has its own analyzer settings - it will not use the one you set for your name field.

You can configure the _all field to use the analyzer, change the default analyzer, or modify the query to request the name field, in particular,

  query { string params[:query], :default_field => 'name'} 
+5
source

All Articles