ElasticSearch查询以将值填充或附加到字段

时间:2019-10-24 20:34:18

标签: elasticsearch

我们的ElasticSearch索引包含的文档包含一个名为SourceId(永远不为空)的字段和一个名为CustomCategories的字段。 CustomCategories字段可以为空白,也可以包含1到10个逗号分隔的5个字符的代码。

我需要将自定义类别代码ABCDE添加到所有包含SourceIds 1,2,3,4,10,15,20,22的文档中。

为此可以运行的ElasticSearch查询是什么,请记住,如果CustomCategories字段为空,我只需要用ABCDE填充它,而如果该字段不是空白,我需要将,ABCDE附加到任何值的末尾?

编辑1 :每个来自@jaspreet_chahal的请求都是一个示例文档,以及customCategories字段的映射:

文档

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

1 个答案:

答案 0 :(得分:2)

您可以使用update by query

数据:

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

EDIT1:

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"}
  }
}