How to return results from elasticsearch after a threshold match

Viewed 20

I have two queries as follows:

  1. The first query returns the count of all documents per domain.
  2. The second query returns the count where a field is empty.

Later I filter it in my backend, such that, if for a domain the count of documents missing field value is more than a specific threshold then only consider them else ignore. Could these two queries be combined together, such that I could do the threshold comparison and then return the results.

The first query is as follows:

GET database/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "source": {
              "value": "Web"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "domains": {
      "terms": {
        "field": "domain_id"
      }
    }
  }
}

The second query just applies a should filter as follows:

GET mapachitl/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "source": {
              "value": "Web"
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "address.city.keyword": {
              "value": ""
            }
          }
        },
        {
          "term": {
            "address.zip.keyword": {
              "value": ""
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "aggs": {
    "domains": {
      "terms": {
        "field": "domain_id"
      }
    }
  }
}

Can I only return those domains where the ratio of documents missing city or zip code is more than 25%? I read about scripting but not sure how can I use it here.

0 Answers
Related