elastic / elasticsearch-py

Official Python client for Elasticsearch
https://ela.st/es-python
Apache License 2.0
29 stars 1.18k forks source link

es.search() sort option on 7.15+ version does not work with format of "field:direction" #1767

Closed bcfox closed 3 years ago

bcfox commented 3 years ago

elasticsearch-py version 7.15.1:

Please make sure the major version matches the Elasticsearch server you are running.

Description of the problem including expected versus actual behavior: Attempting to use the sort parameter on the search function fails with an error like:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    results = es.search(index='index_name', sort='startTimestamp:desc')
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/client/utils.py", line 301, in _wrapped
    return func(*args, params=params, headers=headers, **kwargs)
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/client/__init__.py", line 1765, in search
    return self.transport.perform_request(
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/transport.py", line 458, in perform_request
    raise e
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/transport.py", line 419, in perform_request
    status, headers_response, data = connection.perform_request(
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/connection/http_urllib3.py", line 287, in perform_request
    self._raise_error(response.status, raw_data)
  File "/Volumes/workdata/src/cognos/es-testing/es-testing/lib/python3.8/site-packages/elasticsearch/connection/base.py", line 337, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'No mapping found for [startTimestamp:desc] in order to sort on')

Steps to reproduce:

from elasticsearch import Elasticsearch

es = Elasticsearch(http_auth=('user','password'))

results = es.search(index='index_name', sort='startTimestamp:desc')

print(results)

This works with version 7.14.2 but fails for 7.15.0 and 7.15.1

sethmlarson commented 3 years ago

Thanks for opening this. This has been reported here: https://github.com/elastic/elasticsearch-py/issues/1743 so I'm closing this as duplicate. A work-around is specifying sort in the body, or using params={"sort": "startTimestamp:desc"}.