Elastic Search Api with Python

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.

Hi @Sharath_B.S

Please provide more details. Are you using Sort?

Elasticsearch cluster 6.6.2
elasticsearch python lib 5.4.0

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:

q = {
  "query": {
      "bool": {
          "must": [
              {"match": {"field_a": "abc"}}
              {"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:

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.

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