# Autocomplete - Return terms instead of whole documents

**URL:** <https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063>\
**Category:** Elasticsearch\
**Created:** [February 5, 2016, 12:55pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063 "2016-02-05T12:55:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilippC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippc/32/7337_2.png) [@PhilippC](https://discuss.elastic.co/u/PhilippC)\
**Post date:** [February 5, 2016, 12:55pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/1 "2016-02-05T12:55:14Z")

</div>

Hi

I am trying to implement a simple auto completion for query terms.  
There are many different approaches but most of them do return documents instead of terms

- or the authors simply stopped explaining from that point and i am not able to adapt 😔

A user is typing in a query - e.g. _phil_  
What i want is to **provide a list of term completion suggestions** like _philipp, philius, philadelphia, ..._

I am able to get document matches via (edge)ngrams, phrase\_prefix and so on  
but i am am stuck at retrieving matching terms (or candidates).

Can someone give me a hint?

---

<div class="post-metadata">

**Author:** ![Benjamin\_Gathmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_gathmann/32/7561_2.png) [@Benjamin\_Gathmann](https://discuss.elastic.co/u/Benjamin_Gathmann)\
**Post date:** [February 5, 2016, 1:00pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/2 "2016-02-05T13:00:15Z")

</div>

Hi Philipp,  
if you look at the Definitive Guide for Elasticsearch, there is an extensive section called "Query-Time Search-as-You-Type". Have a look at that.  
Benjamin

---

<div class="post-metadata">

**Author:** ![PhilippC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippc/32/7337_2.png) [@PhilippC](https://discuss.elastic.co/u/PhilippC)\
**Post date:** [February 5, 2016, 1:10pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/3 "2016-02-05T13:10:20Z")

</div>

Thank you for your answer.

I already tried _match\_phrase\_prefix_ or _match\_phrase_ but my problem is how a result looks  
and how i need to process it in order to get the completion suggestions (instead of document results).

Or do i have a general misunderstanding?

(btw: I am working with the java-api)

---

<div class="post-metadata">

**Author:** ![Benjamin\_Gathmann](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_gathmann/32/7561_2.png) [@Benjamin\_Gathmann](https://discuss.elastic.co/u/Benjamin_Gathmann)\
**Post date:** [February 5, 2016, 1:14pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/4 "2016-02-05T13:14:05Z")

</div>

I use the REST API, but the point is (probably) the same:  
In addition to the query, you need to define what the query shall return by using the "\_source" or "fields" parameter.

---

<div class="post-metadata">

**Author:** ![PhilippC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippc/32/7337_2.png) [@PhilippC](https://discuss.elastic.co/u/PhilippC)\
**Post date:** [February 5, 2016, 1:26pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/5 "2016-02-05T13:26:20Z")

</div>

But it do not need the whole content of a field. Only some terms/tokens as completion suggestion.

I have documents like this {"title":"...", "description":"...", "content":"..."}  
All fields have larger string values but especially the field content contains fulltext content.

For example while typing _phil_ i do not want to suggest the whole title of a document containing e.g. Philadelphia. Just the word Philadelphia.

---

<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:18pm UTC](https://discuss.elastic.co/t/autocomplete-return-terms-instead-of-whole-documents/41063/6 "2017-07-05T23:18:32Z")

</div>


