Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch NEST return specific fields

I'm trying to write a query that will give me back only one of the fields. Right now I'm storing the filePath of a file and the contents of a file and in my search I want to search against the contents, but only return the filePath.

I'm starting with this statement:

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

Which returns results in searchResults.Documents and adding .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 doesn't have anything in searchResults.Documents but it shows the number of hits correctly using searchResults.Hits.Total.

The File class is just:

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*"
      }
    }
  }
}

Which when ran in Sense returns results and when doing searchResults.Hits.Total gives the number of hits.

However, there is no records in the searchResults.Document IEnumerable.

Is there a different way I'm supposed to be returning the one specific field?

like image 218
Nived Avatar asked Apr 06 '15 16:04

Nived


1 Answers

Use the "source" field to specify which fields you want to pull back. Here is sample code from my application that only returns 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)
        );

You then access the fields through the "source" in the result:

            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
                          };
like image 71
jhilden Avatar answered Nov 04 '22 07:11

jhilden