We have elastic search document with a string field named "Type". This field can have different values from "A" to "Z". Multiple documents can have the same type i.e. multiple documents can have Type "A"
We want to write an elastic search query that returns us maximum 30 of these documents. We want output to be in different group based on the type. For example:
- If we have 10 document of type A, 15 of type B, 20 of type C, I should get all 10 with type A, all 15 with type B and 5 of type C.
- If we have 0 document of type A, 10 of type B, 15 of type C, 20 of type D, I should get all 10 with type B, 15 of type C and 5 of type D.
- Worst case: If we don't have any document of type A ... Y and 30 document of type Z, I should get 30 document of type Z.
I have written a very basic multi-search query for this (total 26 queries) i.e.
POST _msearch/
{"index":"<index_name>","type":"<type>"}
{"from":0,"size":30,"query":{"bool":{"must":[{"terms":{"type":["A"]}}]}}}
{"index":"<index_name>","type":"<type>"}
{"from":0,"size":30,"query":{"bool":{"must":[{"terms":{"type":["B"]}}]}}}
...
{"index":"<index_name>","type":"<type>"}
{"from":0,"size":30,"query":{"bool":{"must":[{"terms":{"type":["Z"]}}]}}}
I am worried about multi-search query execution i.e. for case 1 and case 2, we got sufficient output i.e. 30 documents from first few queries, then why should we execute rest of the multi-search queries? Is there any way to stop multi-search query operation once we get desired number of result i.e. terminate multi search once we get 30 or more result.
Please note:
- I have given very simple condition here i.e. the condition of different multi search is more complex than just on basis of type.
- We want multiple collections in output i.e. type A, type B etc. all in different collections. (due to this constraint we had to rule out painless script option)