how make a calculation with elastic document fields?

Viewed 54

I need to make some calculation with each doc's some fields and then return it as response's part I've written this

 TermsAggregationBuilder subAggregation = AggregationBuilders
            .terms("price")
            .script(new Script(ScriptType.INLINE, "painless",

                    "            int total = 0;\n" +
                            "            for (int i = 0; i < 5; ++i) {\n" +
                            "              total += doc['age'].value;\n" +
                            "            }\n" +
                            "            return total ;\n"
                    , Collections.emptyMap()));
    NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
            .withQuery(matchAllQuery())
            .addAggregation(subAggregation)
            .build();
SearchHits<Stories>
            searchHits = elasticsearchOperations.search(nativeSearchQuery, Stories.class
            , IndexCoordinates.of("long_stories"));

and this returns this one

{
"totalHits": 1,
"totalHitsRelation": "EQUAL_TO",
"maxScore": 1,
"scrollId": null,
"searchHits": [
    {
        "id": "44",
        "score": 1,
        "sortValues": [],
        "content": {
            "id": "44",
            "innerId": 5,
            "age": 44,
            "salary": 433,
            "mark": 10,
            "state": "state",
            "stores": [
                {
                    "storiesId": "44",
                    "innerId": 1
                },
                {
                    "storiesId": "44b",
                    "innerId": 2
                },
                {
                    "storiesId": "44c",
                    "innerId": 3
                }
            ],
            "esh": null
        },
        "highlightFields": {}
    }
],
"aggregations": {
    "asMap": {
        "price": {
            "name": "price",
            "metadata": null,
            "buckets": [
                {
                    "aggregations": {
                        "asMap": {},
                        "fragment": true
                    },
                    "keyAsString": "220",
                    "docCount": 1,
                    "docCountError": 0,
                    "key": "220",
                    "keyAsNumber": 220,
                    "fragment": true
                }
            ],
            "type": "sterms",
            "docCountError": 0,
            "sumOfOtherDocCounts": 0,
            "fragment": true
        }
    },
    "fragment": true
},
"empty": false

}

this response gives separate calculation I need it to be with above part ,and also if key which is 220 now if I had some another doc which would give the same answer then response would print 220 and doc count 2 I need it to be separately and in above response like doc1 220 doc2 220 and in above response where actual data holds

1 Answers

Terms aggregation works as "group by". So it will have only unique keys and documents clubbed under those keys. If you need it on per document basis use scripted field in query instead of aggregation.

With scripted field you will need to specify "source" to fetch rest of the document.

You can also explore runtime field in place of scripted field. It offer some benefits over scripted field

You can use script fields to access values in _source and return calculated values based on a script valuation. Runtime fields have these same capabilities, but provide greater flexibility because you can query and aggregate on runtime fields in a search request. Script fields can only fetch values.

Related