我正在尝试创建自动填充全文seacrh,在传入搜索字词时,我需要搜索所有字段并突出显示匹配的文本。
搜索描述符
var searchDescriptor = new SearchDescriptor<EmployeeSearchDocument>().Index("employees").Size(15)
.Query(a => a.Bool(b => b.Must(m => m.QueryString(qs => qs.DefaultField("*").Query(q))))).Highlight(
h => h.PreTags("<em'>").PostTags("</em>").Fields(
f => f.Field(d => d.EmployeeId),
f => f.Field(d => d.EmpNumber),
f => f.Field(d => d.PreferredName),
f => f.Field(d => d.FirstName),
f => f.Field(d => d.MiddleName),
f => f.Field(d => d.LastName),
f => f.Field(d => d.TerminationDate),
f => f.Field(d => d.CompanyName),
f => f.Field(d => d.CompanyGroupId),
f => f.Field(d => d.IdNumber),
f => f.Field(d => d.Description),
f => f.Field(d => d.JobNumber)));
return await this.client.SearchAsync<EmployeeSearchDocument>(searchDescriptor);
Highlight
部分中的字段是我模型中的所有值。
我一直在查看文档,有很多方法可以构建和查询。这是正确的做法吗?
qs.DefaultField("*")
似乎有点奇怪。我应该指定所有字段吗?
更新 好吧,经过一段时间后,我重构了代码看起来像这样 我仍然得到一些奇怪的结果,我发现这是因为该领域的MultiMatch不能像我预期的那样工作。似乎忽略除了最后一个字段 描述 之外的所有字段,因此搜索仅适用于最后一个字段。肯定是不对的
var response = await this.client.SearchAsync<EmployeeDocument>(
x => x.Index("employees").Type("employee")
.Query(
q => q.MultiMatch(
mp => mp.Query(query).Fields(
f => f.Field(f1 => f1.EmployeeId).Field(f2 => f2.FirstName)
.Field(f3 => f3.LastName).Field(f4 => f4.Description))))
.From(page - 1).Size(pageSize).Highlight(
h => h.PreTags("<em>").PostTags("</em>").Fields(
f => f.Field(p => p.EmployeeId),
f => f.Field(p => p.FirstName),
f => f.Field(p => p.LastName),
f => f.Field(p => p.Description))));