NEST(ElasticSearch)将突出显示与文档匹配 [英] NEST (ElasticSearch) matching Highlights to documents

查看:76
本文介绍了NEST(ElasticSearch)将突出显示与文档匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#NEST与ElasticSearch结合使用.我能够查询产品的索引,并在其 Name CategoryName 字段中查找匹配项.我还可以使用 Highlights 扩展查询.

I'm using C# NEST with ElasticSearch. I'm able to query an index of Products and look in their Name and CategoryName fields for matches. I can also extend the query using Highlights.

现在,在我的 IQueryResponse 响应中,我有两个集合:(1) .Documents 和(2) .Highlights .

Now in my IQueryResponse response I have two collections: (1) .Documents and (2) .Highlights.

例如:考虑搜索:具有3个文档结果的猫":

e.g.: Consider the search for: "cat" which has 3 document results:


{
   { Name: "Cat product", CategoryName: "Category1" },
   { Name: "Some product", CategoryName: "Category2" },
   { Name: "Some product2", CategoryName: "Category3" }
}

但是现在我有4个突出结果:

But now I have 4 highlight results:


{
   { Field: "name", Highlights: ['"<u>Cat</u> product"'] },
   { Field: "categoryName", Highlights: ['"<u>Cat</u>egory1"'] },
   { Field: "categoryName", Highlights: ['"<u>Cat</u>egory2"'] },
   { Field: "categoryName", Highlights: ['"<u>Cat</u>egory3"'] }
}

它们似乎毫无关联.我怎么知道哪个 Highlight 项属于哪个 Document 项?

They seem to be in no way related to each other. How do I know which Highlight item belongs to which Document item?

推荐答案

IQueryResponse 还公开了 .IEnumerable< IHit< T>> 其中T是您的文档类型

IQueryResponse also exposes .DocumentsWithMetaData of type IEnumerable<IHit<T>> where T is the type of your document

这基本上是结果的展开视图,因为elasticsearch IHit< T> 的返回具有许多有用的属性,例如 Highlights .

This is basically the unwrapped view of the results as return by elasticsearch IHit<T> has many useful properties such as the Highlights.

我已将DocumentId结果添加到突出显示类 Highlight 中,这样,无论您如何使用突出显示,都可以轻松地将其与匹配匹配.

I've added a DocumentId result to the highlight class Highlight so that no matter how you get to the highlight you can relate it back easily to the hit.

因此,现在使用 .DocumentsWithMetaData ,下一个版本将为突出显示内容提供更具逻辑性的API.

So use .DocumentsWithMetaData for now, the next release will have a more logical API for highlights.

这篇关于NEST(ElasticSearch)将突出显示与文档匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆