# Get distinct words for search suggestions

**URL:** <https://discuss.elastic.co/t/get-distinct-words-for-search-suggestions/5805>\
**Category:** Elasticsearch\
**Created:** [November 8, 2011, 4:16am UTC](https://discuss.elastic.co/t/get-distinct-words-for-search-suggestions/5805 "2011-11-08T04:16:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JAS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jas/32/3085_2.png) [@JAS](https://discuss.elastic.co/u/JAS)\
**Post date:** [November 8, 2011, 4:16am UTC](https://discuss.elastic.co/t/get-distinct-words-for-search-suggestions/5805/1 "2011-11-08T04:16:38Z")

</div>

Hi,

I like ElasticSearch so far, nice job!

I am indexing what I call Items under a single index called items, a  
default index with default config. So I add items like this:

{  
"name" : "Steak"  
"description" : "yummy"  
}

Suppose I've added 1000 items, 300 with the name "Steak", 200 with the  
name "Steak Sandwich" and then 500 random names involving the word  
Steak.

If I do a simple search like [http://localhost:9200/items/\_search?q=steak&size=100](http://localhost:9200/items/_search?q=steak&size=100)  
I get at list like the following:

{

```
              {
                  # "_index": "items",
                  # "_type": "document",
                  # "_id": "41ae5059a28b4838ad3bbc19f84610ba",
                  # "_score": 2.1778517,
                  #
                    -
                    "_source": {
                        * "id":

```

"41ae5059a28b4838ad3bbc19f84610ba",  
\* "name": "Steak",  
\* "description": "\n"  
}  
},

.... A WHOLE BUNCH OF "name" : "Steak" ...

```
              {
                  # "_index": "items",
                  # "_type": "document",
                  # "_id": "f7c714f8b66f4854884379c3a954e4f6",
                  # "_score": 2.1087837,
                  #
                    -
                    "_source": {
                        * "id":

```

"f7c714f8b66f4854884379c3a954e4f6",  
\* "name": "Steak Encebollado",  
\* "description": "Steak with onions.\n"  
}  
},

This is not what I need for search suggestions. I basically want "the  
set of all distinct names with the word Steak in them". In other  
words, even if there are 300 documents with the name "Steak" I only  
want the exact name "Steak" to be returned once. I want to see all  
the variations of names that contain the word "Steak" exactly once.

How do I do that? I don't want to do it client side, so I want the  
limit parameter to mean "return 100 distinct variations of the name  
Steak".

Is this possible? Is it efficient?

Thanks,

Joel

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [November 8, 2011, 7:01am UTC](https://discuss.elastic.co/t/get-distinct-words-for-search-suggestions/5805/2 "2011-11-08T07:01:30Z")

</div>

Why not create a new field where you feed the name which is splitted  
by whitespace (=\> array of strings) and then facet on that value for  
the suggestions. Not sure if you could simply use a whitespace  
tokenizer as well.

But probably you want also suggestions for parts of the words like 'st  
which should return 'Steak''? Then you should use the (edge) ngram  
token filters...

Peter.

On Nov 8, 5:16 am, JAS [j...@republicofapps.com](mailto:j...@republicofapps.com) wrote:

> Hi,
> 
> I like Elasticsearch so far, nice job!
> 
> I am indexing what I call Items under a single index called items, a  
> default index with default config. So I add items like this:
> 
> {  
> "name" : "Steak"  
> "description" : "yummy"
> 
> }
> 
> Suppose I've added 1000 items, 300 with the name "Steak", 200 with the  
> name "Steak Sandwich" and then 500 random names involving the word  
> Steak.
> 
> If I do a simple search likehttp://localhost:9200/items/\_search?q=steak&size=100  
> I get at list like the following:
> 
> {
> 
> ```
> {
> # "_index": "items",
> # "_type": "document",
> # "_id": "41ae5059a28b4838ad3bbc19f84610ba",
> # "_score": 2.1778517,
> #
> -
> "_source": {
> * "id":
> 
> ```
> 
> "41ae5059a28b4838ad3bbc19f84610ba",  
> \* "name": "Steak",  
> \* "description": "\n"  
> }  
> },
> 
> .... A WHOLE BUNCH OF "name" : "Steak" ...
> 
> ```
> {
> # "_index": "items",
> # "_type": "document",
> # "_id": "f7c714f8b66f4854884379c3a954e4f6",
> # "_score": 2.1087837,
> #
> -
> "_source": {
> * "id":
> 
> ```
> 
> "f7c714f8b66f4854884379c3a954e4f6",  
> \* "name": "Steak Encebollado",  
> \* "description": "Steak with onions.\n"  
> }  
> },
> 
> This is not what I need for search suggestions. I basically want "the  
> set of all distinct names with the word Steak in them". In other  
> words, even if there are 300 documents with the name "Steak" I only  
> want the exact name "Steak" to be returned once. I want to see all  
> the variations of names that contain the word "Steak" exactly once.
> 
> How do I do that? I don't want to do it client side, so I want the  
> limit parameter to mean "return 100 distinct variations of the name  
> Steak".
> 
> Is this possible? Is it efficient?
> 
> Thanks,
> 
> Joel

---

<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 6, 2017, 3:49am UTC](https://discuss.elastic.co/t/get-distinct-words-for-search-suggestions/5805/3 "2017-07-06T03:49:37Z")

</div>

Closed on July 6, 2017, 3:49am UTC


