Search code examples
nest

NEST 7 ignore a property mapping but still available in _source


How can this mapping be converted using NEST 7 client. I'm trying to set the enabled setting to false. Which will causes Elasticsearch to skip parsing of the contents of the field entirely but still make it available from the _source.

PUT my_index
{
  "mappings": {
    "properties": {
      "user_id": {
        "type":  "keyword"
      },
      "last_updated": {
        "type": "date"
      },
      "session_data": { 
        "type": "object",
        "enabled": false
      }
    }
  }
}

Solution

  • One way is to use attribute mapping

    await client.Indices.CreateAsync("documents", c => c
        .Map<Document>(m => m.AutoMap()));
    
    public class Document
    {
        public string Id { get; set; }
        [Object(Enabled = false)]
        public object Data { get; set; }
    }
    

    another one is to use fluent mapping

    await client.Indices.CreateAsync("documents", c => c
        .Map<Document>(m => m
            .Properties(p => p.Object<object>(o => o.Name(n => n.Data).Enabled(false)))));
    

    You can find more in docs.

    Hope that helps.