用于向字段填充或追加值的ElasticSearch查询

uklbhaso  于 2022-11-28  发布在  ElasticSearch
关注(0)|答案(1)|浏览(167)

我们的ElasticSearch索引中的文档包含一个名为SourceId(从不为空)的字段和一个名为CustomCategories的字段。CustomCategories字段可以为空,也可以包含1到10个逗号分隔的5字符代码。
我需要将自定义类别代码ABCDE添加到所有包含SourceId 1、2、3、4、10、15、20、22的文档中。
我可以为此运行什么样的ElasticSearch查询,请记住,如果CustomCategories字段为空,我只需要用ABCDE填充它,而如果该字段不为空,我需要将,ABCDE附加到任何值的末尾?

EDIT 1:根据@jaspreet_chahal的请求,下面是一个示例文档,以及customCategories字段的Map:
文件

{
                "_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字段的Map:

"customCategories": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
vof42yt1

vof42yt11#

您可以使用update by query和Painless脚本。
数据来源:

[
      {
        "_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 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"}
  }
}

回应:

[
      {
        "_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
        }
      }
    ]

相关问题