# Return number of maching words in an index

**URL:** https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310
**Category:** Elasticsearch
**Created:** [July 12, 2016, 2:51pm UTC](https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310 "2016-07-12T14:51:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nicopapy](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@nicopapy](https://discuss.elastic.co/u/nicopapy)
#### Post date: [July 12, 2016, 2:51pm UTC](https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310/1 "2016-07-12T14:51:05Z")

</div>

Hi,

I have an index as follow

```
IDX text cID
#1 - "this is a random text" - 2
#2 - "another random cool test" - 3
#3 - "my car is blue " - 2
#4 - "lorem ipsum indolor si" - 3
#5 - "i don't know what is it for"- 2

```

I have a text to search against :

`My very cool text is a long text with many words and random sense.`

What i'm trying to achieve is to find how many of **exact unique words** in the phrase above i have in my whole index with cID = 2

**Expected results :**  
`5` (my, text, is, random, a)

Any idea ?

---

<div class="post-metadata">

### Author: ![nicopapy](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@nicopapy](https://discuss.elastic.co/u/nicopapy)
#### Post date: [July 13, 2016, 6:41am UTC](https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310/2 "2016-07-13T06:41:34Z")

</div>

Considering this stackoverflow thread ([http://stackoverflow.com/questions/38271068/elasticsearch-return-number-of-maching-words](http://stackoverflow.com/questions/38271068/elasticsearch-return-number-of-maching-words)) it seems to be impossible to achieve this with elasticsearch. Someone can confirm ?

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [July 13, 2016, 3:21pm UTC](https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310/3 "2016-07-13T15:21:42Z")

</div>

You can do it but this requires some client-side logic.

First pass your sentence to the analyze API in order to break it into tokens and deduplicate them.  
Then search for each token using a filter on the `cID` field and `terminate_after=1` (for efficiency).  
Finally count how many search requests matched.

---

<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:35pm UTC](https://discuss.elastic.co/t/return-number-of-maching-words-in-an-index/55310/4 "2017-07-05T22:35:59Z")

</div>


