How to sort "sum of 3 properties" in Elasticsearch? How to sort "sum of 3 properties" in Elasticsearch? elasticsearch elasticsearch

How to sort "sum of 3 properties" in Elasticsearch?


Try the below query :

GET /index/_search{    "query": {        "match_all": {}    },    "sort": [        {           "_script": {              "script": "doc['share'].value + doc['like'].value+doc['comment'].value",              "type": "number",              "order": "desc"           }        }    ]}

The query will fetch all data and sort them in descending order according to the total value of (share+like+comment).

If you want to sort in ascending order then change the order value to asc.

To run script query, you need to add script.engine.groovy.inline.search: on on your config/elasticsearch.yml file.


above answare is correct but it was getting field error so blow query is correct.

curl -XGET "localhost:9200/stores/store/_search?pretty=true&size=3" -d '{    "query": {        "match_all": {}    },    "sort": [        {           "_script": {              "script": "doc['"'share'"'].value + doc['"'like'"'].value+doc['"'comment'"'].value",              "type": "number",              "order": "desc"           }        }    ]}'