ElasticSearch and Tire : how to use html_strip

霸气de小男生 提交于 2019-12-24 13:30:35

问题


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 :)


回答1:


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



来源:https://stackoverflow.com/questions/25585971/elasticsearch-and-tire-how-to-use-html-strip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!