Search code examples
ruby-on-railselasticsearchtire

ElasticSearch and Tire : how to use html_strip


I'd like to add the char_filter html_strip in my Rails Project. But I'm not sure how and where to do it with the (re)tire gem. I'm not even sure it's possible the way I'm doing it.

My code for the moment :

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
   indexes :author, type: 'string'
   indexes :content, type: 'string', index_options: "offsets", analyzer: 'snowball', language: "French", char_filter: 'html_strip'
   indexes :name, type: 'string', index_options: "offsets", analyzer: 'snowball', language: "French", :boost => 5
   indexes :topic_id, type: :integer, :index => :not_analyzed
  end

  def self.search params
    query = params[:query]

    tire.search do
      query do
        boolean minimum_number_should_match: 1 do
          should { string query, fields: [:name], default_operator: "AND", analyzer: 'snowball' }
          should { string query, fields: [:content], default_operator: "AND", analyzer: 'snowball' }
          should { string query, fields: [:author], default_operator: "AND" }
          must { range :topic_id, gt: 0 }
        end
      end

      highlight :content, :author, :name, options: {pre_tags: ['<em style="background-color: yellow">'], post_tags: ['</em>'], :number_of_fragments => 50}
    end
  end

I'm not really sure how to implement it. I tried many things but with no results for now!

Thanks!

EDIT

I changed my code, following IS04's answer, to that :

settings analysis: {
      analyzer: {
          html_analyzer: {
              type: 'custom',
              tokenizer: 'standard',
              filter: ['classic'],
              char_filter: ['html_strip']
          }
      }
  } do
    mapping do
      indexes :author, type: 'string'
      indexes :content, type: 'string', index_options: "offsets", analyzer: 'html_analyzer', search_analyzer: 'snowball', language: "French"
      indexes :name, type: 'string', index_options: "offsets", search_analyzer: 'snowball', language: "French", boost: 5
      indexes :topic_id, type: :integer, index: :not_analyzed
    end
  end

I post it here, if it helps somebody someday :)


Solution

  • you could try something like:

    settings analysis: {               
        analyzer: {                      
          some_custom_analyzer: {
            type: 'custom',
            tokenizer: 'standard',       
            filter: ['classic'],         
            char_filter: ['html_strip']  
          }
        }
      }
    

    then:

    indexes :content, type: 'string', index_options: "offsets", analyzer: 'snowball', search_analyzer: 'some_custom_analyzer'
    

    analysis, analyzer, tokenizers, filters, char filters