简体   繁体   中英

How to write script to sort two buckets individually in elasticsearch

I have two fields (ID and Name) in elastic search.

I have a requirement to search and sort data by below conditions :

Bucket 1 : Contains all records having Name value empty (Sort by ID field in ascending order)
Bucket 2 : Contains all records having Name value Present (Sort by Name field in ascending order)

then return all the records of Bucket 1 followed by Bucket 2 in one response.

It sounds like you could state this a bit differently: Get all documents, and Sort first by Name (empty first!), then by ID.

Stated that way, the solution falls out pretty easily. Empty fields should be sorted first when sorting in ascending order anyway, so:

{
    "query" : {
        "match_all": {}
    },
    "sort" : [
        { "Name" : "asc" },
        { "ID" : "asc" }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM