[英]tire and elasticsearch_autocomplete accents and facets
我正在使用elasticsearch_autocomplete gem作为自动完成功能。
我对特殊字符ñ
和重音áéíóú
。
模型:
class Car
ac_field :name, :description, :city, :skip_settings => true
def self.ac_search(params, options={})
tire.search load: true, page: params[:page], per_page: 9 do
query do
boolean do
must { string params[:query], default_operator: "AND" } if params[:query].present?
must { term :city, params[:city] } if params[:city].present?
end
end
filter :term, city: params[:city] if params[:city].present?
facet "city" do
terms :city
end
end
end
end
此版本可以正常使用特殊字符。
例如:用Martin
查询我得到Martín, martín, martin, Martin
所有结果
使用这种方法会出现问题:
现在,结果就是单个单词。 例如,标记为[“ San Francisco”,“ Madrid”]的城市最终将具有三个单独的标签。 同样,如果我执行查询以搜索“旧金山”(必须{术语“ city”,params [:city]}),则将失败,而对“ San”或“ Francisco”的查询将成功。 此处所需的行为是标签应为原子标签,因此只有“旧金山”(或“马德里”)标签搜索才能成功。
为了解决这个问题,我创建了自定义映射:
model = self
settings ElasticsearchAutocomplete::Analyzers::AC_BASE do
mapping _source: {enabled: true, includes: %w(name description city)} do
indexes :name, model.ac_index_config(:name)
indexes :description, model.ac_index_config(:description)
indexes :city, :type => 'string', :index => :not_analyzed
end
end
通过此映射,解决了多字问题,并且现在具有city
字段的构面可以正常工作:
而不是让字体变成San
Francisco
和San
Francisco
现在我得到San Francisco
现在的问题是,在模型内部使用此映射时,搜索无法找到带有特殊字符的结果
例如:用Martin
查询我只用Martin martin
得到结果
我正在使用蒙古式代替活动记录。
我该如何解决这个问题? 我认为问题出在asciifolding tokenfilter上 。
我用以下方法解决了这个问题:
class User
include Mongoid::Document
field :city, :type => String
has_one: car
end
class Car
ac_field :name, :description, :user_city, :skip_settings => true
def self.ac_search(params, options={})
tire.search load: true, page: params[:page], per_page: 9 do
query do
boolean do
must { term :user_city, params[:user_city] } if params[:user_city].present?
end
end
facet "cities" do
terms :user_city
end
end
end
model = self
settings ElasticsearchAutocomplete::Analyzers::AC_BASE do
mapping _source: {enabled: true, includes: %w(car_city name description)} do
indexes :car_city, :type => 'string', :index => :not_analyzed
end
end
def to_indexed_json
to_json(methods: [:user_city])
end
def user_city
user.city
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.