首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于高级Nest客户端和AutoMap的PUT映射

基于高级Nest客户端和AutoMap的PUT映射
EN

Stack Overflow用户
提问于 2019-04-04 00:11:14
回答 1查看 2.3K关注 0票数 0

我正在使用以下代码片段创建Elasticsearch索引:

代码语言:javascript
复制
ICreateIndexResponse createIndexResponse = elasticClient.CreateIndex(IndexName, c => c
    .Mappings(ms => ms
        .Map<Document>(m => m.AutoMap())
    )
);

Document类是一个带有属性映射的POCO。

我希望能够将字段添加到映射中。使用Put映射API看起来是可能的:

代码语言:javascript
复制
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

注意,第一个PUT正在创建索引和映射。第二个PUT是添加和修改字段。我想要能够执行第二次投票。

理想的情况是向我的Document类添加属性,调用AutoMap,并使用客户机调用PUT映射API。新的属性将添加到我的映射中,以前存在的属性将被适当地更新/忽略。

这个是可能的吗?是否应该再次使用某些参数调用CreateIndex方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-04 03:20:36

Put映射API在客户机上公开为.Map<T>

代码语言:javascript
复制
var client = new ElasticClient();

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

这将使Document的所有属性自动化。我相信Elasticsearch对那些已经存在的映射将不使用op,并添加新的映射。

如果您只想发送那些尚未映射的属性,可以通过获取Document的自动属性,从索引中检索映射,但后者除外,然后发送带有.Map<T>()的属性。有点像

代码语言:javascript
复制
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);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55506022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档