com*_*aqt 5 .net elasticsearch nest
我正在尝试使用 NEST 客户端进行自动完成。
代码如下:
Poco(浓缩):
public class Course
{
[ElasticProperty(Name="id")]
public int ID { get; set; }
public string Name { get; set; }
[ElasticProperty(Type = FieldType.Completion)]
public CompletionField Suggest { get; set; }
public Course(sm.Models.Course c)
{
if (c != null)
{
this.ID = c.ID;
this.Name = c.Name;
this.Suggest = new CompletionField
{
Input = new List<string>(this.Name.Split(' ')) { this.Name },
Output = this.Name,
Payload = new
{
id = this.Name
},
Weight = 1
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
索引:
Client.CreateIndex("myindex", c => c
.NumberOfReplicas(1)
.NumberOfShards(5)
.Settings(s => s
.Add("merge.policy.merge_factor", "10")
.Add("search.slowlog.threshold.fetch.warn", "1s")
)
.AddMapping<Course>(m => m.MapFromAttributes()
.Properties(props => props
.Completion(s=>s
.Name(p=>p.Suggest)
.IndexAnalyzer("simple")
.SearchAnalyzer("simple")
.MaxInputLength(20)
.Payloads()
.PreservePositionIncrements()
.PreserveSeparators()
)
)
));
Run Code Online (Sandbox Code Playgroud)
我的建议查询:
GET _suggest
{
"course-suggest": {
"text": "Nothilfe",
"completion": {
"field": "suggest",
"size": 10
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致此错误:
"failures": [
{
"shard": 1,
"index": "myindex",
"status": "INTERNAL_SERVER_ERROR",
"reason": {
"type": "exception",
"reason": "failed to execute suggest",
"caused_by": {
"type": "exception",
"reason": "Field [suggest] is not a completion suggest field"
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我的建议字段未被识别为完成字段?
获取_映射/课程
"suggest": {
"properties": {
"input": {
"type": "string"
},
"output": {
"type": "string"
},
"payload": {
"properties": {
"id": {
"type": "string"
}
}
},
"weight": {
"type": "long"
}
}
Run Code Online (Sandbox Code Playgroud)
您收到此错误的可能原因有多种,但最明显的原因是索引中类型的映射相course对于myindex您发送的查询而言已经过时。
您可以轻松检查类型的映射
curl -XGET "http://localhost:9200/myindex/course/_mapping"
Run Code Online (Sandbox Code Playgroud)
它应该看起来像
{
"myindex": {
"mappings": {
"course": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 20
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有,您需要解决此问题,方法是删除索引中的类型并重新创建(或完全删除索引并重新创建),或者使用正确的映射创建新索引并从旧索引复制文档如果您想*尝试*保留您已有的任何数据(这在这里可能可行,也可能不可能)。
以下将按预期创建索引并执行suggest您尝试运行的查询。请注意,我已将查询范围限定为仅针对索引运行myindex。类型
public class Course
{
[ElasticProperty(Name = "id")]
public int ID { get; set; }
public string Name { get; set; }
[ElasticProperty(Type = FieldType.Completion)]
public CompletionField Suggest { get; set; }
public Course(Course c)
{
if (c != null)
{
this.ID = c.ID;
this.Name = c.Name;
this.Suggest = new CompletionField
{
Input = new List<string>(this.Name.Split(' ')) { this.Name },
Output = this.Name,
Payload = new
{
id = this.Name
},
Weight = 1
};
}
}
}
// I'm guessing CompletionField looks something like this?
public class CompletionField
{
public List<string> Input { get; set; }
public string Output { get; set; }
public object Payload { get; set; }
public double Weight { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及索引和查询的创建
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.ExposeRawResponse(true)
.SetConnectionStatusHandler(response =>
{
// log out the requests
if (response.Request != null)
{
Console.WriteLine("{0} {1} \n{2}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl,
Encoding.UTF8.GetString(response.Request));
}
else
{
Console.WriteLine("{0} {1}\n", response.RequestMethod.ToUpperInvariant(), response.RequestUrl);
}
if (response.ResponseRaw != null)
{
Console.WriteLine("{0}\n{1}\n\n{2}\n", response.HttpStatusCode, Encoding.UTF8.GetString(response.ResponseRaw), new String('-', 30));
}
else
{
Console.WriteLine("{0}\n\n{1}\n", response.HttpStatusCode, new String('-', 30));
}
});
var client = new ElasticClient(settings);
var indexResponse = client.CreateIndex("myindex", c => c
.NumberOfReplicas(1)
.NumberOfShards(5)
.Settings(s => s
.Add("merge.policy.merge_factor", "10")
.Add("search.slowlog.threshold.fetch.warn", "1s")
)
.AddMapping<Course>(m => m.MapFromAttributes()
.Properties(props => props
.Completion(s => s
.Name(p => p.Suggest)
.IndexAnalyzer("simple")
.SearchAnalyzer("simple")
.MaxInputLength(20)
.Payloads()
.PreservePositionIncrements()
.PreserveSeparators()
)
)
));
// give Elasticsearch some time to initialize the index
Thread.Sleep(TimeSpan.FromSeconds(5));
var suggestResponse = client.Suggest<Course>(s => s
.Index("myindex")
.Completion("course-suggest", c => c
.Text("Nothilfe")
.OnField("suggest")
.Size(10)
)
);
// do something with suggestResponse
}
Run Code Online (Sandbox Code Playgroud)
这会将以下内容记录到控制台
POST http://localhost:9200/myindex
{
"settings": {
"index": {
"number_of_replicas": 1,
"number_of_shards": 5,
"merge.policy.merge_factor": "10",
"search.slowlog.threshold.fetch.warn": "1s"
}
},
"mappings": {
"course": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"suggest": {
"type": "completion",
"search_analyzer": "simple",
"index_analyzer": "simple",
"payloads": true,
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_len": 20
}
}
}
}
}
200
{"acknowledged":true}
------------------------------
POST http://localhost:9200/myindex/_suggest
{
"course-suggest": {
"text": "Nothilfe",
"completion": {
"field": "suggest",
"size": 10
}
}
}
200
{"_shards":{"total":5,"successful":5,"failed":0}}
------------------------------
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4997 次 |
| 最近记录: |