API-интерфейс Put Mapping предоставляется клиенту как .Map<T>
var client = new ElasticClient();
var putMappingResponse = client.Map<Document>(m => m
.AutoMap()
);
Это позволит автоматизировать все свойства Document
. Я считаю, что Elasticsearch просто не сможет использовать те сопоставления, которые уже существуют, и добавит новые сопоставления.
Если вы хотите отправить только те свойства, которые еще не сопоставлены, это было бы возможно, если получить автоматически сопоставленные свойства 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);
}