19
votes

I am trying to sort search results by a date across multiple pages of results. Sorting is working within each page, is there a way to sort across the entire set?

For example: If page 1 has items from Feb 13 through Feb 1st; the second page should not have items after Feb 1st.

Here is a brief snippet of the request that I am sending. I have also tried sorting by date then _score, but that produced even stranger results (primarily, the sort order was the same).

{
  "query": {
    "from": 0,
    "size": 24
    "sort": [
      "published_on": {
        "missing": "_last",
        "ignore_unmapped": true,
        "order": "desc"
      }
    ]
    "custom_filters_score": {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "content": "Some query"
              }
            }
          ]
          ... more ...
        }
      },
      "filters": [
        {
          "filter" => {
            "type" => {
              "value" => "cats"
            }
          },
          "boost" => 2
        }
        ... more ...
      ]
    }
  }
}

Any thoughts? Thanks in advance!

2
It should be date then _score. Please consider including your "strange" results in the question. Somebody might be able to explain why they are so strange. - imotov
As I said in the question I tried sorting by date then _score. I will try to put up some results today. - Jake
Sorry, I didn't read your comment well enough to catch what you are saying. As of right now the _sort is not in place and I am still seeing the same results. - Jake
How is published_on indexed? - imotov

2 Answers

20
votes

I was not multiplying the page index by the number of items on the page. I'm not sure how I missed the same entires on every page, moved up by a single spot.

I'm sure this will resolve all of the other odd issues that I have been dealing with.

8
votes

sort, size, from must be in the root

    {
       query:{
          ...
       },
       sort:[
         ...
       ],
       from:15,
       size:5
    }

Search API - Sort