Search code examples
c#elasticsearchnest

Elasticsearch NEST add property to existing index


I would like to add a new property to an existing index that already contains documents.

Original document

[ElasticsearchType(Name = "mydocument", IdProperty = nameof(Id))]
public class MyDocument
{
    public Guid Id { get; set; } = Guid.NewGuid();

    [Date(Name = "occurredon")]
    public DateTime OccurredOn { get; set; }

    [Text(Name = "details")]
    public string Details { get; set; }
}

How the index was initialized and populated...

var client = new ElasticClient(settings);
var createResponse = client.CreateIndex("myindex", d => d.Mappings(ms => ms.Map<MyDocument>(m => m.AutoMap())));

// index many documents...
var docs= new List<MyDocument>();
docs.Add(new MyDocument { OccurredOn = DateTime.UtcNow, Details = "foo1" });
docs.Add(new MyDocument { OccurredOn = DateTime.UtcNow, Details = "foo2" });
var indexResponse = await client.IndexManyAsync(docs, "myindex");

Now I would like to update the document by adding the 'collector' property. The new document will look like this...

[ElasticsearchType(Name = "mydocument", IdProperty = nameof(Id))]
public class MyDocument
{
    public Guid Id { get; set; } = Guid.NewGuid();

    [Date(Name = "occurredon")]
    public DateTime OccurredOn { get; set; }

    [Text(Name = "details")]
    public string Details { get; set; }

    // new property
    [Text(Name = "collector")]
    public string CollectionHost { get; set; }
}

What one-time command do I have to issue to add the new 'collector' property? I tried this, but it fails.

var z = await client.MapAsync(new PutMappingRequest("myindex", typeof(MyDocument)));

I'm sure this can be done, but maybe not with NEST, I'd need to do it with a lower level API?


Solution

  • I guess I miss understood what ES and Kibana were doing. I thought if I indexed a document with additional properties into an existing index they would be lost. Guess not. I just needed to refresh Kibana to make it happy.

    In short, I just added a document that made use of the updated POCO (the one with the 'collector' field) and then refreshed Kibana's field list for the index and everything behaved as expected.

    Guess I made this more difficult than it needed to be. Won't be the last time I over think something.