【发布时间】:2020-09-10 11:36:35
【问题描述】:
我正在尝试实现一个使用 Elastic Search 中的建议功能的示例代码。
索引文档由平面 POCO 组成
public class CandidateDocument
{
public Guid Id { get; set; }
public string Name { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public CompletionField Suggest { get; set; }
}
映射如下
var client = new ElasticClient(settings);
client.Indices.Create("candidates", c =>
c.Map<CandidateDocument>(m =>
m.Properties(ps => ps.Text(s => s.Name(n => n.Name)
.Store(false)
.Fields(f => f.Keyword(k => k.Name("nameRaw"))))
.Text(s => s.Name(n => n.FirstName)
.Store(false)
.Fields(f => f.Keyword(k => k.Name("firstNameRaw"))
.Date(s => s.Name(n => n.BirthDate).Format("ddMMyyyy"))
.Keyword(s => s.Name(n => n.Id))))
.Completion(c => c.Name(n => n.Suggest)))));
索引文档如下所示:
var candidateDocument = new CandidateDocument
{
Id = Guid.NewGuid(),
Name = "Lennon",
FirstName = "John",
BirthDate = DateTime.Now,
Suggest = new CompletionField
{
Input = new[] { "Lennon" },
}
};
var indexResponse = await this.elasticClient.IndexAsync(candidateDocument, i => i.Index("candidates"));
我的查询是:
var searchResponse = await this.elasticClient.SearchAsync<CandidateDocument>(s => s.Index("candidates").Suggest(su => su
.Completion("suggestions", c => c
.Field(f => f.Suggest)
.Prefix(query)
.Fuzzy(f => f.Fuzziness(Fuzziness.Auto))
.Size(5))));
我面临以下问题:
Elasticsearch.Net.ElasticsearchClientException:请求执行失败。呼叫:状态代码 400 来自:POST /candidates/_search?typed_keys=true。 ServerError:类型:search_phase_execution_exception 原因:“所有分片失败” CausedBy:“类型:非法参数异常原因:“未找到字段 [建议] 的映射” CausedBy:“类型:非法参数异常原因:“未找到字段 [建议] 的映射”
查看我使用 kibana 的索引映射:
"suggest": {
"properties": {
"input": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
顺便说一句,我在 .NET Core 3.1 中使用的是最新版本的 Nest (7.9.0)
感谢您的帮助。
【问题讨论】:
标签: c# asp.net-core elasticsearch nest