0

I would like to add a field B, which should contain an array with words of field A. I would like to use some kind of script which breaks down the value of Field A into sections of words.

So something like this: Field A: "The Quick Brown Fox" Field B: ["The","Quick","Brown","Fox"]

I'm struggling with googling how to achieve this in ElasticSearch 5.6

Elvira
  • 1,210
  • 1
  • 18
  • 42

1 Answers1

1

You can achieve this with the update by query API and a script that splits fieldA and stores the resulting array into fieldB:

POST test/_update_by_query
{
  "script": {
    "inline": "ctx._source.fieldB = /\\s+/.split(ctx._source.fieldA);",
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}

You'll get this:

    {
      "fieldA" : "The brown fox",
      "fieldB" : [
        "The",
        "brown",
        "fox"
      ]
    }
Val
  • 165,097
  • 10
  • 260
  • 279
  • Trying this gives me the following error: "type": "parse_exception", "reason": "expected one of [inline], [file] or [stored] fields, but found none" I've replaced fieldB with "zoektermen" and fieldA with "hoofdtitel". – Elvira Jan 16 '19 at 15:34
  • Sorry, you're using 5.6 and I tested on 6.5... Check my updated answer – Val Jan 16 '19 at 15:35
  • Thanks for your quick reply. I need to set Regex enabled to true. I use ElasticSearch on AWS. I googled, and apparently I'm not able to change the value in AWS. (https://stackoverflow.com/questions/47271384/enabling-regex-support-on-aws-managed-elasticsearch-in-painless-scripts) Do you know a second solution? – Elvira Jan 16 '19 at 15:42
  • Yes, split that field at indexing time :-) or better: migrate to ES Cloud which is by far much much better than AWS managed ES. – Val Jan 16 '19 at 15:53
  • You can find a good comparison [here](https://www.elastic.co/aws-elasticsearch-service) and and older one [here](https://www.elastic.co/blog/hosted-elasticsearch-services-roundup-elastic-cloud-and-amazon-elasticsearch-service) – Val Jan 17 '19 at 11:50