Load specific fields into an Elasticsearch Nest request

The documentation seems to indicate that I can return a subset of the fields instead of the entire document. here is my code:

var result = client.Search<MyObject>(s => s .Fields(f => f.Title) .Query(q => q .QueryString(qs => qs .OnField("title") .Query("the")))); 

I am looking for the word "the" in the "title" field and I just want to return "title". my result.Documents object contains 10 objects, each of which has a null value.

I see the values โ€‹โ€‹I want, but it is deep in the answer to the search: result.Hits [0] .Fields.FieldValues โ€‹โ€‹[0] ...

is there a better way to get a list of returned 'title' fields?

my mapping for data (truncated) is ...

 { "myidex": { "mappings": { "myobject": { "properties": { "title": { "type": "string" }, "artists": { "properties": { "id": { "type": "string", "index": "not_analyzed", "analyzer": "fullTerm" }, "name": { "type": "string", "index": "not_analyzed", "analyzer": "fullTerm" } } } } } } } } 

and my class objects are like this:

 [Table("MyTable")] [Serializable] [ElasticType(Name="myobject")] public class MyObject { [ElasticProperty] public string Title { get; set; } [JsonIgnore] public string Artistslist { get; set; } [ElasticProperty(Analyzer = "caseInsensitive")] public List<Person> Artists { get; set; } } [Serializable] public class Person { [ElasticProperty(Analyzer = "fullTerm", Index = FieldIndexOption.not_analyzed)] public string Name { get; set; } [ElasticProperty(Analyzer = "fullTerm", Index = FieldIndexOption.not_analyzed)] public string Id { get; set; } } 

The artist list comes from my data source (sql), then I parse it into a new List object before indexing the data.

+3
elasticsearch nest
source share
2 answers

I think this deeply nested value makes for a change in Elasticsearch 1.0 and how partial fields are now returned as arrays (see 1.0 Breaking changes - return values for details.). This is described in the NEST 1.0 Breaking Changes documentation; in the Fields () and SourceIncludes () section. It shows an example of using the FieldValue helper method to access these values. Based on this, try the following:

For all elements:

  foreach (var hit in result.Hits) { var title = hit.Fields.FieldValue<MyObject, string>(f => f.Title); } 

For a specific item:

  var title = result.Hits.ElementAt(0) .Fields.FieldValue<MyObject, string>(f => f.Title); 

I know this is still a bit verbose, but it should work for you and will handle the formatting of the new Elasticsearch 1.0 array.

+4
source share

I found a solution in the Nest Github registry. They created a problem about this problem. You should use FielddataFields instead of fields.

https://github.com/elastic/elasticsearch-net/issues/1551

 var result = client.Search<MyObject>(s => s .FielddataFields(f => f.Title) .Query(q => q .QueryString(qs => qs .OnField("title") .Query("the")))); 

and in response you see FieldSelections. You get the fields you like.

+2
source share

All Articles