gpt4 book ai didi

c# - C#Nest Elasticsearch-使用上下文描述符按 bool 字段过滤完成建议

转载 作者:行者123 更新时间:2023-12-02 23:14:10 25 4
gpt4 key购买 nike

我正在使用完成建议者来提供搜索建议作为用户类型。我在C#类中添加了IsActive属性,但无法弄清楚如何在.Suggest.Completion查询中使用该属性。我知道它与上下文有关,但是我找不到Elasticsearch / Nest 6.8的任何示例。

如何更新映射和查询以防止建议使用IsActive = false的文档?

这是我的后备类:

[ElasticsearchType(
IdProperty = "search"
)]
public class SearchCompletion
{
public string search { get; set; }


/// <summary>
/// Use this field for aggregations and sorts
/// </summary>
[Keyword]
public string search_keyword { get; set; }

public bool isActive { get; set; }

/// <summary>
/// To use for sorting results when searching SearchCompletions
/// directly since you can't sort by the Completionfield.Weight
/// property for some reason
/// </summary>
public int weight { get; set; }

public CompletionField suggest { get; set; }
}

这是我的映射方法:
public static void MapSearchCompletions(ElasticClient client, string index)
{
var mapResponse = client.Map<SearchCompletion>(m => m
.Index(index)
.AutoMap()


// WHAT GOES HERE??
.Properties(props => props
.Completion(c => c
.Name(n => n.isActive)
.Contexts(context => context
.Category(cat => cat.Name("isActive"))
)
)
)
); //re-apply the index mapping
}

这是我的查询
var response = client.Search<SearchCompletion>(s => s
.Index(suggest_index)
.Suggest(su => su
.Completion("search", cs => cs
.Field(f => f.suggest)
.Contexts(con => con.Context("")) // WHAT GOES HERE??
.Prefix(search)
.Size(size)
)
)
);

最佳答案

我认为您不能将boolean用作category值。我没有在ES中尝试过,但NEST正在接受category类型的字符串。因此,我建议您更新映射。
(我将其更新为string以继续)
好像您在其他地方创建索引。您的代码只是创建映射。可能是:

        var mapResponse = client.Map<SearchCompletion>(m => m
.Index("index")
.AutoMap()
.Properties(props => props
.Completion(c => c
.Name(n => n.suggest)
.Contexts(context => context
.Category(cat => cat.Name("isActive"))
)
)
)
);

// let's create data
var r1 = client.Index(new SearchCompletion
{
search = "plate", suggest = new CompletionField
{
Input = new[] {"plate", "dish"}, // this one has two suggestions
Contexts = new Dictionary<string, IEnumerable<string>>
{
{
"isActive",
new[] {"yes"} // isActive is 'yes'
}
}
}
}, d => d.Index("index"));

var r2 = client.Index(new SearchCompletion
{
search = "fork",
suggest = new CompletionField
{
Input = new[] { "fork", "dis_example" }, // this has two suggestions
Contexts = new Dictionary<string, IEnumerable<string>>
{
{
"isActive",
new[] {"no"} // isActive is 'no'
}
}
}
}, d => d.Index("index"));

var r3 = client.Index(new SearchCompletion
{
search = "spoon",
suggest = new CompletionField
{
Input = new[] { "spoon" },
Contexts = new Dictionary<string, IEnumerable<string>>
{
{
"isActive",
new[] {"yes"} // isActive is 'yes'
}
}
}
}, d => d.Index("index"));

// Let's search for the dish using 'dis' and limiting it to IsActive == 'yes'
var searchResponse = client.Search<SearchCompletion>(s => s.Index("index").Suggest(su => su
.Completion("search", cs => cs
.Field(f => f.suggest)
.Contexts(con => con.Context("isActive", aa => aa.Context("yes")))
.Prefix("dis")
.Size(10)
)
));

确实,我们得到了板块建议:
{
...
"suggest" : {
"search" : [
{
"text" : "dis",
"offset" : 0,
"length" : 3,
"options" : [
{
"text" : "dish",
"_index" : "testindex",
"_type" : "searchcompletion",
"_id" : "plate",
"_score" : 1.0,
"_source" : {
"search" : "plate",
"weight" : 0,
"suggest" : {
"input" : [
"plate",
"dish"
],
"contexts" : {
"isActive" : [
"yes"
]
}
}
},
"contexts" : {
"isActive" : [
"yes"
]
}
}
]
}
]
}
}

关于c# - C#Nest Elasticsearch-使用上下文描述符按 bool 字段过滤完成建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261942/

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