Terms Aggregation With Script Field

Hi,

I'm using Elasticsearch JAVA DSL library with Version 5.6

I am trying to create an Elasticsearch Query that is similar to the one mentioned below.

{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": "Math.round((doc['field1'].value - doc['field2'].value)/640000)"     
        }    
    }  
  }
}

The way I am creating a Terms aggregate now is as follows.

TermsAggregationBuilder subAggregation = AggregationBuilders
                            .terms("aggName")
                            .script(new Script("Math.round((doc['field1'].value - doc['field2'].value)/640000)")
                            .size(0)
                            .order(Terms.Order.term(true));

However, the response for this aggregate Builder is as follows.

{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": {
                "source": "Math.round((doc['field1'].value - doc['field2'].value)/640000)",
                "lang": "painless"
          }
       }    
    }  
  }
}

And when the same query is run on Elasticsearch, I end up with the following Error.

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[terms] failed to parse field [script]",
                "line": 7,
                "col": 21
            }
        ],
        "type": "parsing_exception",
        "reason": "[terms] failed to parse field [script]",
        "line": 7,
        "col": 21,
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "[script] unknown field [source], parser not found"
        }
    },
    "status": 400
}

Can someone please direct me in the right way to generate the Expected JSON Structure that I want to use for querying the Elasticsearch using the JAVA SDK.

I also tried the following way of creating the Aggregation and it raised the same issue.

TermsAggregationBuilder subAggregation = AggregationBuilders
                            .terms("aggName")
                            .script(new Script(ScriptType.INLINE, "painless", "Math.round((doc['field1'].value - doc['field2'].value)/640000)", Collections.emptyMap()))
                            .size(0)
                            .order(Terms.Order.term(true));

The String output for the above is generated as follows.

{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": {
                "source": "Math.round((doc['field1'].value - doc['field2'].value)/640000)",
                "lang": "painless"
          }
       }    
    }  
  }
}

Never mind, got the Issue resolved by moving to ES Version 5.6.4.

Turns out the "inline" script was converted to "source" script in 5.6.X and someone downgraded the ES Node I was using to test into a 5.5.2 Node and all the "source" queries were failing to run.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.