2

I have ElasticSearch 5 and I would like to do sorting based on field value. Imagine having document with category e.g. genre which could have values like sci-fi, drama, comedy and while doing search I would like to order values so that first comes comedies then sci-fi and drama at last. Then of course I will order within groups by other criteria. Could somebody point me to how do this ?

0

1 Answer 1

3

Elasticsearch Sort Using Manual Ordering

This is possible in elasticsearch where you can assign order based on particular values of a field.

I've implemented what you are looking for using script based sorting which makes use of painless script. You can refer to the links I've mentioned to know more on these for below query would suffice what you are looking for.

Also assuming you have genre and movie as keyword with the below mapping.

PUT sampleindex
{
  "mappings": {
    "_doc": {
      "properties": {
        "genre": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        },
        "movie": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

You can make use of below query to get what you are looking for

GET sampleindex/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [{
        "_script": {
            "type": "number",
            "script": {
                "lang": "painless",
                "inline": "if(params.scores.containsKey(doc['genre.raw'].value)) { return params.scores[doc['genre.raw'].value];} return 100000;",
                "params": {
                    "scores": {
                        "comedy": 0,
                        "sci-fi": 1,
                        "drama": 2
                    }
                }
            },
            "order": "asc"
        }
    },
    { "movie.raw": { "order": "asc"}
    }]
}

Note that I've included sort for both genre and movie. Basically sort would happen on genre and post that further it would sort based on movie for each genre.

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

that's not quite what i wanted. I want to define order of the genres by supplying it. no asc,desc but rather define first genre 1, then genre 2, then genre 3 and also being able to say that within genre 1 i want then order by some property asc|desc
Hey @aRagornol, please check the answer once again. I've updated it and I think it would suffice what you are looking for.
For me, above query was throwing illegal_argument_exception; With below change in "inline" script I was able to run the query: "inline": "if(params.scores.containsKey(params._source.listingStatus)) { return params.scores[params._source.listingStatus];} return 100000;",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.