Elasticsearch: aggregating "existing" fields

I am new to ElasticSearch aggregates. I want to be able to count how many documents are retrieved with a non-empty field.

Here's what I do to count how many documents have no field name

.

{
  "size": 3,
  "query": {
    "query_string": {
      "query": "martin"
    }
  },
  "aggs": {
    "results_without_mb_id": {
      "missing": {
        "field": "name"
      }
    }
  }
}

      

It works, but I want to do the exact opposite. Does aggregation exist existing

?

+3


source to share


2 answers


As above, just replace "missing" with "exists" and also add the "filter" key, so:



{ "size": 3, 
  "query": {
    "query_string": {
      "query" : "martin"
    } 
  }, 
  "aggs": {
    "results_without_mb_id": { 
       "filter": { 
          "exists": { 
            "field": "name" 
          }  
       } 
    } 
}

      

+4


source


You want to use the filter "exists".

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html

Here's an example that finds all documents where authResult.codeID exists and then runs aggregation on it:



GET prodstarbucks/authEvent/_search
{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "exists": {
          "field": "authResult.codeID"
        }
      }
    }
  },
  "aggs": {
    "users": {
      "terms": {
        "field": "authInput.userName.userNameNotAnalyzed",
        "size": 5
      }
    }
  }
}

      

}

Note. If you only want to count documents, you don't even need aggregation, just use the total number of hits returned.

0


source







All Articles