Вопрос

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top