Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch : aggregation "existing" fields

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?

like image 447
litil Avatar asked Oct 31 '14 13:10

litil


People also ask

Is Elasticsearch good for aggregation?

Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.

What is Sum_other_doc_count?

sum_other_doc_count is the number of documents that didn't make it into the the top size terms.

How do you do aggregation in Kibana?

Create an aggregation-based visualization paneledit Choose the type of visualization you want to create, then use the editor to configure the options. On the dashboard, click All types > Aggregation based. Select the visualization type you want to create. Select the data source you want to visualize.


2 Answers

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" 
          }  
       } 
    } 
}
like image 87
emaxi Avatar answered Sep 27 '22 20:09

emaxi


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.

like image 34
jhilden Avatar answered Sep 27 '22 21:09

jhilden