Stpete111
Stpete111

Reputation: 3457

ElasticSearch query to populate or append a value to a field

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.

I need to add the custom category code ABCDE to all documents that contain SourceIds 1,2,3,4,10,15,20,22.

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?

EDIT 1: per request from @jaspreet_chahal here is an example document, as well as the mapping for the customCategories field:

Document

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

Mapping for customCategories field:

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

Upvotes: 0

Views: 1359

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9109

You can use update by query and a Painless script.

Data:

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

Query:

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

Response:

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

Upvotes: 2

Related Questions