1、相關性排序
ElasticSearch為了按照相關性來排序,需要將相關性表示為一個數值,在 Elasticsearch 中, 相關性得分 由一個浮點數進行表示,並在搜索結果中通過 _score 參數返回, 默認排序是 _score 降序。
GET /index_china/fulltext/_search { "query": { "match": { "name": "小張" } } }

2、按照字段排序
有時,按照相關性評分排序並沒有意義,下面的例子通過年齡來對 name 進行排序是有意義的,按照年齡排序,可以使用 sort 參數進行實現:
GET /index_china/fulltext/_search { "query": { "match": { "name": "小張" } }, "sort": "age" }
默認是按照年齡升序

下面是安裝年齡降序
GET /index_china/fulltext/_search { "query": { "match": { "name": "小張" } }, "sort": { "age": { "order": "desc" }} }

3、多級排序
用 age 和 _score 進行查詢,並且匹配的結果首先按照年齡排序,然后按照相關性排序
GET /index_china/fulltext/_search { "query": { "match_all": {} }, "sort":[ {"age":{"order":"asc"}}, {"_score":{"order":"desc"}}] }

