简体   繁体   中英

Elasticsearch + Tire not ignore accents

I am trying everything to solve this issue.

I have a simple app rails with a search. The search is working fine when I search something with accents, but if I search the word without accents my results is empty.

I read the documentations of Tire and Elasticsearch, but I don't know what's happening

class Article < ActiveRecord::Base
  attr_accessible :description, :title, :user_id

  belongs_to :user

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

  mapping do
    indexes :_id, index: :not_analyzed
    indexes :title, analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball'
  end

  def self.search(params)
    tire.search(page: params[:page], per_page: 10) do
      query { string params[:q], default_operator: "AND" } if params[:q].present?
    end
  end
end

Bellow I tried to use asciifolding but it didn´t work.

  class Article < ActiveRecord::Base
  attr_accessible :description, :title, :user_id

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

  tire.settings :index => {
      :analysis => {
          :analyzer => {
              :index_analyzer => {
                  :tokenizer => "whitespace",
                  :filter => ["asciifolding", "lowercase", "snowball"]
              },
              :search_analyzer => {
                  :tokenizer => "whitespace",
                  :filter => ["asciifolding", "lowercase", "snowball"]
              }
          },
          :filter => {
              :snowball => {
                  :type => "snowball",
                  :language => "Portuguese"
              }
          }
      }
  }

  mapping do
    indexes :_id, index: :not_analyzed
    indexes :title, analyzer: 'snowball', boost: 100
    indexes :description, analyzer: 'snowball'
  end

  def self.search(params)
    tire.search(page: params[:page], per_page: 10) do
      query { string params[:q], default_operator: "AND" } if params[:q].present?
    end
  end
end

I am using Sense on Chrome to test, mapping and all config is OK!

What's happening???

Thanks

You have to use the analyzer you specify in your index. You are currently using the "snowball" analyzer for your title and description which does not perform asciifolding:

mapping do
  indexes :_id, index: :not_analyzed
  indexes :title, analyzer: 'snowball', boost: 100
  indexes :description, analyzer: 'snowball'
end

Do this instead

mapping do
  indexes :_id, index: :not_analyzed
  indexes :title, analyzer: :index_analyzer, boost: 100
  indexes :description, analyzer: :index_analyzer
end

Assuming you want the query_analyzer. Then when you want to search, use the other analyzer:

tire.search(page: params[:page], per_page: 10) do
  query { string params[:q], 
          analyzer: :search_analyzer, 
          default_operator: "AND" } if params[:q].present?
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM