Determining the count of words in a given field

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?

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:

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

I tried it but it one shows 1 now