Elasticsearch + Tire不会忽略重音

时间:2013-09-02 23:33:57

标签: ruby-on-rails elasticsearch tire

我正在努力解决这个问题。

我有一个简单的app rails搜索。当我使用重音搜索某些内容时,搜索工作正常,但如果我搜索没有重音符号的单词,则结果为空。

我阅读了Tire和Elasticsearch的文档,但我不知道发生了什么

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

贝娄我尝试使用asciifolding,但它不起作用。

  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

我在Chrome上使用Sense进行测试,映射和所有配置都没问题!

发生了什么事?

由于

1 个答案:

答案 0 :(得分:0)

您必须使用在索引中指定的分析器。您目前正在使用“雪球”分析器作为您的标题和描述,但不执行asciifolding:

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

改为

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

假设您需要query_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