# Find number of times we have a character in a row in elastic search

**URL:** <https://discuss.elastic.co/t/find-number-of-times-we-have-a-character-in-a-row-in-elastic-search/48806>\
**Category:** Elasticsearch\
**Created:** [April 29, 2016, 1:10pm UTC](https://discuss.elastic.co/t/find-number-of-times-we-have-a-character-in-a-row-in-elastic-search/48806 "2016-04-29T13:10:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachin1753](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@sachin1753](https://discuss.elastic.co/u/sachin1753)\
**Post date:** [April 29, 2016, 1:10pm UTC](https://discuss.elastic.co/t/find-number-of-times-we-have-a-character-in-a-row-in-elastic-search/48806/1 "2016-04-29T13:10:05Z")

</div>

I am working on elasticsearch and facing an issue that how many time a word comes in a row at the time search records.  
Like I have following rows:

{  
{ "user":"Aniket", "postDate":"2016-04-26","body":"Search as we discuss yesterday one time word", "title":"One time word"}  
},  
{  
"user": "aniket", "postDate": "2016-04-26", "body": "Distribution is hard. Distribution should be easy.word word word word" , "title": "Four times word"}  
},  
{"user": "aniket", "postDate": "2016-04-26", "body": "Distribution is hard. Distribution should be easy.word word word" , "title": "Three times word"}  
},  
{"user": "aniket",  
"postDate": "2016-04-26",  
"body": "Distribution is hard. Distribution should be easy.word word" ,  
"title": "Two times word"  
}  
I have above four rows under user aniket and we have "word" in each row but sometimes it goes two,three,four or one time. I need the result like if I search for "word" and we found word four times in the result than it will come at the top like: 1. word word word word 2. word word word 3. word word 4. word I tried with score too but score is not going to provide me any information related to that.

I am trying the following query  
curl -XGET 'localhost:9200/blog/post/\_search?pretty=1' -d '{  
"query": {  
"match": {  
"body": "word"  
}  
},  
"sort": {  
"\_script": {  
"type": "number",  
"script": "termInfo=\_index['body'][term].tf();return termInfo;",  
"params": {  
"term": "word"  
},  
"lang": "groovy",  
"order": "desc"  
}  
}  
}'

and getting this error:  
{  
"index" : "blog",  
"shard" : 4,  
"status" : 500,  
"reason" : "QueryPhaseExecutionException[[blog][4]: query[filtered(body:word)-\>cache(\_type:post)],from[0],size[10],sort[\<custom:"\_script": org.elasticsearch.index.fielddata.fieldcomparator.DoubleScriptDataComparator$InnerSource@51c07776\>!]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingPropertyException[No such property: body for class: Script5]]; "  
} ]

If I remove the sort part of my query than its give me the result even if I use simple sort then body and order by asc than also it works properly but not is our word count case. Any solution for that and what I am missing?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:55pm UTC](https://discuss.elastic.co/t/find-number-of-times-we-have-a-character-in-a-row-in-elastic-search/48806/2 "2017-07-05T22:55:14Z")

</div>


