1

When I use the Elasticsearch.Net client to do a Get document request, I am setting _source to false and specifying the stored fields I want to return. I do not want the _source because there are large contents and even if I exclude them, the server still loads the source into memory to parse it which is very slow and I do not want.

When I do have source included, I can just specify the object type and it is automatically de-serialized from the _source. How can I do this with the Fields property? It is of type FieldValues : IsADictionaryBase<string, LazyDocument>

var response = await client.GetAsync<MyObject>(docId,
    s => s.Index(myIndex)
         .SourceEnabled(false)
         .StoredFields(new string[] { 
              "MyStringArray1", 
              "MyStringArray2", 
              "MyLongValue" }
          ));

The only way I have found to do this is to iterate over all the properties in the object manually.

MyObject mine = new MyObject()
{
    MyStringArray1 = response.Fields["MyStringArray1"].As<string[]>(),
    MyStringArray2 = response.Fields["MyStringArray2"].As<string[]>(),
    MyLongValue = response.Fields.Value<long>("MyLongValue")
};

Is there an easier way to deserialize to MyObject type?

Gabe
  • 122
  • 5

1 Answers1

0

to prevent fetch large data you can use source filtering

var searchResponse = _client.Search<Project>(s => s
    .Source(sf => sf
        .Includes(i => i 
            .Fields(
                f => f.Name,
                f => f.StartedOn,
                f => f.Branches
            )
        )
        .Excludes(e => e 
            .Fields("num*") 
        )
    )
    .Query(q => q
        .MatchAll()
    )
);

using this way, objects are serialized automatically and all excluded fields will be null