# Elastic Search Api with Python

**URL:** <https://discuss.elastic.co/t/elastic-search-api-with-python/329009>\
**Category:** Elasticsearch\
**Created:** [March 31, 2023, 6:35am UTC](https://discuss.elastic.co/t/elastic-search-api-with-python/329009 "2023-03-31T06:35:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharath\_B.S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sharath_b.s/32/119254_2.png) [@Sharath\_B.S](https://discuss.elastic.co/u/Sharath_B.S)\
**Post date:** [March 31, 2023, 6:35am UTC](https://discuss.elastic.co/t/elastic-search-api-with-python/329009/1 "2023-03-31T06:35:43Z")

</div>

Im trying to sort the search results according to the date in ascending order. it would be helpful if i could get to know how to sort the results according to the date.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 31, 2023, 11:09am UTC](https://discuss.elastic.co/t/elastic-search-api-with-python/329009/2 "2023-03-31T11:09:06Z")

</div>

Hi @Sharath_B.S

Please provide more details. Are you using [Sort](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#sort-search-results)?

---

<div class="post-metadata">

**Author:** ![tenzing](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tenzing](https://discuss.elastic.co/u/tenzing)\
**Post date:** [March 31, 2023, 6:45pm UTC](https://discuss.elastic.co/t/elastic-search-api-with-python/329009/3 "2023-03-31T18:45:31Z")

</div>

@Sharath_B.S  
Elasticsearch cluster 6.6.2  
elasticsearch python lib 5.4.0

```auto
import pandas as pd
from pandas.io.json import json_normalize
from elasticsearch import Elasticsearch

es = Elasticsearch("http://<host>:<port>")

```

Option 1 you can sort directly in the query:

```auto
q = {
  "query": {
      "bool": {
          "must": [
              {"match": {"field_a": "abc"}}
          ],
          "must":[
              {"range": {"field_timestamp": {"gte": "2021-12-08"}}}
            ]
      }
  },
  "_source": ["field_a", "field_timestamp"],
  "size": 100
  "sort": {"field_timestamp": {"order": "asc"}}
}

data = es.search(index="my-index*", doc_type="event", body=q)
df = pd.concat([json_normalize(item) for item in data["hits"]["hits"]])
df = df[["_source.field_a", "_source.field_timestamp"]]

```

Option 2 you can query without "sort" parameter and sort the data after you receive it from the search. Run the same code in option 1, but comment "sort" parameter and then run these lines:

```auto
df["_source.field_timestamp"] = pd.to_datetime(df["_source.field_timestamp"])
df.sort_values(by="_source.field_timestamp", inplace=True)

```

I usually work with pandas when it comes to analyzing data from Elastic.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 28, 2023, 6:46pm UTC](https://discuss.elastic.co/t/elastic-search-api-with-python/329009/4 "2023-04-28T18:46:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
