concept terms in category elasticsearch

appears as: terms
Elasticsearch in Action

This is an excerpt from Manning's book Elasticsearch in Action.

To return people who are group organizers, you can use a terms aggregation. This shows counters for each term that appears in the field you specify—in this case, organizer. The aggregation might look like this:

% curl localhost:9200/get-together/group/_search?pretty -d '{
  "aggregations" : {
    "organizers" : {
      "terms" : { "field" : "organizer" }
    }
  }
}'

In plain English, this request translates to “give me an aggregation named organizers, which is of type terms and is looking at the organizer field.” The following results display at the bottom of the reply:

To get such a list of popular tags in Elasticsearch, you’d use aggregations, and in this specific case, you’d use the terms aggregation on the tags field, which counts occurrences of each term in that field and returns the most frequent terms. Many other types of aggregations are also available, and we’ll discuss them later in this chapter. For example, you can use a date_histogram aggregation to show how many events happened in each month of the last year, use the avg aggregation to show you the average number of attendees for each event, or even find out which users have similar taste for events as you do by using the significant_terms aggregation.

We first looked at the terms aggregation in section 7.1 as an example of how all aggregations work. The typical use case is to get the top frequent X, where X would be a field in your document, like the name of a user, a tag, or a category. Because the terms aggregation counts every term and not every field value, you’ll normally run this aggregation on a non-analyzed field, because you want “big data” to be counted once and not once for “big” and once for “data.”

You could use the terms aggregation to extract the most frequent terms from an analyzed field, like the description of an event. You can use this information to generate a word cloud, like the one in figure 7.6. Just make sure you have enough memory for loading all the fields in memory if you have many documents or the documents contain many terms.

Figure 7.6. A terms aggregation can be used to get term frequencies and generate a word cloud.
  • In the second query, if you have lots of IDs, it might be faster to execute the terms filter on field data:
    "query": {
      "filtered": {
        "filter": {
          "terms": {
            "members": [1, 4],
            "execution": "fielddata"
          }
        }
      }
    }
    !@%STYLE%@!
    {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":5,\"ch\":8},{\"line\":5,\"ch\":32}]]"}
    !@%STYLE%@!