Elasticsearch get random document, atleast 5 from each category


(Jack Array) #1

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.


(colaci) #2

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).


(Jack Array) #3

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


(Jack Array) #4

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


(David Pilato) #5

Would _msearch help?


(Jack Array) #6

i will try it.


(system) #7

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