简体   繁体   English

Elasticsearch NEST 向现有索引添加属性

[英]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.现在我想通过添加 'collector' 属性来更新文档。 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?我确定这可以完成,但也许不能用 NEST,我需要用较低级别的 API 来完成?

I guess I miss understood what ES and Kibana were doing.我想我很想知道 ES 和 Kibana 在做什么。 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.我只需要刷新 Kibana 即可让它开心。

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.简而言之,我只是添加了一个使用更新后的 POCO(带有“collector”字段的文件)的文档,然后刷新了 Kibana 的索引字段列表,一切都按预期运行。

Guess I made this more difficult than it needed to be.猜猜我让这比需要的更困难。 Won't be the last time I over think something.不会是我最后一次想太多。

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 https://stackoverflow.com/a/39029907/7327715

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM