Elasticsearch- Filtering out unique docs based on a field value and prioritizing on basis of another field value

22 Views Asked by At

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.

1

There are 1 best solutions below

0
Sajad Soltanian On

Collapse API

simply take a look at collapse API.

You just need to collapse the result based on ISIN and use the sort section to prioritize the Exch either desc or asc based on your needs. (here NSE is greater than BSE, so desc should work for you)