Elasticsearch Sorting By Date and Score

2,305 views
Skip to first unread message

edwarda...@gmail.com

unread,
Jun 6, 2014, 8:17:34 AM6/6/14
to elastica-...@googlegroups.com
Hi all,

I'm having difficultly sorting by multiple fields, I want to get results ordered with a mixture of _score and by the date. I want the most matching documents first - then ordered by the date

e.g.

{
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "name": {
                     "value": "T-shirt"
                  }
               }
            }
         ],
         "should": [
            {
               "terms": {
                  "colors": [
                     "red",
                     "white"
                  ]
               }
            }
         ]
      }
   },
   "sort": [
      {
         "_score": {
            "order": "desc"
         },
      }
   ]
}

The results should look like:

{
{ "name" : "t-shirt", colors : ['red', 'green'], "date" : "2014-06-02" },
{ "name" : "t-shirt", colors : ['red' ],      "date" : "2014-06-02" }, // this score less but is more recent
{ "name" : "t-shirt", colors : ['red', 'green'], "date" : "2013-06-20" }, // this score is higher but it is older 
{ "name" : "t-shirt", colors : ['red', 'green'], "date" : "2012-06-20" },
}


I've had a look at function_scores and scripts etc but I'm a bit lost - any ideas please?

Many Thanks,

Edward

ruflin

unread,
Jun 8, 2014, 10:56:35 AM6/8/14
to elastica-...@googlegroups.com
Reply all
Reply to author
Forward
Message has been deleted
0 new messages