# Determining the count of words in a given field

**URL:** <https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578>\
**Category:** Kibana\
**Tags:** painless, runtime\
**Created:** [May 16, 2024, 12:02am UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578 "2024-05-16T00:02:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 16, 2024, 12:02am UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/1 "2024-05-16T00:02:16Z")

</div>

I wrote a painless script to determine the word count in a particular filed

emit (doc['skus.keyword'].length)

the problem is that it doesn't count duplicates. so if there is more than one of the same keyword it only count it one. so if the field contains 3 keyword total but 2 are the say it say i have 2. is there a way to get the total count?

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 17, 2024, 9:53pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/2 "2024-05-17T21:53:54Z")

</div>

Hi Honestabe,

Assuming `skus.keyword` is mapped as a `keyword` field and not `text`, you can split the text with the `splitOnToken()` method and count the length of the result array:

```auto
emit(doc['skus.keyword'].value.splitOnToken(' ').length)

```

---

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 17, 2024, 10:48pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/3 "2024-05-17T22:48:38Z")

</div>

I tried it but it one shows 1 now

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 21, 2024, 2:16pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/4 "2024-05-21T14:16:37Z")

</div>

Could you share your index mapping and a sample document in which you expect the word count to be \> 1?

---

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 21, 2024, 8:15pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/5 "2024-05-21T20:15:03Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/0/b/0bc36305a2e4e80a6d051df63381bd9952b740f8.png)

This is the indexing mapping for the field

![DOC TEMP](https://us1.discourse-cdn.com/elastic/original/3X/a/6/a65eeb541be88acb9a8f088bb4bfafb672a3ff48.png)

and a sample doc with expected word count of skus. I appreciate the help!

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 21, 2024, 8:31pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/6 "2024-05-21T20:31:09Z")

</div>

Ah, so there is an array of SKUs in the `skus.keyword` field, and you want to count the number of items in the array, right? That's a different problem from counting the words in a single text string.

Let me try to find an answer. Are the two SKUs in your example identical?

---

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 21, 2024, 8:35pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/7 "2024-05-21T20:35:10Z")

</div>

yes they are identical  
I tried to use the text field but it said  
"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead."

---

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 21, 2024, 8:37pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/8 "2024-05-21T20:37:55Z")

</div>

would there have been a way to use it's text filed to do it?

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 21, 2024, 9:15pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/9 "2024-05-21T21:15:17Z")

</div>

> would there have been a way to use it's text filed to do it?

No, `keyword` is the right choice. `text` is for [text analysis](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-overview.html).

The reason your identical SKUs get deduplicated is due to the inverted index structure that Elasticsearch builds on top of the documents for optimal searching. Instead you can look into the stored document's `_source` and do the array length calculation there:

```auto
emit(params['_source']['skus'].length)

```

Depending on the context in which you're running the script you may need to modify it. The above should work if you're using it e.g. in a [runtime field context](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-runtime-fields-context.html).

---

<div class="post-metadata">

**Author:** ![Honestabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honestabe/32/122763_2.png) [@Honestabe](https://discuss.elastic.co/u/Honestabe)\
**Post date:** [May 21, 2024, 9:24pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/10 "2024-05-21T21:24:46Z")

</div>

That worked. Thanks! Is there a place you guys have some of this painless syntax documented? I know some of the very basic stuff is there but I was having trouble finding the more complex ones

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 21, 2024, 9:44pm UTC](https://discuss.elastic.co/t/determining-the-count-of-words-in-a-given-field/359578/11 "2024-05-21T21:44:02Z")

</div>

I'm glad it worked!

The syntax is a bit scattered in our [documentation](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-guide.html), unfortunately - some parts are in the language specification and in the context sections, and it requires some trial and error to find the right solution.
