elastic/elasticsearch-java

How elasticsearch determines aggregation kind

Mert18 opened this issue · 0 comments

Java API client version

8.10

Java version

11

Elasticsearch Version

8.10

Problem description

So I am basically creating aggregations. When these aggregations uses multiple script parameters (scripts saved to elasticsearch) and I use the stored script, the aggregation kind becomes "SimpleValue". My stored scripts like (avg(x) + sum(y)) etc.

But when I want to get "avg(x)" value only (enters the if block), my aggregation becomes a Filter Aggregation(I want it to be SimpleValue). And the result bucket aggregation deletes "simple_value#" prefix from my resulting aggregation. I recently moved my infrastracture from java client 7 to 8. There is so little information about this on the internet. Do anyone have an idea how java client classifies Aggregations by kind? Thanks in advance. (code is edited and not fully functional code, I put it for reference.)

        Script.Builder scriptBuilder = new Script.Builder();
        if (var.getType() == null) {
            scriptBuilder.inline(s -> s.source(var.getScriptExpression()).lang(ScriptLanguage.Painless));
        } else {
            String scriptId = UUID.nameUUIDFromBytes((var.getName()).getBytes()).toString();
            StoredScriptId storedScriptId = StoredScriptId.of(ss -> ss.id(scriptId));
            scriptBuilder.stored(storedScriptId);
        }
        bucketAggName = "simple_value#" + var.getName();
        script = scriptBuilder.build();

        Map<String, String> bucketsPaths = new HashMap<>();

        Aggregation bucketScriptAggregation = Aggregation.of(a -> a.bucketScript(b -> b
                .script(script)
                .gapPolicy(GapPolicy.Skip)
                .bucketsPath(p -> p.dict(bucketsPaths))));