1

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?

Tony
  • 1,986
  • 2
  • 25
  • 36

2 Answers2

1

Try

client.Update<MyDocument, object>(documentId, d => d
    .Doc(new 
    {
        Collector = "value"
    }));

Also, check this answer for more details

https://stackoverflow.com/a/39029907/7327715

Manuel
  • 584
  • 2
  • 9
  • Isn't update for updating an existing document? I want to modify the index's document definition without negatively affecting existing data – Tony Dec 21 '18 at 13:03
1

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.

Tony
  • 1,986
  • 2
  • 25
  • 36