9

I'm quite new to ElasticSearch aggregations. I want to be able to count how many documents are retrieved with a not null field.

Here's what I do to count how many documents don't have a name field.

{
  "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. Is there an existing aggregation?

2 Answers 2

16

Do this by passing an 'exists' filter to a filter aggregation. Like above, just replace 'missing' with 'exists', and also add 'filter' key, so:

{ "size": 3, 
  "query": {
    "query_string": {
      "query" : "martin"
    } 
  }, 
  "aggs": {
    "results_without_mb_id": { 
       "filter": { 
          "exists": { 
            "field": "name" 
          }  
       } 
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

3

You want to use the "exists" filter.

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

Here is a sample that finds all the documents where authResult.codeID exists, and then runs an 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 the documents you don't even need an aggregation, just use the "total" number of hits returned.

2 Comments

actually I don't want to change my query. I just want to count the number of documents with a not null "name" (and "age", and ... ).
"exists" (aka not null) is only valid as a filter option, you can't put it in your query node. You can combine filters and queries together as I did in my example above. So you would have "Name" in the exists filter, and your query_string stuff in the query portion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.