Create index with multi field mapping syntax with NEST 2.x

前端 未结 4 1273
野趣味
野趣味 2021-02-09 21:54

I just can\'t seem to get the syntax correct for multi field mapping in NEST 2.0--if that\'s the correct terminology. Every example I\'ve found for mapping seems to be <= the

4条回答
  •  甜味超标
    2021-02-09 22:31

    As far as I can see, you don't have any complex types that you are trying map. So you can easily use NEST attributes to map your objects.

    Check this out:

    [Nest.ElasticsearchType]
    public class MyType
    {
        // Index this & allow for retrieval.
        [Nest.Number(Store=true)]
        int Id { get; set; }
    
        // Index this & allow for retrieval.
        // **Also**, in my searching & sorting, I need to sort on this **entire** field, not just individual tokens.
        [Nest.String(Store = true, Index=Nest.FieldIndexOption.Analyzed, TermVector=Nest.TermVectorOption.WithPositionsOffsets)]
        string CompanyName { get; set; }
    
        // Don't index this for searching, but do store for display.
        [Nest.Date(Store=true, Index=Nest.NonStringIndexOption.No)]
        DateTime CreatedDate { get; set; }
    
        // Index this for searching BUT NOT for retrieval/displaying.
        [Nest.String(Store=false, Index=Nest.FieldIndexOption.Analyzed)]
        string CompanyDescription { get; set; }
    
        [Nest.Nested(Store=true, IncludeInAll=true)]
        // Nest this.
        List Locations { get; set; }
    }
    
    [Nest.ElasticsearchType]
    public class MyChildType
    {
        // Index this & allow for retrieval.
        [Nest.String(Store=true, Index = Nest.FieldIndexOption.Analyzed)]
        string LocationName { get; set; }
    
        // etc. other properties.
    }
    

    After this declaration, to create this mapping in elasticsearch you need to make a call similar to:

    var mappingResponse = elasticClient.Map(m => m.AutoMap());
    

    With AutoMap() call NEST will read your attributes from your POCO and create a mapping request accordingly.

    Also see "Attribute Based Mapping" section from here.

    Cheers!

提交回复
热议问题