我的用例:当用户可以键入他的查询时,我有一个搜索栏。除了常规查询建议之外,我还想向用户显示多种类型的搜索建议。例如,在下面的屏幕快照中(如您在此屏幕快照中所见),您可以看到有关公司部门,公司和学校的建议。
当前使用completion suggesters和以下映射来实现(这是我们Ruby实现中的代码,但我相信您应该能够轻松理解它)
{
_source: '',suggest: {
text: query_from_the_user,# User query like "sec" to find "security" related matches
'school_names': {
completion: {
field: 'school_names_suggest',},'companies': {
completion: {
field: 'company_name.suggest','sectors': {
completion: {
field: sector_field_based_on_current_language(I18n.locale),# uses 'company_sector.french.suggest' when the user browses in french
},}
这是我的映射(这是用Ruby编写的,但我认为从心理上将其转换为Elasticsearch JSON配置应该不太困难
indexes :company_name,type: 'text' do
indexes :suggest,type: 'completion'
end
indexes :company_sector,type: 'object' do
indexes :french,type: 'text' do
indexes :suggest,type: 'completion'
end
indexes :english,type: 'completion'
end
end
indexes :school_names_suggest,type: 'completion'
# sample Indexed JSON
{
company_name: "Christian Dior Couture",company_sector: {
english: 'Milk sector',french: 'Secteur laitier'
},school_names_suggest: ['Télécom ParisTech','Ecole Centrale Paris']
}
问题在于建议不够强大,无法基于句子的中间部分自动完成,即使完美匹配也无法提供其他结果。这是我在ES实施中需要捕捉的一些场景
案例1-在句子中间按前缀匹配
# documents
[{ company_name: "Christian Dior Couture" }]
# => A search term "Dior" should return this document because it matches by prefix on the second word
案例2-即使完美匹配也能提供结果
# documents
[
{ company_name: "Crédit Agricole" },{ company_name: "Crédit Agricole Pyrénées Gascogne" },]
# => A search term "Crédit Agricole" should return both documents (using the current implementation it only returns "Crédit Agricole"
我可以在Elasticsearch中使用建议程序实现此功能吗?还是我需要回退到多个search
,以使用in the doc中提到的search-as-you-type
来利用新的query
数据类型?
我正在AWS和Ruby驱动程序(gem elasticsearch-7.3.0
)上使用elasticsearch 7.1