[英]Elasticsearch-rails completion suggester, map data, create indexes, suggest method
I am using elasticsearch-model
and elasticsearch-rails
gems. 我正在使用elasticsearch-model
和elasticsearch-rails
宝石。
In my Profile model I have 在我的个人资料模型中
settings index: { number_of_shards: 3 } do
mappings dynamic: 'false' do
indexes :profile_type, analyzer: 'keyword'
indexes :profile do |p|
indexes :name, type: 'multi_field' do
indexes :name, analyzer: 'snowball'
indexes :tokenized, analyzer: 'keyword'
end
end
end
end
I tried adding suggest
field 我尝试添加suggest
字段
settings index: { number_of_shards: 3 } do
mappings dynamic: 'false' do
indexes :profile_type, analyzer: 'keyword'
indexes :suggest, type: 'suggest' # not sure about this
# do not really know how to index this field
indexes :profile do |p|
indexes :name, type: 'multi_field' do
indexes :name, analyzer: 'snowball'
indexes :tokenized, analyzer: 'keyword'
end
end
end
end
I am trying to make suggest on profile.name
, I have added as_indexed_json
我想在profile.name
上提出建议,我添加了as_indexed_json
def as_indexed_json(options={})
suggest= {
suggest: {
:input => profile.name,
:output => profile.name,
:payload => {
name: profile.name
}
}
}
self.as_json(
:only => [:profile_type, :slug],
:include => {
:profile => {}
}
).merge(suggest)
end
Any help would be appreciated, any suggestion. 任何帮助将不胜感激,任何建议。
I figured out how to map data, using elasticsearch-rails
gem 我想出了如何使用elasticsearch-rails
gem映射数据
settings index: { number_of_shards: 3 } do
mappings dynamic: 'false' do
indexes :profile_type, analyzer: 'keyword'
indexes :suggest, type: 'completion', index_analyzer: 'simple', search_analyzer: 'simple', payloads: true
# will do the trick
indexes :profile do |p|
indexes :name, type: 'multi_field' do
indexes :name, analyzer: 'snowball'
indexes :tokenized, analyzer: 'keyword'
end
end
end
end
Then to create indexes 然后创建索引
def as_indexed_json(options={})
suggest= {
suggest: {
:input => profile.name,
:output => profile.name,
:payload => {
name: profile.name
}
}
}
self.as_json(
:only => [:profile_type, :slug],
:include => {
:profile => {}
}
).merge(suggest)
end
Then do not use search
method, instead use suggest
method 然后不要使用search
方法,而是使用suggest
方法
def self.suggest query
__elasticsearch__.client.suggest(:index => index_name, :body => {
:suggestions => {
:text => query,
:completion => {
:field => 'suggest',
:size => 20
}
}
})
end
This way you get complete suggestion
这样您就可以得到complete suggestion
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.