다시 정렬 지옥으로 왔다뤼… 🤯 이번에는 동적으로 정렬할 일이 생겨서 sort script 를 만들게 되었다. 가보자공~~~
script를 하도 짰었더니 이제는 쉽게 만들었다..하핳…
Request:
https://{es_url}/{index}/_search
{
"query" : {
"bool" : {
"must": [
{
"terms": {
"_index": [
"{index명}"
]
}
}
],
"filter": {
"bool": {
"must": [
{
"term": {
"{field1}": true
}
},
{
"term": {
"{field2}": true
}
},
{
"terms": {
"{field3}": [
"{조건1}","{조건2}"
]
}
}
]
}
}
}
},
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "if(params.{param값}.get(doc['{field3}'].value) != null) { params.{param값}.get(doc['{field3}'].value)}",
"params": {
"{param값}" : {
"{조건1}" : 20,
"{조건2}" : 10
}
}
},
"order": "desc"
}
}
}
Response :
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "{index명},
"_type": "_doc",
"_id": "{key값}",
"_score": null,
"_source": {
"{field1}": "http://afasfd.com/asdfadsf",
"{field2}": null,
"{field3}": false,
"{field3}": [
{
"values": [
"ababa"
],
"option_name": "a"
},
{
"option_values": [
"asdfas"
],
"option_name": "b"
}
],
"{field4}": "2019-05-08T13:45:12+09:00"
},
"sort": [
20.0
]
},
{
"_index": "{index명},
"_type": "_doc",
"_id": "{key값}",
"_score": null,
"_source": {
"{field1}": "http://afasfd.com/asdfadsf",
"{field2}": null,
"{field3}": false,
"{field3}": [
{
"values": [
"ababa"
],
"option_name": "a"
},
{
"option_values": [
"asdfas"
],
"option_name": "b"
}
],
"{field4}": "2019-05-08T13:45:12+09:00"
},
"sort": [
10.0
]
}
]
}
}