# Elastic search fetch limit on each doc type

**URL:** <https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366>\
**Category:** Elasticsearch\
**Created:** [January 12, 2017, 12:06pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366 "2017-01-12T12:06:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sstest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sstest/32/43659_2.png) [@sstest](https://discuss.elastic.co/u/sstest)\
**Post date:** [January 12, 2017, 12:06pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/1 "2017-01-12T12:06:12Z")

</div>

Hi, I am new to Elastic search and trying to retrieve documents across the document types stored in the same index. However what i really need to find is there a way to apply the fetch limit on each type instead of the entire resultset.

For example  
I have an index with name _my\_index_ having two document types _doc1_ and _doc2_. _Doc1_ is a kind of big document having more fields and _doc2_ is kind of small document having very few fields. Now i would to query all document matching a criteria and among the results fetched, i would like to see maximum 5 documents of type _doc1_ and 10 documents of type _doc2_

The below one fetches only 15 rows overall , i may end up having 15 doc1 or 15 doc2 which i dont want to .

GET /my-index/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"fields": [  
"\_all"  
],  
"query": "test"  
}  
}  
]  
}  
},  
"from": 0,  
"size": 15  
}

Looking for something like  
GET /my-index/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"fields": [  
"\_all"  
],  
"query": "test",  
"type": [ {  
"type":"doc1"  
"from" : 0  
"size":5  
},  
{  
type":"doc2"  
"from" : 0  
"size":10  
}  
]  
}  
}  
}  
]  
}  
},

}

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 12, 2017, 12:20pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/2 "2017-01-12T12:20:15Z")

</div>

You could use the [`_msearch` API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html) here and send two searches in the request, one for each document type, setting the size of each search appropriately for the document type being searched.

---

<div class="post-metadata">

**Author:** ![sstest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sstest/32/43659_2.png) [@sstest](https://discuss.elastic.co/u/sstest)\
**Post date:** [January 12, 2017, 1:05pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/3 "2017-01-12T13:05:04Z")

</div>

Yes, tried with that and able to set size specific to the multiple types. But in the response output I am getting is **_Response_** as parent tag with 2 child _ **took** _ tags, unfortunately this is not the one I am looking for since I need to get all the clubbed records with in the single **_took_** in order to support my apps.

Can you give anymore suggestion, also advise if this cannot be done....

**Request** :-  
POST my-index/\_msearch?pretty=true  
{"type":"txnentity"}  
{"from":0,"size":1,"query":{"match\_all":{}}}  
{"type":"sampletxn"}  
{"from":0,"size":2,"query":{"match\_all":{}}}

**Response** :-  
{  
"responses": [  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 4,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "my-index",  
"\_type": "txnentity",  
"\_id": "2",  
"\_score": 1,  
"\_source": {  
"Name": "Test1"  
}  
}  
]  
},  
"status": 200  
},  
{  
"took": 5,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 24,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "my-index",  
"\_type": "sampletxn",  
"\_id": "AVmHwA7Pkw5MudRUOp-q",  
"\_score": 1,  
"\_source": {  
"Name": "Tester2"  
}  
},  
{  
"\_index": "my-index",  
"\_type": "sampletxn",  
"\_id": "AVmH2YWckw5MudRUOp-u",  
"\_score": 1,  
"\_source": {  
"accountName": "Tester3"  
}  
}  
]  
},  
"status": 200  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 12, 2017, 1:44pm UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/4 "2017-01-12T13:44:17Z")

</div>

It is not possible to select the number of hits you would like per document type in a single request. Your application would need to run the `_msearch` request and then combine the different responses (using what ever business rules you have) itself

---

<div class="post-metadata">

**Author:** ![sstest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sstest/32/43659_2.png) [@sstest](https://discuss.elastic.co/u/sstest)\
**Post date:** [January 13, 2017, 9:15am UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/5 "2017-01-13T09:15:34Z")

</div>

Thanks for the clarifications.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 10, 2017, 9:15am UTC](https://discuss.elastic.co/t/elastic-search-fetch-limit-on-each-doc-type/71366/6 "2017-02-10T09:15:40Z")

</div>

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