ElasticSearch NEST returns specific fields

I am trying to write a query that returns only one of the fields to me. Right now I am storing the filePath of the file and the contents of the file, and in my search I want to search the contents, but only return filePath.

I start with this statement:

var searchResults = client.Search<File>( s => s.Query(q => q.Wildcard(w => w.Value("*" + genre + "*").OnField("fileContents"))).AllIndices().AllTypes()); 

which returns the results in searchResults.Documents and adds fields to it.

 var searchResults = client.Search<File>( s => s.Query(q => q.Wildcard(w => w.Value("*" + genre + "*").OnField("fileContents"))).AllIndices().AllTypes().Fields(f=>f.filePath)); 

And it has nothing in searchResults.Documents , but shows the correct number of hits with searchResults.Hits.Total .

File Class:

 public class File { public string filePath { get; set; } public string fileContents { get; set; } } 

This generates the following json request:

 { "fields": [ "filePath" ], "query": { "wildcard": { "fileContents": { "value": "*int*" } } } } 

That when run in Sense returns the results, and when doing searchResults.Hits.Total - the number of hits.

However, there are no entries in searchResults.Document IEnumerable.

Is there any other way that I have to return one specific field?

+7
c # elasticsearch nest
source share
1 answer

Use the source field to indicate which fields you want to cancel. Here is a sample code from my application that returns only some fields.

  var searchResults = ElasticClient.Search<AuthForReporting>(s => s .Size(gridSortData.PageSize) .From(gridSortData.PageIndex * gridSortData.PageSize) .Sort(sort) .Source(sr => sr .Include(fi => fi .Add(f => f.AuthEventID) .Add(f => f.AuthResult.AuthEventDate) .Add(f => f.AuthInput.UID) .Add(f => f.AuthResult.CodeID) .Add(f => f.AuthResult.AuthenticationSuccessful) .Add(f => f.AuthInput.UserName) .Add(f => f.AuthResult.ProductID) .Add(f => f.AuthResult.ProductName) .Add(f => f.AuthInput.AuthType) .Add(f => f.AuthResult.Address.City) .Add(f => f.AuthResult.Address.State) .Add(f => f.AuthResult.Address.CountryCode) .Add(f => f.AuthResult.RulesFailed) ) ) .Query(query) ); 

Then you get access to the fields through the "source" as a result of:

  var finalResult = from x in searchResults.Hits select new AlertListRow { AlertCode = x.Source.AlertCode, AlertDate = x.Source.AlertDate, AlertID = x.Id, AlertSummary = x.Source.Subject, AlertMessage = x.Source.Body }; 
+11
source share

All Articles