0
votes

I want to perform a terms aggregation and in every results bucket get the hit fields after a script manipulation was performed.

For example, if these are the documents:

{"age": 15, "firstName": "Dana", "lastName": "Miller"}
{"age": 15, "firstName": "Michelle", "lastName": "Bob"}
{"age": 32, "firstName": "Mary", "lastName": "Smith"}
{"age": 32, "firstName": "Anna", "lastName": "Taylor"}

The aggregation is by the "age" field, and the script is: "return 'doc['firstName'] + ' ' + doc['lastName']"

The results should be:

bucket 1 (age: 15):

  • "Dana Miller"
  • "Michelle Bob"

Buchet 2 (age 32):

  • Mary Smith
  • Anna Taylor

Is this possible in elasticsearch?

EDIT:

In addition, I'm looking for a way to run a script over multiple hits in a bucket. For example, if we use the documents above and the terms aggregation by the age field, can I get the results of the hits in the bucket together in the following way?

Bucket 1 (age 15):

  • "Dana Miller and Michelle Bob"

Bucket2 (age 32):

  • "Mary Smith and Anna Taylor"

Is it possible in ES?

thank you.

1

1 Answers

1
votes

I guess Following Query can help you.

{
"size": 0,
"aggs": {
  "group By age": {
     "terms": {
        "field": "age"
     },
     "aggs": {
        "top hits": {
           "top_hits": {
              "script_fields": {
                 "Name": {
                    "script": "doc['firstName'].value + ' ' + doc['lastName'].value "
                  }
                 }
              }
           }
        }
      }
   }
  }

Hope this helps!!