简体   繁体   English

使用高级Nest Client和AutoMap进行PUT映射

[英]PUT Mapping Using High Level Nest Client and AutoMap

I am creating Elasticsearch indices using the following code snippet: 我正在使用以下代码片段创建Elasticsearch索引:

ICreateIndexResponse createIndexResponse = elasticClient.CreateIndex(IndexName, c => c
    .Mappings(ms => ms
        .Map<Document>(m => m.AutoMap())
    )
);

The Document class is a POCO with attribute mappings. Document类是具有属性映射的POCO。

I would like the ability to add fields to my mapping. 我希望能够将字段添加到映射中。 This looks to be possible using the Put Mapping API: 使用Put Mapping API似乎可以实现:

PUT my_index 
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT my_index/_mapping/_doc
{
  "properties": {
    "name": {
      "properties": {
        "last": { 
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

Notice that the first PUT is creating the index and mapping. 请注意,第一个PUT正在创建索引和映射。 The second PUT is adding and modifying fields. 第二个PUT是添加和修改字段。 I want to be able to execute the second PUT. 我希望能够执行第二个PUT。

The ideal scenario would be to add properties to my Document class, call AutoMap , and use the client to call the PUT Mapping API. 理想的情况是将属性添加到Document类中,调用AutoMap ,然后使用客户端调用PUT Mapping API。 New properties would be added to my mapping and properties that previously existed were updated/ignored as is appropriate. 新属性将添加到我的映射中,并且适当时会更新/忽略以前存在的属性。

Is this possible? 这可能吗? Should I be calling the CreateIndex method again with certain parameters? 是否应该使用某些参数再次调用CreateIndex方法?

The Put Mapping API is exposed on the client as .Map<T> Put Mapping API在客户端上显示为.Map<T>

var client = new ElasticClient();

var putMappingResponse = client.Map<Document>(m => m
    .AutoMap()
);

This is going to automap all the properties of Document . 这将自动映射Document所有属性。 I believe Elasticsearch will simply no-op for those mappings that already exist, and add the new mappings. 我相信,Elasticsearch将仅对那些已经存在的映射不做任何操作,并添加新的映射。

If you wanted to send only those properties that are not yet mapped, that would be possible by getting the automapped properties of Document , retrieving the mappings from the index, excepting the latter from the former, then sending those with .Map<T>() . 如果你想只发送尚未映射这些属性,这将有可能通过获得的automapped性Document ,从索引检索映射,从以前的除外后者,然后再送那些.Map<T>() Something like 就像是

var defaultIndex = "properties_example";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

var settings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

if (!client.IndexExists(defaultIndex).Exists)
{
    var createIndexResponse = client.CreateIndex(defaultIndex, c => c
        .Mappings(m => m
            .Map<Document>(mm => mm.AutoMap())
        )
    );
}

var properties = new PropertyWalker(typeof(Document), null).GetProperties();

// will use the index inferred for Document, or the default index if none
// specified. Can specify an index on this call if you want to  
var getMappingResponse = client.GetMapping<Document>();

var indexedMappings = getMappingResponse
    // Use the index name to which the call was made.
    .Indices[defaultIndex]
    .Mappings[typeof(Document)]
    .Properties;

var propertiesToIndex = new Dictionary<PropertyName, IProperty>();  
foreach(var property in properties)
{
    if (!indexedMappings.ContainsKey(property.Key))
    {
        propertiesToIndex.Add(property.Key, property.Value);
    }
}

// map new properties only if there are some to map
if (propertiesToIndex.Any())
{
    var request = new PutMappingRequest<Document>()
    {
        Properties = new Properties(propertiesToIndex)
    };

    var putMappingResponse = client.Map(request);
}

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

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