# Counting Terms In Text Field

**URL:** <https://discuss.elastic.co/t/counting-terms-in-text-field/363790>\
**Category:** Kibana\
**Created:** [July 25, 2024, 1:59pm UTC](https://discuss.elastic.co/t/counting-terms-in-text-field/363790 "2024-07-25T13:59:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)\
**Post date:** [July 25, 2024, 1:59pm UTC](https://discuss.elastic.co/t/counting-terms-in-text-field/363790/1 "2024-07-25T13:59:48Z")

</div>

I'm using Logstash's RSS input to pull data into Elastic. I've configured the field `title` as a multi-type field, one as text, the other as a keyword. I want to count the frequency of words that appear in titles across searches to identify trends. However, when I go to create a visualization, say a bar chart, I choose terms as the aggregation and then the only field I can select is `title.keyword`. Obviously, that doesn't help me as it doesn't count the number of times a word shows up but the number of times that whole string of text shows up. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Musab\_Dogan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/musab_dogan/32/70691_2.png) [@Musab\_Dogan](https://discuss.elastic.co/u/Musab_Dogan)\
**Post date:** [July 28, 2024, 11:03pm UTC](https://discuss.elastic.co/t/counting-terms-in-text-field/363790/2 "2024-07-28T23:03:28Z")

</div>

The [fieldata](https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html#fielddata-mapping-param) disabled by default. You should enable it to use `text field type` in the aggregation or Kibana visualization.

Here is an example for you.

```auto
#push example data
PUT collection_test2/_doc/1
{"text_field":"word1 word2 word3"}

PUT collection_test2/_doc/2
{"text_field":"word2 word2 word3 word4"}

#enable the fielddata to true
PUT collection_test2/_mapping
{
  "properties": {
    "text_field": { 
      "type": "text",
      "fielddata": true
    }
  }
}

#Create visualization or search your text field type data
GET collection_test2/_search
{
  "size": 0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "text_field"
      }
    }
  }
}

```

Note: You should update the Kibana data view after update the index mapping and refresh the browser =\> `Stack Management => Data views => collection_test3 => refresh (there is a button for that)`

Note2: The result will show the number of docs that contains per wordX.

 ![first image](https://us1.discourse-cdn.com/elastic/original/3X/f/7/f7bdc830cda29eb3fc0c25b3d28a0f6a6ee93ae5.png)

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [August 1, 2024, 7:23am UTC](https://discuss.elastic.co/t/counting-terms-in-text-field/363790/3 "2024-08-01T07:23:32Z")

</div>

Adding to @Musab_Dogan suggestion -

From the [doc](https://elastic.co/guide/en/elasticsearch/reference/current/text.html#before-enabling-fielddata)

It usually doesn’t make sense to enable fielddata on text fields. Field data is stored in the heap with the [field data cache](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-fielddata.html) because it is expensive to calculate. Calculating the field data can cause latency spikes, and increasing heap usage is a cause of cluster performance issues.

Most users who want to do more with text fields use [multi-field mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html) by having both a `text` field for full text searches, and an unanalyzed [`keyword`](https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html) field for aggregations, as follows:

```auto
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "my_field": { 
        "type": "text",
        "fields": {
          "keyword": { 
            "type": "keyword"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)\
**Post date:** [August 6, 2024, 8:40pm UTC](https://discuss.elastic.co/t/counting-terms-in-text-field/363790/4 "2024-08-06T20:40:11Z")

</div>

This was originally what I did, but I wasn't getting any results. Enabling fielddata solved my issue.
