C# 按文本字段嵌套排序
我正在尝试编写一个搜索模块,该模块使用NEST并将搜索功能保持在其自身的有界上下文中 为了实现这一点,我进行了以下用户配置文件搜索:C# 按文本字段嵌套排序,c#,elasticsearch,nest,C#,elasticsearch,Nest,我正在尝试编写一个搜索模块,该模块使用NEST并将搜索功能保持在其自身的有界上下文中 为了实现这一点,我进行了以下用户配置文件搜索: public class UserProfileSearch : IUserProfileSearch { ... public async Task<PagedItems<UserProfileModel>> FindAsync(string searchTerm, int skip, int take) {
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 };
}
}
这正是报告所要求的。在每次端到端测试期间,我都使用ElasticUserProfileModel
重建索引
我也尝试过使用关键字
属性而不是文本
属性,但这产生了完全相同的错误
如果我按Id
(数字)而不是电子邮件进行排序,则不会出现错误。但这是一个明显不太有用的搜索
有没有一种简单的方法可以解决这个问题?根据上的文档,我发现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 };
}
public async Task FindAsync(字符串searchTerm、int skip、int take)
{
var client=_elasticClientProvider.GetClient();
var response=wait client.SearchAsync(s=>s
.Index(_elasticConfiguration.GetIndex())
.从(跳过).大小(取)
.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.升序(u=>u.Email.Suffix(“关键字”)));
var count=await client.CountAsync(s=>s.Index(_elasticConfiguration.GetIndex());
返回新的PagedItems{Items=response.Documents.ToArray(),Total=count.count};
}
这解决了问题。映射是什么样子的?映射是否按预期拾取重写属性上的属性?@RussCam——是的。描述符将电子邮件属性显示为FieldData设置为true。
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 };
}