Search code examples
elasticsearchopensearchamazon-opensearch

Elastic Search Aggregate not tied to Top level Query


I have an ElasticSearch/OpenSearch query that returns data and 2 aggregates that give a count and max value, however I need ANOTHER aggregate that is not tied to the top level query. Is that possible w/o a 2nd query that just filters by the clientId and NOT the full top level query?

{
    "aggs": {
        "searchCount": {
            "value_count": {
                "field": "clientId"
            }
        },
        "maxAmount": {
            "max": {
                "field": "total"
            }
        }
    },
    "from": 0,
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "clientId": [
                            5
                        ]
                    }
                }
            ],
            "must": [
                {
                    "bool": {
                        "should": [
                          {
                                "wildcard": {
                                    "customerName": {
                                        "value": "*348*"
                                    }
                                }
                            },
                            {
                                "wildcard": {
                                    "referenceNumber": {
                                        "value": "*348*"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "size": 10,
    "sort": [
        {
            "createdDate": {
                "order": "desc"
            }
        }
    ]
}

Disclaimer: This query was written by the NEST client and does not look entirely correct to me, but works, I would also love comments on how to make this even better.


Solution

  • The solution will be to have 2 queries like @rabbitbr has said.