Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to perform filter on aggregation results in elastic search?

I have an elastic search index that contains a certain field on which I want to perform a wildcard query. The issue is that the field is duplicated in many docs hence I want to use aggregation first to get unique values for that field and then perform a wildcard query on top of that. Is there a way I can perform the query on aggregation results in elastic search?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I believe you can find the results you need by collapsing your search results rather than using your strategy of first obtaining the aggregation results and then running a wildcard query.

Adding a working example with index data (with the default mapping), search query and search result.

Index Data:

{
    "role": "example123",
    "number": 1
}

{
    "role": "example",
    "number": 2
}

{
    "role": "example",
    "number": 3
}

Search Query:

{
  "query": {
    "wildcard": {
      "role": "example*"
    }
  },
  "collapse": {
    "field": "role.keyword"         
  }                   
}

Search Result:

 "hits": [
            {
                "_index": "72724517",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "role": "example",
                    "number": 1
                },
                "fields": {
                    "role.keyword": [
                        "example"
                    ]
                }
            },
            {
                "_index": "72724517",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "role": "example123",
                    "number": 1
                },
                "fields": {
                    "role.keyword": [
                        "example123"
                    ]
                }
            }
        ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading