# Autcomplete implementation

**URL:** <https://discuss.elastic.co/t/autcomplete-implementation/132448>\
**Category:** Elasticsearch\
**Created:** [May 18, 2018, 9:29am UTC](https://discuss.elastic.co/t/autcomplete-implementation/132448 "2018-05-18T09:29:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamidou830](https://avatars.discourse-cdn.com/v4/letter/h/c77e96/32.png) [@hamidou830](https://discuss.elastic.co/u/hamidou830)\
**Post date:** [May 18, 2018, 9:29am UTC](https://discuss.elastic.co/t/autcomplete-implementation/132448/1 "2018-05-18T09:29:10Z")

</div>

Hello,

I want to implement autocomplete with elasticsearch and I'm unable to do it.  
I want to have something like the following :  
My indexed strings are for e.g :

"Developpeur Java"  
"Developpeur C#"  
"Je suis Developpeur"  
"Je suis écrivan"  
"Il est developpeur C++"

For input "developpeur", I want as output :  
"developpeur Java"  
"developpeur C#"  
"developpeur C++"

for input "suis", I want as ouput :  
"suis developpeur"  
"suis écrivan"

I tried to acheive this using [completion suggester](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#search-suggesters-completion) :

here's the elasticsearch I'm using :  
"number": "6.2.2",  
"build\_hash": "10b1edd",  
"build\_date": "2018-02-16T19:01:30.685723Z",  
"build\_snapshot": false,  
"lucene\_version": "7.2.1",  
"minimum\_wire\_compatibility\_version": "5.6.0",  
"minimum\_index\_compatibility\_version": "5.0.0"

the mapping :  
{  
"settings": {  
"number\_of\_shards": "1",  
"analysis": {  
"filter": {  
"prefix\_filter": {  
"type": "edge\_ngram",  
"min\_gram": 1,  
"max\_gram": 20  
},  
"ngram\_filter": {  
"type": "nGram",  
"min\_gram": "3",  
"max\_gram": "3"  
},  
"synonym\_filter": {  
"type": "synonym",  
"synonyms": [  
"hackwillbereplacedatindexcreation,hackwillbereplacedatindexcreation"  
]  
},  
"french\_stop": {  
"type": "stop",  
"stopwords": "_french_"  
}  
},  
"analyzer": {  
"word": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"asciifolding",  
"french\_stop"  
],  
"char\_filter": []  
},  
"prefix": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"asciifolding",  
"synonym\_filter",  
"prefix\_filter"  
],  
"char\_filter": []  
},  
"ngram\_with\_synonyms": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"asciifolding",  
"synonym\_filter",  
"ngram\_filter"  
],  
"char\_filter": []  
},  
"ngram": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"asciifolding",  
"ngram\_filter"  
],  
"char\_filter": []  
}  
}  
}  
},  
"mappings": {  
"training": {  
"properties": {  
"id": {  
"type": "text",  
"index": false  
},  
"label": {  
"type": "text",  
"index\_options": "docs",  
"copy\_to": "full\_label",  
"analyzer": "word",  
"fields": {  
"prefix": {  
"type": "text",  
"index\_options": "docs",  
"analyzer": "prefix",  
"search\_analyzer": "word"  
},  
"ngram": {  
"type": "text",  
"index\_options": "docs",  
"analyzer": "ngram\_with\_synonyms",  
"search\_analyzer": "ngram"  
}  
}  
},  
"labelSuggest": {  
"type": "completion",  
"analyzer": "word"  
},  
}  
}  
}  
}

Then when I create the index with my data I do this (this is the body of the put call made to the ES api, I'm using pyhon for this):

```
            body = {
            "label": r["title"],
            "labelSuggest": {
                "input": r["title"].ngrams()
            },
            "weight": 1.
        }

```

r["title"].ngrams() gets all the ngrams of the title. e.g :  
"Development research biotech" would give : "Development", "research", "biotech", "Development research", "research biotech" and "Development research biotech"

then to call the suggseter, I do :

```
   POST http://localhost:9200/training/_search ? pretty{
"suggest": {
	"labelSuggest": {
		"text": "developpeur",
		"completion": {
			"field": "labelSuggest",
            "skip_duplicates": true

		}
	}
}

```

}

The result is :  
{  
"text": "développement",  
"\_index": "activity\_20180518092449",  
"\_type": "activity",  
"\_id": "2031ce8b-6589-3270-afdf-7901aa21efa1",  
"\_score": 1,  
"\_source": {  
"id": "2031ce8b-6589-3270-afdf-7901aa21efa1",  
"name": "development research biotech",  
"labelSuggest": [  
"development",  
"research",  
"biotech",  
"development research",  
"research biotech",  
"development research biotech"  
]  
}  
But I want something that gives me : "development", "development research" and "development research biotech" (supposing we only have that document as input)

I hope my question is clear. I searched a lot about it in vain.

Thanks in advance

---

<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:** [June 15, 2018, 9:29am UTC](https://discuss.elastic.co/t/autcomplete-implementation/132448/2 "2018-06-15T09:29:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
