# Autocomplete of single words

**URL:** https://discuss.elastic.co/t/autocomplete-of-single-words/31326
**Category:** Elasticsearch
**Created:** [September 29, 2015, 10:39am UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326 "2015-09-29T10:39:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![cardea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cardea/32/5065_2.png) [@cardea](https://discuss.elastic.co/u/cardea)
#### Post date: [September 29, 2015, 10:39am UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/1 "2015-09-29T10:39:24Z")

</div>

Hi, everyone,

following problem: I want to have an autocompletion of single words. This means: if I type in "bor", I want to get "boring (num: 29)" and "border (num: 10)" back. "border" and "boring" are parts of several large texts. I do not want to get the whole text, just the number of documents there "boring" and "border" occurs and - of course - the terms "boring" and "border".

I think I have to use [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html) to get this. But all examples are about fulltext-results. So - how do I get he fragments I want?

Thanks and best,  
Ernesto

---

<div class="post-metadata">

### Author: ![softwaredoug](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/softwaredoug/32/22681_2.png) [@softwaredoug](https://discuss.elastic.co/u/softwaredoug)
#### Post date: [September 29, 2015, 4:16pm UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/2 "2015-09-29T16:16:24Z")

</div>

Do you want a list of snippets where it occurs? Or do you want a breakdown of all the terms in the index that start with `bor` by count?

The former sounds like a highlighting problem. The latter sounds like a [terms aggregration](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) with a [prefix filter](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-prefix-filter.html). I've used the latter for single term autocomplete before. It can work depending on the size of your index and term dictionary (ie number of unique terms).

---

<div class="post-metadata">

### Author: ![cardea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cardea/32/5065_2.png) [@cardea](https://discuss.elastic.co/u/cardea)
#### Post date: [September 29, 2015, 5:07pm UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/3 "2015-09-29T17:07:52Z")

</div>

I want a list of all the terms in the index starting with bor.  
when i have these 6 datasets:  
"blah is blubb boring"  
"blah is blah"  
"boring is border booooo"  
"narf is border"  
"border is boring"  
"blah is border blaaaaaah"  
and I search for "bor" I want to have boring: 3, border: 4 as result. My problem is especially how to get the full terms.

---

<div class="post-metadata">

### Author: ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)
#### Post date: [October 2, 2015, 5:44am UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/4 "2015-10-02T05:44:18Z")

</div>

Using a terms agg with filtering may satisfy what you need. Note that you need to put the prefix text in both the query and in the agg filter:

```
{
  "query": {
    "match_phrase_prefix": {
      "name": "bor"
    }
  },
  "size": 0, 
  "aggs": {
    "myagg": {
      "terms": {
        "field": "name",
        "include": "bor.*", 
        "size": 0
      }
    }
  }
}

```

See this for more details:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#\_filtering\_values](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values)

Here is a working shell script (works for Elasticsearch 1.7.2):

```
curl -XDELETE "http://localhost:9200/foo"

curl -XPUT "http://localhost:9200/foo" -d'
{
  "mappings": {
    "foo": {
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  }
}'

curl -XPOST "http://localhost:9200/foo/foo/_bulk" -d'
{"index":{}}
{ "name" : "blah is blubb boring" }
{"index":{}}
{ "name" : "blah is blah" }
{"index":{}}
{ "name" : "boring is border booooo" }
{"index":{}}
{ "name" : "narf is border" }
{"index":{}}
{ "name" : "border is boring" }
{"index":{}}
{ "name" : "blah is border blaaaaaah" }
'

curl -XGET "http://localhost:9200/foo/_refresh"

echo

curl -XGET "http://localhost:9200/foo/foo/_search?pretty=true" -d'
{
  "query": {
    "match_phrase_prefix": {
      "name": "bor"
    }
  },
  "size": 0, 
  "aggs": {
    "myagg": {
      "terms": {
        "field": "name",
        "include": "bor.*", 
        "size": 0
      }
    }
  }
}'

```

This gives:

```
"aggregations" : {
    "myagg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "border",
        "doc_count" : 4
      }, {
        "key" : "boring",
        "doc_count" : 3
      } ]
    }
```

---

<div class="post-metadata">

### Author: ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)
#### Post date: [October 2, 2015, 5:46am UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/5 "2015-10-02T05:46:14Z")

</div>

As follow up: using edge ngrams is likely to be faster than "match\_phrase\_prefix"

---

<div class="post-metadata">

### Author: ![cardea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cardea/32/5065_2.png) [@cardea](https://discuss.elastic.co/u/cardea)
#### Post date: [October 15, 2015, 12:57pm UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/6 "2015-10-15T12:57:59Z")

</div>

Late reply, but: thank! After some failures because of bad list definitions in my model it works now - and it's really fast, even with match\_phrase\_prefix. 🙂 But I think, I'll test ngrams soon.

---

<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, 11:44pm UTC](https://discuss.elastic.co/t/autocomplete-of-single-words/31326/7 "2017-07-05T23:44:37Z")

</div>


