python elasticsearch-dsl return all unique values for specific key

Viewed 213

I have a field called account_number . It contains random 6 character string.

I can't seem to get python elasticsearch dsl to return just those unique values.

search = Search(using=client, index=index_name).query(
         {
          "range": {
            "date": {
              "gte": "2021-08-01T08:00:00.000Z",
              "lte": "2021-08-31T23:59:59.599Z"
              #"format": "strict_date_optional_time"
            }
        }
    })

 
search.aggs.bucket("account_number","terms",field="account_number",size="1000")

es_data = search.execute()

Not sure if I need to define the account_number in the query or if its in the agg bucket?. Right now I just get random full rows returned with all columns

Here is an example of a working query in non-dsl form. I didnt think the metric was necessary but maybe it is.

{
  "aggs": {
    "3": {
      "terms": {
        "field": "account_number",
        "order": {
          "1": "desc"
        },
        "size": 5
      },
      "aggs": {
        "1": {
          "sum": {
            "field": "hits"
          }
        }
      }
    }
  },
  "size": 0,
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [
    {
      "field": "@timestamp",
      "format": "date_time"
    },
    {
      "field": "date",
      "format": "date_time"
    }
  ],
  "_source": {
    "excludes": []
  },
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "match_all": {}
        },
        {
          "range": {
            "date": {
              "gte": "2021-04-08T21:00:00.000Z",
              "lte": "2021-10-08T21:00:00.000Z",
              "format": "strict_date_optional_time"
            }
          }
        }
      ]
    }
  }
}
0 Answers
Related