'Random samples from Elasticsearch query using python

ELK noob here. I have an index with 10000 documents and I define a filter for date range and it gives me 500 documents. I want to get 50 random documents.

I am trying to do this using python.

It would really help if someone can direct me towards an example.

This is my code block

search_body = {
  "query": {
    "bool": {
      "must": [
        {"match": {
          "customer_gender": "MALE"
        }}
      ],
      "filter": [
        {"range": {
          "order_date": {
            "gte": "2022-03-27"
          }
        }}
      ]
    }
  }
}

results = scan(client, index="sample_data", query=search_body)
for hit in results:
   print(hit)

I want to add the randomness to it. I suppose function_score may be the I need to look at, but a working example can really help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source