Elasticsearch Suggester 是如何实现的?
How do implement Elasticsearch Suggester?
我正在尝试实现使用 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: Request failed to execute. Call: Status code 400 from: POST /candidates/_search?typed_keys=true. ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]"
使用 kibana 查看我的索引映射:
"suggest": {
"properties": {
"input": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
顺便说一句,我在 .NET Core 3.1 中使用最新版本的 Nest (7.9.0)
非常感谢你的帮助。
它看起来候选索引和建议字段可能在发出带有映射请求的创建索引时已经存在。您可以检查返回的 CreateIndexResponse
以查看是否属于这种情况; IsValid
属性 将是错误的,响应中的错误详细信息提供了原因。
Elasticsearch 的默认行为将创建一个索引和映射,如果一个索引请求进入将一个文档索引到它,根据它看到的第一个文档推断字段的映射。这对于许多用例都是有用的行为,但对于搜索用例,您通常希望在您执行操作时使用显式映射来控制索引的创建,因此要关闭此行为,您可以设置 action.auto_create_index
cluster setting to false
.。现在,如果您删除索引并使用您拥有的创建索引请求重新创建它,在您有机会显式创建它之前,它不会自动创建。
你说得对,映射定义是通过索引文档创建的,而不是根据我描述的映射定义创建的。
检查 CreateIndexResponse IsValid 属性 表明问题如您所料。
我花了一些时间找出根本问题:我使用的是 ES Docker 图像版本 6.3.0 和 Nest 客户端 7.*
让我的 ES 与我的 Nest 客户端兼容解决了这个问题。
感谢您的帮助。
有同样的问题,已通过显式映射解决:
_elasticClient.Indices.Create("candidates", c => c
.Map<CandidateDocument>(m => m
.Properties(ps => ps
.Completion(s => s
.Name(n => n.Suggest)))));
public class CandidateDocument
{
public Guid? Id { get; set; }
public CompletionField Suggest { get; set; }
}
我正在尝试实现使用 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: Request failed to execute. Call: Status code 400 from: POST /candidates/_search?typed_keys=true. ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]" CausedBy: "Type: illegal_argument_exception Reason: "no mapping found for field [suggest]"
使用 kibana 查看我的索引映射:
"suggest": {
"properties": {
"input": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
顺便说一句,我在 .NET Core 3.1 中使用最新版本的 Nest (7.9.0)
非常感谢你的帮助。
它看起来候选索引和建议字段可能在发出带有映射请求的创建索引时已经存在。您可以检查返回的 CreateIndexResponse
以查看是否属于这种情况; IsValid
属性 将是错误的,响应中的错误详细信息提供了原因。
Elasticsearch 的默认行为将创建一个索引和映射,如果一个索引请求进入将一个文档索引到它,根据它看到的第一个文档推断字段的映射。这对于许多用例都是有用的行为,但对于搜索用例,您通常希望在您执行操作时使用显式映射来控制索引的创建,因此要关闭此行为,您可以设置 action.auto_create_index
cluster setting to false
.。现在,如果您删除索引并使用您拥有的创建索引请求重新创建它,在您有机会显式创建它之前,它不会自动创建。
你说得对,映射定义是通过索引文档创建的,而不是根据我描述的映射定义创建的。 检查 CreateIndexResponse IsValid 属性 表明问题如您所料。
我花了一些时间找出根本问题:我使用的是 ES Docker 图像版本 6.3.0 和 Nest 客户端 7.* 让我的 ES 与我的 Nest 客户端兼容解决了这个问题。
感谢您的帮助。
有同样的问题,已通过显式映射解决:
_elasticClient.Indices.Create("candidates", c => c
.Map<CandidateDocument>(m => m
.Properties(ps => ps
.Completion(s => s
.Name(n => n.Suggest)))));
public class CandidateDocument
{
public Guid? Id { get; set; }
public CompletionField Suggest { get; set; }
}