gpt4 book ai didi

c# - NEST 按文本字段排序

转载 作者:行者123 更新时间:2023-11-30 21:37:42 25 4
gpt4 key购买 nike

我正在尝试编写一个使用 NEST 并将搜索功能保留在其自己的有界上下文中的搜索模块。

为此,我进行了以下用户配置文件搜索:

public class UserProfileSearch : IUserProfileSearch
{
...

public async Task<PagedItems<UserProfileModel>> FindAsync(string searchTerm, int skip, int take)
{
var client = _elasticClientProvider.GetClient();
var response = await client.SearchAsync<ElasticUserProfileModel>(s => s
.Index(_elasticConfiguration.GetIndex())
.From(skip).Size(take)
.Query(q => q.MultiMatch(m => m.Fields(f => f
.Field(u => u.Email)
.Field(u => u.FirstName)
.Field(u => u.LastName))
.Query(searchTerm)))
.Sort(q => q.Ascending(u => u.Email)));
var count = await client.CountAsync<ElasticUserProfileModel>(s => s.Index(_elasticConfiguration.GetIndex()));
return new PagedItems<UserProfileModel> { Items = response.Documents.Cast<UserProfileModel>().ToArray(), Total = count.Count };
}
}

响应与此报告一致失败:

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"xxxx","node":"xxxx","reason":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}]},"status":400}

然而,我按照报告中的建议做了,但同样的错误不断发生。我已经定义了

public class ElasticUserProfileModel : UserProfileModel
{
[Text(Fielddata = true)] public override string Email { get => base.Email; set => base.Email = value; }
}

这应该正是报告所要求的。我在每次端到端测试期间使用 ElasticUserProfileModel 重建索引。

我也尝试过使用 Keyword 属性而不是 Text 属性,但这会产生完全相同的错误。

如果我按 Id(数字)而不是 Email 排序,则不会出现错误。但这是一个非常有用的搜索。

有没有简单的方法来解决这个问题?

最佳答案

基于 https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/multi-fields.html 处的文档,我发现 POCO 字符串会自动映射到关键字和文本字段。 Suffix() 扩展方法是启用字符串排序所需的全部。

我删除了ElasticUserProfileModel派生类,FindAsync()方法变成了

public async Task<PagedItems<UserProfileModel>> FindAsync(string searchTerm, int skip, int take)
{
var client = _elasticClientProvider.GetClient();
var response = await client.SearchAsync<UserProfileModel>(s => s
.Index(_elasticConfiguration.GetIndex())
.From(skip).Size(take)
.Query(q => q.MultiMatch(m => m.Fields(f => f
.Field(u => u.Email)
.Field(u => u.FirstName)
.Field(u => u.LastName))
.Query(searchTerm)))
.Sort(q => q.Ascending(u => u.Email.Suffix("keyword"))));
var count = await client.CountAsync<UserProfileModel>(s => s.Index(_elasticConfiguration.GetIndex()));
return new PagedItems<UserProfileModel> { Items = response.Documents.ToArray(), Total = count.Count };
}

解决了问题。

关于c# - NEST 按文本字段排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979724/

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