C# Elasticsearch嵌套放置映射以添加字段/属性
我正在使用NEST Suggest.Completion查询提供提示性搜索。我已经有了索引中的数据,并希望添加一个新字段“IsActive”,以允许我禁用某些文档出现在建议中 我认为NESTC# Elasticsearch嵌套放置映射以添加字段/属性,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,我正在使用NEST Suggest.Completion查询提供提示性搜索。我已经有了索引中的数据,并希望添加一个新字段“IsActive”,以允许我禁用某些文档出现在建议中 我认为NESTMap方法在运行时会将新字段添加到索引中的所有现有文档中,但它没有。有没有办法让它像那样工作 我正在使用Elasticsearch 6.8.0 使用新字段创建我的对象 [ElasticsearchType( IdProperty = "search" )] public class Sea
Map
方法在运行时会将新字段添加到索引中的所有现有文档中,但它没有。有没有办法让它像那样工作
我正在使用Elasticsearch 6.8.0
使用新字段创建我的对象
[ElasticsearchType(
IdProperty = "search"
)]
public class SearchCompletion
{
public string search { get; set; }
/// <summary>
/// Use this field for aggregations and sorts
/// </summary>
[Keyword]
public string search_keyword { get; set; }
public bool isActive { get; set; } // <---- This is the new field
/// <summary>
/// To use for sorting results
/// since you can't sort by the Completionfield.Weight
/// property for some reason
/// </summary>
public int weight { get; set; }
public CompletionField suggest { get; set; }
}
映射后查询索引的结果
public static void MapSearchCompletions(ElasticClient client, string index)
{
var mapResponse = client.Map<SearchCompletion>(m => m
.Index(index)
.AutoMap()
); //re-apply the index mapping
}
GET /local.project.tests.searchcompletions/searchcompletion/_search
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "local.project.tests.searchcompletions",
"_type": "searchcompletion",
"_id": "the",
"_score": 1,
"_source": {
"search": "the",
"search_keyword": "the",
"weight": 1,
"suggest": {
"input": [
"the",
"the"
],
"weight": 1
}
}
}
]
}
}
是的,更新映射不会更改现有文档。为此,可以使用API
希望能有所帮助。好的,成功了,谢谢!我希望这是一种可以使文档映射与基础模型自动更新的东西(即没有神奇的字符串)。因此,例如,当我推出新版本的API时,索引映射将“自动更新”到C#model,根据需要添加或删除任何字段。我想我必须像对待SQL数据库一样对待这个问题,并让“更新脚本”作为新版本部署的一部分运行。
GET /local.project.tests.searchcompletions/searchcompletion/_search
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "local.project.tests.searchcompletions",
"_type": "searchcompletion",
"_id": "the",
"_score": 1,
"_source": {
"search": "the",
"search_keyword": "the",
"weight": 1,
"suggest": {
"input": [
"the",
"the"
],
"weight": 1
}
}
}
]
}
}
var updateByQueryResponse = await client.UpdateByQueryAsync<Document>(u => u
.Query(q => q.MatchAll())
.Script("ctx._source.isActive = true")
.Refresh());
class Program
{
public class Document
{
public int Id { get; set; }
public bool IsActive { get; set; }
}
static async Task Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);
connectionSettings.DefaultIndex("documents");
var client = new ElasticClient(connectionSettings);
var deleteIndexResponse = await client.Indices.DeleteAsync("documents");
var createIndexResponse = await client.Indices.CreateAsync("documents", d => d
.Map(m => m.AutoMap<Document>()));
var indexDocument = await client.IndexDocumentAsync(new Document {Id = 1});
var refreshAsync = client.Indices.RefreshAsync();
var putMappingResponse = await client.MapAsync<Document>(m => m
.AutoMap());
var updateByQueryResponse = await client.UpdateByQueryAsync<Document>(u => u
.Query(q => q.MatchAll())
.Script("ctx._source.isActive = true")
.Refresh());
var response = await client.GetAsync<Document>(1);
Console.WriteLine(response.Source.IsActive);
}
}
True