Search code examples
c#.netelasticsearchnestnest2

Add() method not found in NEST 2.0


This is my NEST2.0 POCO declaration:

[ElasticsearchType(Name = "MyDocument")]
public class MyDocument: DynamicResponse
{
    [String(Store = false, Index = FieldIndexOption.NotAnalyzed)]
    public string HistoryId{ get; set; }

    [String(Store = false, Index = FieldIndexOption.NotAnalyzed)]
    public string FileId { get; set; }

    [Date(Store = false)]
    public DateTime DateTime { get; set; }
}

and this is the mapping for it:

            elastic.Map<MyDocument>(m => m
                .Index(indexName)
                .AutoMap().AllField(a => a.Enabled(false))
                .Dynamic()
                .DynamicTemplates(dt => dt
                    .Add(t => t
                        .Name("pv_values_template")
                        .Match("ch_*")
                        .Mapping(m2 => m2
                            .Number(n => n
                                .Store(false)
                                .Index(NonStringIndexOption.NotAnalyzed)
                                .DocValues(true))))));

Looks like the .Add() method doesn't exist anymore (it was working fine with NEST 1.0)


Solution

  • Add method has been renamed to DynamicTemplate and signature changed a bit, so take a look:

    client.Map<Document>(m => m
        .Index(indexName)
        .AutoMap().AllField(a => a.Enabled(false))
        .Dynamic()
        .DynamicTemplates(dt => dt
            .DynamicTemplate("pv_values_template", t => t 
                .Match("ch_*")
                .Mapping(m2 => m2
                    .Number(n => n
                        .Store(false)
                        .DocValues(true))))));
    

    Probably you are asking where is .Index(NonStringIndexOption.NotAnalyzed) option in the new mapping. This issue has a really nice description, so please take a look.

    Hope it helps.