从弹性搜索结果检索数据 [英] Retrieve data from elasticsearch results

查看:182
本文介绍了从弹性搜索结果检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种方法,搜索弹性搜索确定的查询字符串,我得到的结果与几个数据,如亮点,点击,得分;但是我只想获取和访问Documents中的数据,我该如何在C#中实现?



这是我的代码到目前为止:

  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 an IEnumerable<T> and .DocumentsWithMetaData is and IEnumerable<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 type OpinionDocumentIndexRecord. While .DocumentsWithMetaData have additional Elasticsearch properties like Highlights, Fields, Explanation, Type, available along with the Source property being your OpinionDocumentIndexRecord. 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屋!

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