Elastic search fetch limit on each doc type


(sundar) #1

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
}
]
}
}
}
]
}
},

}


(Colin Goodheart-Smithe) #2

You could use the _msearch API 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.


(sundar) #3

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
}
]
}


(Colin Goodheart-Smithe) #4

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


(sundar) #5

Thanks for the clarifications.


(system) #6

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