【问题标题】:Elasticsearch + Tire not ignore accentsElasticsearch + Tire 不会忽略重音符号
【发布时间】:2014-02-07 06:17:56
【问题描述】:

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

我有一个带有搜索功能的简单应用程序轨道。当我搜索带有重音的内容时,搜索工作正常,但如果我搜索不带重音的单词,我的结果是空的。

我阅读了 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 进行测试、映射和所有配置都OK!

发生了什么事???

谢谢

【问题讨论】:

    标签: ruby-on-rails elasticsearch tire


    【解决方案1】:

    您必须使用您在索引中指定的分析器。您目前正在为您的标题和描述使用“雪球”分析器,它不执行 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 2020-12-09
      • 2013-08-28
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多