我正在使用以下代码片段创建Elasticsearch索引:
ICreateIndexResponse createIndexResponse = elasticClient.CreateIndex(IndexName, c => c
.Mappings(ms => ms
.Map<Document>(m => m.AutoMap())
)
);Document类是一个带有属性映射的POCO。
我希望能够将字段添加到映射中。使用Put映射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
注意,第一个PUT正在创建索引和映射。第二个PUT是添加和修改字段。我想要能够执行第二次投票。
理想的情况是向我的Document类添加属性,调用AutoMap,并使用客户机调用PUT映射API。新的属性将添加到我的映射中,以前存在的属性将被适当地更新/忽略。
这个是可能的吗?是否应该再次使用某些参数调用CreateIndex方法?
发布于 2019-04-04 03:20:36
Put映射API在客户机上公开为.Map<T>
var client = new ElasticClient();
var putMappingResponse = client.Map<Document>(m => m
.AutoMap()
);这将使Document的所有属性自动化。我相信Elasticsearch对那些已经存在的映射将不使用op,并添加新的映射。
如果您只想发送那些尚未映射的属性,可以通过获取Document的自动属性,从索引中检索映射,但后者除外,然后发送带有.Map<T>()的属性。有点像
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);
}https://stackoverflow.com/questions/55506022
复制相似问题