Search code examples
ruby-on-railselasticsearchtire

Elastic search with Tire: Custom stopwords not working


I want to add a custom set of stopwords to one particular field in a model. So I added a custom analyzer for that field. But still when I search with the stopwords, results are showing up. The code inside my model is as follows:

settings :analysis => {
 :filter  => {
  :stop_filter => {
    :type        => "stop",
    :stopwords   => ["how", "when", "where", "who", "which", "what", "do", "the", "a", "is", "to"]
  }
 },
 :analyzer => {
  :my_analyzer => {
    :type         => "standard",
    :filter       => "stop_filter",
    :tokenizer    => "standard"
  }
 }
} do
 mapping do
  indexes :id,              :type => 'integer',     :index    => :not_analyzed
  indexes :sortable_id,     :type => 'integer',     :index    => :not_analyzed
  indexes :summary,         :type => 'string',      :analyzer => 'my_analyzer'
 end
end

def self.search_all(search_string = nil, options = {})
  tire.search(:load => true, :page => options[:page] || 1, :per_page => options[:per_page] || 10) do
    query {search_string.present? ? string(search_string) : all}
    filter :term, {:topics_list_filter =>  options[:topic_id]} if options[:topic_id]
    sort {by options[:sort], options[:order] || 'desc'} if options[:sort].present?
  end
end

I have also tried by giving stopwords as an option in analyzer, without creating the stop_filter. I am not sure where I am going wrong.


Solution

  • Your query is searching the _all field which is indexed using default analyzer. You can either replace the default field in your query with summary or replace the default analyzer. See How do I set the default analyzer for elastic search with tire? for more information.