0

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 Answer 1

1

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!!

Sign up to request clarification or add additional context in comments.

3 Comments

thank you. can you also run a script over multiple buckets?
Your answer helped. I also added to the question above another question: run a script over the results of multiple buckets. For example, get the following result for bucket 1: "Dana Miller and Michelle Bob".
I'm sorry: run a script over multiple hits in a bucket (not multiple buckets).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.