从弹性搜索结果检索数据 [英] Retrieve data from elasticsearch results
问题描述
这是我的代码到目前为止:
public string GetByOpinionDocumentTextElastic(string queryString)
{
var settings = new ConnectionSettings(new Uri(ConfigurationManager.AppSettings [ ElasticSearchAddress ]))SetDefaultIndex( 判例法);
var client = new ElasticClient(settings);
var result = client.Search< OpinionDocumentIndexRecord>(body => body.Query(query => query.QueryString(qs => qs.Query(queryString))));
}
我的方法应该怎么返回?感谢提前!
您可以根据您正在使用的Nest版本取得您的文档。从 Nest 1.0破解更改页面:
DocumentsWithMetaData
当您使用NEST 0.12进行搜索时,您会收到
QueryResponse< T>
有两种方式循环您的结果。.Documents
是一个IEnumerable< T>
和.DocumentsWithMetaData
是和IEnumerable< IHit< T>
根据您的需要,其中一个可能会更容易使用。
从NEST 1.0开始
.DocumentsWithMetaData
现在简称为.Hits
。
< blockquote>
所以给你的代码示例:
NEST 0.12
result.Documents
result.DocumentsWithMetaData
NEST 1.0
result.Hits
0.12属性最大的区别在于
.Documents
只是搜索结果的集合,直接映射到键入OpinionDocumentIndexRecord
。虽然.DocumentsWithMetaData
具有附加的弹性搜索属性,如焦点,字段,说明,类型,可用的源属性是您的OpinionDocumentIndexRecord
。您需要或想要的将最有可能取决于您的特定应用程序/业务需求。
所以如果你使用Nest 0.12.0并且想要一个可以轻松地移植到Nest 1.0.0,我建议使用
.DocumentsWithMetaData
,然后您可以更改为.Hits
,当您升级Nest Client。
如果您真正关心
OpinionDocumentIndexRecord
,无论您访问结果的是什么属性,那么你可以使用以下之一:var myData = new List< OpinionDocumentIndexRecord>();
foreach(var hit in results.DocumentsWithMetaData)
// foreach(var hit in results.Hits)// for Nest 1.0
{
myData.Add(hit.Source) ;
}
返回myData;
希望这有帮助。
I'm writing a method that will search on elastic search for a determined query string, I get the results with several data such as Highlights, Hits, score; But I want only to get and access the data that is within the Documents, how can I achieve that in C#?
This is my code so far:
public string GetByOpinionDocumentTextElastic(string queryString) { var settings = new ConnectionSettings(new Uri(ConfigurationManager.AppSettings["ElasticSearchAddress"])).SetDefaultIndex("caselaw"); var client = new ElasticClient(settings); var result = client.Search<OpinionDocumentIndexRecord>(body => body.Query(query => query.QueryString(qs => qs.Query(queryString)))); }
What should my method return? Thanks in advance!
解决方案Where you get your documents from can depend based on the version of Nest you are using. From the Nest 1.0 Breaking Changes page:
DocumentsWithMetaData
When you do a search with NEST 0.12 you'd get back a
QueryResponse<T>
with two ways to loop over your results..Documents
is anIEnumerable<T>
and.DocumentsWithMetaData
is andIEnumerable<IHit<T>>
depending on your needs one of them might be easier to use.Starting from NEST 1.0
.DocumentsWithMetaData
is now called simply.Hits
.So given your code example:
NEST 0.12
result.Documents result.DocumentsWithMetaData
NEST 1.0
result.Hits
The biggest difference with the 0.12 properties is that
.Documents
are just a collection of the results from the search, mapped directly to your typeOpinionDocumentIndexRecord
. While.DocumentsWithMetaData
have additional Elasticsearch properties like Highlights, Fields, Explanation, Type, available along with the Source property being yourOpinionDocumentIndexRecord
. Which you need or want will most likely depend upon your specific application/business needs.So if you are using Nest 0.12.0 and want a solution that will easily port to Nest 1.0.0, I would recommend using
.DocumentsWithMetaData
and then you can just change to.Hits
when you upgrade the Nest Client.If you truly only care about the
OpinionDocumentIndexRecord
regardless of what property you access off of the results, then you can use one of the folllowing:var myData = new List<OpinionDocumentIndexRecord>(); foreach (var hit in results.DocumentsWithMetaData) //foreach (var hit in results.Hits) //for Nest 1.0 { myData.Add(hit.Source); } return myData;
Hope this helps.
这篇关于从弹性搜索结果检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!