Konrad R
Konrad R

Reputation: 75

Get all tags with count from all documents elasticsearch

I've got index mp_v1 with source fields: id and tags. "Tags" field contains all tags in document in string.

Example:

{
        "_index": "mp_v1",
        "_type": "mp",
        "_id": "5",
        "_score": 1,
        "_source": {
          "id": 5,
          "tags": "tag1 black blue"
        }
}

How can I get from elastic search tags with occurances in all documents? For example if I have two documents, the first one with tags "tag1 black blue" and second with tags "blue square" it should return: blue: 2, tag1: 1, black: 1, square: 1

Upvotes: 2

Views: 1966

Answers (2)

Prashant Agrawal
Prashant Agrawal

Reputation: 381

You can simply use a simple term aggregation to get the same with fielddata enabled (dirty way).

But would suggest to use breaking down the field and then perform aggregation.

Upvotes: 0

user3775217
user3775217

Reputation: 4803

I am running ES 5.12

PUT testindex_51
{
    "settings": {
        "analysis": {
            "analyzer": {
            },
             "filter":{
        }
        }
    },
    "mappings": {
        "table1": {
            "properties": {
                "title": {
                    "type": "text",
                    "analyzer": "whitespace",
                    "fielddata": true
                }
            }
        }
    }
}

POST testindex_50/table1
{
  "title" : "tag1 aggs1 blue"
}

POST testindex_50/table1
{
  "title" : "tag2 aggs2 blue"
}

POST testindex_50/table1/_search
{
  "aggs": {
    "tags_count": {
      "terms": {
        "field": "title",
        "size": 10
      }
    }
  }
}

Response

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "tags_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "blue",
          "doc_count": 2
        },
        {
          "key": "aggs1",
          "doc_count": 1
        },
        {
          "key": "aggs2",
          "doc_count": 1
        },
        {
          "key": "tag1",
          "doc_count": 1
        },
        {
          "key": "tag2",
          "doc_count": 1
        }
      ]
    }
  }
}

Upvotes: 4

Related Questions