# Issue iterating over the suggestions included in query response

**URL:** https://discuss.elastic.co/t/issue-iterating-over-the-suggestions-included-in-query-response/13959
**Category:** Elasticsearch
**Created:** [October 15, 2013, 11:37pm UTC](https://discuss.elastic.co/t/issue-iterating-over-the-suggestions-included-in-query-response/13959 "2013-10-15T23:37:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)
#### Post date: [October 15, 2013, 11:37pm UTC](https://discuss.elastic.co/t/issue-iterating-over-the-suggestions-included-in-query-response/13959/1 "2013-10-15T23:37:59Z")

</div>

Hi everyone - I am using a combination of term/phrase/completion as part of  
my search query. However I am having some issues trying to iterate over the  
list of suggestions returned.

I generate the "suggestion id" when I create the request object. Now when I  
am try to parse the list of suggestions retrieved, I have no way to iterate  
over the list of suggestions returned or get all of them as Map or any  
other mechanism. I use SearchResponse#getSuggest()#iterator() but it uses  
generics and it assume that I already know all the suggestions that I have  
entered in the request.

My application server is stateless and I do not intend to store the  
suggestion ids on the server while my request is getting processed. so  
overall I am stuck and looking for a generic way to iterate over the  
suggestions from the query response.

public void doMapping(SearchResponse response) {  
Suggest suggest = response.getSuggest();

```
//Option 1:
suggest.getSuggestion(*NAME*); //where do I get the name from, given

```

that I am not storing the state on my server when creating the request

```
//Option 2
Iterator<*what should I specify here*> iter = suggest.iterator();

//Preferred way
Iterator<Suggestion> iter = suggest.iterator();
*//Then iterate over and typecast each object as necessary

```

- 

}

-Amit.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [October 16, 2013, 7:52am UTC](https://discuss.elastic.co/t/issue-iterating-over-the-suggestions-included-in-query-response/13959/2 "2013-10-16T07:52:13Z")

</div>

Hey,

using suggest.iterator() and then checking/casting if it is a  
PhraseSuggestion/TermSuggestion/CompletionSuggestion while running through  
the iterator, makes most sense from what I read so far.

Also if you only need to access get options and their text, there might not  
be any need for you to cast into the specific suggestion implementation.

--Alex

On Wed, Oct 16, 2013 at 1:37 AM, Amit Soni [amitsoni29@gmail.com](mailto:amitsoni29@gmail.com) wrote:

> Hi everyone - I am using a combination of term/phrase/completion as part  
> of my search query. However I am having some issues trying to iterate over  
> the list of suggestions returned.
> 
> I generate the "suggestion id" when I create the request object. Now when  
> I am try to parse the list of suggestions retrieved, I have no way to  
> iterate over the list of suggestions returned or get all of them as Map or  
> any other mechanism. I use SearchResponse#getSuggest()#iterator() but it  
> uses generics and it assume that I already know all the suggestions that I  
> have entered in the request.
> 
> My application server is stateless and I do not intend to store the  
> suggestion ids on the server while my request is getting processed. so  
> overall I am stuck and looking for a generic way to iterate over the  
> suggestions from the query response.
> 
> public void doMapping(SearchResponse response) {  
> Suggest suggest = response.getSuggest();
> 
> ```
> //Option 1:
> suggest.getSuggestion(*NAME*); //where do I get the name from, given
> 
> ```
> 
> that I am not storing the state on my server when creating the request
> 
> ```
> //Option 2
> Iterator<*what should I specify here*> iter = suggest.iterator();
> 
> //Preferred way
> Iterator<Suggestion> iter = suggest.iterator();
> *//Then iterate over and typecast each object as necessary
> 
> ```
> 
> - 
> 
> }
> 
> -Amit.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:12am UTC](https://discuss.elastic.co/t/issue-iterating-over-the-suggestions-included-in-query-response/13959/3 "2017-07-06T02:12:15Z")

</div>


