我是弹性搜索的完全初学者,我一直在尝试使用elasticsearch的完成建议,使用Nest来自动完成属性 .
这是我的映射(如上所述here :):
var createResult = client.CreateIndex(indexName, index => index
.AddMapping<Contact>(tmd => tmd
.Properties(props => props
.Completion(s =>
s.Name(p => p.CompanyName.Suffix("completion"))
.IndexAnalyzer("standard")
.SearchAnalyzer("standard")
.MaxInputLength(20)
.Payloads()
.PreservePositionIncrements()
.PreserveSeparators())
)
)
);
var resultPerson = client.IndexMany(documents.OfType<Person>(), new SimpleBulkParameters { Refresh = true });
var resultCompany = client.IndexMany(documents.OfType<Company>(), new SimpleBulkParameters { Refresh = true });
索引时,我只是使用 IndexMany
方法并传递 IEnumberable<Contact>
(Contact有一个名称为CompanyName的属性,Contact是一个抽象类,Person和Company都是它的具体实现) . 搜索引发了一个例外 ElasticSearchException[Field [companyName] is not a completion suggest field] . 查询如下所示:
SearchDescriptor<Contact> descriptor = new SearchDescriptor<Contact>();
descriptor = descriptor.SuggestCompletion("suggest", c => c.OnField(f => f.CompanyName).Text(q));
var result = getElasticClientInstance("contacts").Search<Contact>(body => descriptor);
string qe = result.ConnectionStatus.ToString();
我在这里做错了什么,我在 SuggestCompletion
上查看了Nest的测试,但没有太多帮助,这意味着测试只描述了如何获得建议而不是如何设置SuggestCompletion的索引映射 .
我也尝试设置 edgeNgram
tokenizer,如post中所述,但是,也无法继续进行 .
关于如何进行的任何方向或示例将极大地帮助 .
1 回答
UPDATE
您尝试创建名为"companyName.completion"的属性,但在该位置它无效,它将使用最后一个令牌"completion" . 所以它实际上映射了一个名为completion的字段....尝试将调用更改为:
.Name(p => p.CompanyName)
Other observations
您为
Contact
指定了映射,但在索引时使用了Person
和Company
类型 .在elasticsearch术语中,您映射:
/index/contact/
但是你的文件将进入:
/index/person/
和/index/company
NEST不会自动映射特定类的所有实现,而elasticsearch无法知道这三者是否相关 .
我会将映射重构为一个方法,并为所涉及的所有类型调用它 .
该方法返回描述符,以便您可以进一步链接它 .
然后,当您搜索联系人时:
该类型提示将导致搜索查找
/index/person
和/index/company
,并将知道如何返回协变的文档列表 .所以你可以在上次通话后做
result.Documents.OfType<Person>()
.