gpt4 book ai didi

c# - ElasticSearch 5.x 上下文建议器 NEST .Net

转载 作者:行者123 更新时间:2023-11-30 19:23:59 28 4
gpt4 key购买 nike

我正在尝试在 ElasticSearch 5.1.2 上使用 Nest 5.0 创建一个带有上下文建议器的索引。

目前,我可以创建映射:

elasticClient.MapAsync<EO_CategoryAutocomplete>(m => m
.Properties(p => p
.Completion(c => c
.Contexts(ctx => ctx
.Category(csug => csug
.Name("lang")
.Path("l")
)
.Category(csug => csug
.Name("type")
.Path("t")
)
.Category(csug => csug
.Name("home")
.Path("h")
)
)
.Name(n => n.Suggest)
)
)
);

但在 POCO 类中,我不知道什么对象类型必须建议属性标记为 ??????:

public class EO_CategoryAutocomplete
{
public string Id { get; set; }
public ????? Suggest { get; set; }
}

public class EO_CategoryAC
{
public int Id { get; set; }
public string Name { get; set; }
}

在 NEST 5.0 中,CompletionField 属性已被删除(这是在 elasticsearch 2.X 上执行上下文建议的方法)

拜托,任何人都可以提供有关如何操作的示例吗?

文档都是关于查询的。 Suggester NEST

谢谢。

最佳答案

完成和上下文建议器能够返回 _source在 5.x+ 的响应中,因此不再需要负载。因此,NEST 5.x 中的类型现在是 CompletionField ,而不是 CompletionField<TPayload>在 NEST 2.x 中,TPayload是载荷类型。

这里有一个 NEST 5.x 的例子,可以让你启动并运行

public class EO_CategoryAutocomplete
{
public string Id { get; set; }
public IEnumerable<string> L { get; set; }
public CompletionField Suggest { get; set; }
}

void Main()
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool)
.DefaultIndex("autocomplete");

var client = new ElasticClient(connectionSettings);

if (client.IndexExists("autocomplete").Exists)
client.DeleteIndex("autocomplete");

client.CreateIndex("autocomplete", ci => ci
.Mappings(m => m
.Map<EO_CategoryAutocomplete>(mm => mm
.AutoMap()
.Properties(p => p
.Completion(c => c
.Contexts(ctx => ctx
.Category(csug => csug
.Name("lang")
.Path("l")
)
.Category(csug => csug
.Name("type")
.Path("t")
)
.Category(csug => csug
.Name("home")
.Path("h")
)
)
.Name(n => n.Suggest)
)
)
)
)
);

client.IndexMany(new[] {
new EO_CategoryAutocomplete
{
Id = "1",
Suggest = new CompletionField
{
Input = new [] { "async", "await" },
// explicitly pass a context for lang
Contexts = new Dictionary<string, IEnumerable<string>>
{
{ "lang", new [] { "c#" } }
}
}
},
new EO_CategoryAutocomplete
{
Id = "2",
Suggest = new CompletionField
{
Input = new [] { "async", "await" },
// explicitly pass a context for lang
Contexts = new Dictionary<string, IEnumerable<string>>
{
{ "lang", new [] { "javascript" } }
}
}
},
new EO_CategoryAutocomplete
{
Id = "3",
// let completion field mapping extract lang from the path l
L = new [] { "typescript" },
Suggest = new CompletionField
{
Input = new [] { "async", "await" },
}
}
}, "autocomplete");

client.Refresh("autocomplete");

var searchResponse = client.Search<EO_CategoryAutocomplete>(s => s
.Suggest(su => su
.Completion("categories", cs => cs
.Field(f => f.Suggest)
.Prefix("as")
.Contexts(co => co
.Context("lang",
cd => cd.Context("c#"),
cd => cd.Context("typescript"))
)
)
)
);

// do something with suggestions
var categorySuggestions = searchResponse.Suggest["categories"];
}

searchResponse返回

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"categories" : [
{
"text" : "as",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "async",
"_index" : "autocomplete",
"_type" : "eo_categoryautocomplete",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : "3",
"l" : [
"typescript"
],
"suggest" : {
"input" : [
"async",
"await"
]
}
},
"contexts" : {
"lang" : [
"typescript"
]
}
},
{
"text" : "async",
"_index" : "autocomplete",
"_type" : "eo_categoryautocomplete",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"suggest" : {
"input" : [
"async",
"await"
],
"contexts" : {
"lang" : [
"c#"
]
}
}
},
"contexts" : {
"lang" : [
"c#"
]
}
}
]
}
]
}
}

建议 ID 为 "1" 的文档和 "3" .您也可以使用 Source Filtering只返回您感兴趣的字段 _source .

关于c# - ElasticSearch 5.x 上下文建议器 NEST .Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41746660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com