詳解Elasticsearch如何實現(xiàn)簡單的腳本排序
1、背景
我有一堆學生數(shù)據(jù),其中湖北省的學生需要排在所有數(shù)據(jù)的最前面。其余省正序排序,對于同一個省的數(shù)據(jù),按照年齡倒序排序。
2、分析
對于上方的排序需求,湖北省的學生數(shù)據(jù)需要排在前端,但是湖北省并不是一個字段,那么這個時候改如何實現(xiàn)呢?對于這種場景我們很容易就想到需要腳本script sort
來實現(xiàn)。
3、構建數(shù)據(jù)
3.1 mapping
PUT /index_person { "settings": { "number_of_shards": 1 }, "mappings": { "properties": { "id":{ "type": "long" }, "name": { "type": "keyword" }, "age": { "type": "integer" }, "province":{ "type": "keyword" } } } }
3.2 插入數(shù)據(jù)
PUT /index_person/_bulk {"index":{"_id":1}} {"id":1, "name":"張三","age":18,"province":"湖北"} {"index":{"_id":2}} {"id":2, "name":"李四","age":19,"province":"湖北"} {"index":{"_id":3}} {"id":3, "name":"王武","age":20,"province":"西安"} {"index":{"_id":4}} {"id":4, "name":"趙六","age":21,"province":"西安"} {"index":{"_id":5}} {"id":5, "name":"錢七","age":22,"province":"上海"}
4、實現(xiàn)
4.1 根據(jù)省升序排序
4.1.1 dsl
GET index_person/_search { "query": { "match_all": {} }, "sort": [ { "province": { "order": "asc" } } ] }
4.1.2 運行結果
可以看到省升序的排序順序為 上海、湖北、西安。
4.2 湖北省排第一
4.2.1 dsl
GET index_person/_search { "query": { "match_all": {} }, "sort": [ { "_script": { "type": "number", "order": "desc", "script": { "lang": "painless", "source": """ if(params['_source']['province'] == '湖北'){ 1 } else { 0 } """ } } } ] }
4.2.2 運行結果
通過如上的 script sort
排序之后,就可以看到 湖北省已經是排到第一位了。
4.3 湖北省排第一,其余省升序排序,按照年齡倒序
4.3.1 dsl
GET index_person/_search { "query": { "match_all": {} }, "sort": [ { "_script": { "type": "number", "order": "desc", "script": { "lang": "painless", "source": """ if(params['_source']['province'] == '湖北'){ 1 } else { 0 } """ } } }, { "province": { "order": "asc" }, "age": { "order": "desc", "missing": "_last" } } ] }
4.3.2 java代碼
@Test @DisplayName("腳本排序,固定的某個值的數(shù)據(jù)排在前面,其余的數(shù)據(jù)按照別的字段排序") public void test01() throws IOException { SearchRequest request = SearchRequest.of(searchRequest -> searchRequest.index("index_person") .query(query -> query.matchAll(matchAll -> matchAll)) .size(100) .sort(sort -> sort.script(sortScript -> sortScript.type(ScriptSortType.Number) .order(SortOrder.Desc) .script(script -> script.inline(inline -> inline.source("if(params['_source']['province'] == params.province){\n" + " 1\n" + " } else {\n" + " 0\n" + " }") .params("province", JsonData.of("湖北")) ) ) ) ) .sort(sort -> sort.field(field -> field.field("province").order(SortOrder.Asc) ) ) .sort(sort -> sort.field(field -> field.field("age").order(SortOrder.Desc).missing("_last") ) ) ); System.out.println("request: " + request); SearchResponse<Object> response = client.search(request, Object.class); System.out.println("response: " + response); }
4.3.3 運行結果
5、完整代碼
到此這篇關于詳解Elasticsearch如何實現(xiàn)簡單的腳本排序的文章就介紹到這了,更多相關Elasticsearch腳本排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot中如何使用Convert接口實現(xiàn)類型轉換器
這篇文章主要介紹了Spring Boot中使用Convert接口實現(xiàn)類型轉換器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08springboot+kafka中@KafkaListener動態(tài)指定多個topic問題
這篇文章主要介紹了springboot+kafka中@KafkaListener動態(tài)指定多個topic問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12request.getParameter()方法的簡單理解與運用方式
在JavaWeb開發(fā)中,request對象扮演著至關重要的角色,它是HTTP請求的封裝,request.getParameter()用于獲取客戶端通過GET或POST方式發(fā)送的參數(shù),與之相對,request.setAttribute()用于在服務器端設置屬性,這些屬性只在一次請求中有效2024-10-10