Elasticsearch - how to apply size after sort

2.3k views Asked by At

I have a query that filters on a date range and term, select N documents starting at from position 0, and then sorts them. However, I want the sorting to occur just after filtering, before selecting N documents.

My query:

{
  'filter': {
    'and': [
      {
        'range': {
          'updated_at': {
            'lte': 1482652799999,
            'gte': 1481961600000
          }
        }
      },
      {
        'term': {
          'name': 'my name'
        }
      }
    ]
  },
  'sort': [
    'updated_at',
    {
      'updated_at': 'desc'
    }
  ],
  'size': 3,
  'from': 0
}

This query is giving me:

batch 1= [12/17, 12/17, 12/15], and incrementing the "from" value, batch 2= [12/21, 12/20, 12/18],

but I want:

batch 1= [12/21, 12/20, 12/18] and batch 2= [12/17, 12/17, 12/15].

2

There are 2 answers

0
cjg On

It should apply the sort before filter. Try converting your filter into a query, or at least the term part. It should be executed as query, sort, filter.

0
Car On

Identified the problem with the query. ES accepts 2 types of sort formats - an example from their docs (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html):

"sort" : [
    { "post_date" : {"order" : "asc"}},
    "user",
    { "name" : "desc" },
    { "age" : "desc" },
    "_score"
]

My query was failing because it had the wrong syntax (see the "sort" key below):

{
    'filter': {
        'and': [
            {
                'range': {
                    'updated_at': {
                        'lte': 1482652799999,
                        'gte': 1481961600000
                    }
                }
            },
            {
                'term': {'name': 'my name'}
            }
        ]
    },
    'sort': {'updated_at': 'desc'}  # vs ['updated_at', {'updated_at': 'desc'}]
    'size': 3,
    'from': 0
}