ElasticSearch查询以将值填充或附加到字段 [英] ElasticSearch query to populate or append a value to a field
问题描述
我们的ElasticSearch索引的文档包含一个名为 SourceId
(永远不为空)的字段和一个名为 CustomCategories
的字段. CustomCategories
字段可以为空白,也可以包含1到10个逗号分隔的5个字符的代码.
Our ElasticSearch index has documents with a field called SourceId
(never blank), and a field called CustomCategories
. The CustomCategories
field can be blank, or can contain anywhere from 1 to 10 comma-separated 5-character codes.
我需要将自定义类别代码 ABCDE
添加到包含SourceIds 1,2,3,4,10,15,20,22的所有文档中.
I need to add the custom category code ABCDE
to all documents that contain SourceIds 1,2,3,4,10,15,20,22.
为此我可以运行的ElasticSearch查询是什么,请记住,如果 CustomCategories
字段为空,我只需要用 ABCDE
填充它,而如果该字段不是空白,我需要在任何值的末尾附加,ABCDE
吗?
What is an ElasticSearch query that I can run for this, keeping in mind that if the CustomCategories
field is blank, I just need it to be populated with ABCDE
, whereas if that field is NOT blank, I need to append ,ABCDE
to the end of whatever value is there?
编辑1 :每个来自@jaspreet_chahal的请求都是一个示例文档,以及 customCategories
字段的映射:
EDIT 1: per request from @jaspreet_chahal here is an example document, as well as the mapping for the customCategories
field:
文档
{
"_index": "index123",
"_type": "wls_doc",
"_id": "JqkGxmYBwD-D6of2dr43",
"_score": 1.0,
"_source": {
"address": null,
"age": null,
"aliasList": null,
"caution": null,
"dateOfBirth": null,
"eyeColor": null,
"gender": null,
"hairColor": null,
"height": null,
"identifier": null,
"nationality": null,
"placeOfBirth": null,
"program": null,
"race": null,
"remarks": null,
"text": null,
"weight": null,
"entities": null,
"individualName": "John Doe",
"capturedDateTime": "2018-04-17T01:19:52.0131214",
"sourceId": 1,
"captureId": 194857,
"sourceAgencyAcronym": "ABC",
"sourceAgencyName": "Another Bad Creation",
"sourceCountry": "USA",
"sourceParentAgency": "Contoso",
"sourceRegion": "United States",
"url": "http://www.contoso.org",
"categories": [
"ABCDE",
"FGHIJ",
"KLMNO"
],
"customCategories": [
"XA001",
"XB001"
]
}
}
自定义类别字段的映射:
"customCategories": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
推荐答案
您可以使用数据:
[
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "YqgIAW4BgXknAapksgky",
"_score" : 1.0,
"_source" : {
"SourceId" : 1,
"CustomCategories" : "abc"
}
},
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "Y6gIAW4BgXknAapkxQl0",
"_score" : 1.0,
"_source" : {
"SourceId" : 2,
"CustomCategories" : ""
}
},
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "ZKgIAW4BgXknAapk1wlV",
"_score" : 1.0,
"_source" : {
"SourceId" : 3,
"CustomCategories" : "abc"
}
}
]
查询:
POST index42/_update_by_query
{
"script": {
"source": "def categories=ctx._source.CustomCategories;if(categories ==''){ctx._source.CustomCategories='xyz'}else ctx._source.CustomCategories=categories+','+params.catg",
"lang": "painless",
"params":{"catg":"xyz"} ---> new value to be appended
},
"query": {
"terms": {
"SourceId": [1,2] --> source ids to be updated
}
}
}
响应:
[
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "ZKgIAW4BgXknAapk1wlV",
"_score" : 1.0,
"_source" : {
"SourceId" : 3,
"CustomCategories" : "abc"
}
},
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "YqgIAW4BgXknAapksgky",
"_score" : 1.0,
"_source" : {
"SourceId" : 1,
"CustomCategories" : "abc,xyz" --> new value appened
}
},
{
"_index" : "index42",
"_type" : "_doc",
"_id" : "Y6gIAW4BgXknAapkxQl0",
"_score" : 1.0,
"_source" : {
"SourceId" : 2,
"CustomCategories" : "xyz" --> new value added
}
}
]
POST index24/_update_by_query
{
"script": {
"source": "def categories=ctx._source.customCategories;if(categories ==null){ctx._source.customCategories= new ArrayList()}else ctx._source.customCategories.add(params.catg)",
"lang": "painless",
"params":{"catg":"xyz"}
}
}
这篇关于ElasticSearch查询以将值填充或附加到字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!