0

After finding the value in params with field1 value, I try to aggregate with the found field value.

GET /_search
{
  "size" : 0,
  "query" : {
    "ids" : {
      "types" : [ ],
      "values" : [ "docId1", "docId2" .... ]
    }
  },
  "aggs": {
    "countries": {
      "terms": {
        "script": {
          "params": {
            "field1": "country",
            "field2": "test",
            "field3": "test"
          },
          "inline": "def syn = params['field1']; doc[syn].value"
        }
      }
    }
  }
}

It fails with the message below.

      "failures": [
         {
            "reason": {
               "type": "script_exception",
               "reason": "failed to run inline script [def syn = params['field1']; doc[syn].value] using lang [groovy]",
               "caused_by": {
                  "type": "missing_property_exception",
                  "reason": "No such property: params for class: 8f7af04189385c7d3c546861d4817e4ae8ca75f5"
               }
            }
         }
      ]

If I enter it directly without importing the value from params, it will be aggregation normally.

GET /_search
{
  "size" : 0,
  "query" : {
    "ids" : {
      "types" : [ ],
      "values" : [ "docId1", "docId2" .... ]
    }
  },
  "aggs": {
    "countries": {
      "terms": {
        "script": {
          "params": {
            "field1": "country",
            "field2": "test",
            "field3": "test"
          },
          "inline": "doc['country'].value"
        }
      }
    }
  }
}

Like this:

{
   ...
   "aggregations": {
      "how_to_merge": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "KR",
               "doc_count": 90
            },
            {
               "key": "JP",
               "doc_count": 83
            },
            {
               "key": "US",
               "doc_count": 50
            },
            {
               "key": "BE",
               "doc_count": 9
            }
         ]
      }
   }
}

I am using groovy and elasticsearch version 2.2

I can't use Python or JavaScript, which requires additional plug-ins to be installed.

Why can't I get the values in params?

1 Answer 1

1

If I'm not mistaken, in 2.2 you didn't need to prefix the parameter with params. in the script source. Try like this:

  "aggs": {
    "countries": {
      "terms": {
        "script": {
          "params": {
            "field1": "country",
            "field2": "test",
            "field3": "test"
          },
          "inline": "def syn = field1; doc[syn].value"
        }
      }
    }
  }
Sign up to request clarification or add additional context in comments.

3 Comments

One more thing, Is there any way to return null if there is no corresponding value? example)def syn = wrongData; if (syn == null) doc[syn].value Currently, errors always occur if there is no corresponding value.
It's not work def syn = wrongData Before checking null, an error occurs in the part where the value is substituted into syn. No such property: params for class: 8f7af04189385c7d3c546861d4817e4ae8ca75f5
Perfect, I see you've created a new question to handle this: stackoverflow.com/questions/70165047/…

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.