I have a dataset of stocks in Elasticsearch, in which there can be cases where one stock can be listed on multiple exchanges. Key to identify unique stock is ISIN. So, I have few ISINs which can be present in 2 docs one with Exch: "NSE", one with Exch: "BSE". How can I filter out my query result set such that in case of multiple records of same ISIN, Exch: "NSE" gets prioritized and Exch: "BSE" doesn't come up in result.
{
"hits": {
"total": {
"value": 6329,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "stkindex",
"_id": "2277NSEE",
"_score": null,
"_source": {
"Exch": "NSE",
"Sym": "MRF",
"Isin": "INE883A01011"
}
},
{
"_index": "stkindex",
"_id": "500290BSEE",
"_score": null,
"_source": {
"Exch": "BSE",
"Isin": "INE883A01011",
"Sym": "MRF"
}
},
{
"_index": "stkindex",
"_id": "517174BSEE",
"_score": null,
"_source": {
"Exch": "BSE",
"Isin": "INE671A01010",
"Sym": "HONAUT"
}
}
]
}
}
If you see the above query result, you can see the stock MRF comes 2 times, one with Exch NSE and one with BSE. What I want here is MRF should come only once which has Exch as "NSE".
Tried aggregations but it doesn't gave me expected results.
Collapse API
simply take a look at collapse API.
You just need to collapse the result based on
ISINand use the sort section to prioritize theExcheitherdescorascbased on your needs. (hereNSEis greater thanBSE, sodescshould work for you)