Elasticsearch get random document, atleast 5 from each category

I have 1000 documents with 5 categories. Each category has around 200 documents. I want to pull 30 random documents with at-least 5 from each category. Please suggest me any solution.

Random Score?

{
    "size": 5,
    "query": {
        "function_score": {
            "functions": [{
                "random_score": {
                    "seed": "1518707649"
                }
            }]
        }
    }
}

Make sure to use a different seed for each query (you can use the current timestamp).

2 Likes

Thanks! i want single query not 5 query to pull 30 docs. Any suggestion?

Thanks! i want single query not 5 query to pull 30 docs. I already know this solution.

Would _msearch help?

i will try it.

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