# How to search multiple indexes with the JAVA Client when the index names are known only at runtime

**URL:** <https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598>\
**Category:** Elasticsearch\
**Created:** [February 1, 2013, 9:10pm UTC](https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598 "2013-02-01T21:10:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Sabou](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_sabou/32/1472_2.png) [@Paul\_Sabou](https://discuss.elastic.co/u/Paul_Sabou)\
**Post date:** [February 1, 2013, 9:10pm UTC](https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598/1 "2013-02-01T21:10:37Z")

</div>

I'm having some issues with the limitations of the Java Client API when  
dealing with search requests across multiple indexes.

_What works:_

- I have two indexes "myIndex\_one" and "myIndex\_two".
- I can execute a search across multiple indexes if I know their name  
before compilation

client.prepareSearch("myIndex\_one","myIndex\_two").setQuery(query).execute().  
get()

as I'm using the method:

```
SearchRequestBuilder prepareSearch(String... indices); // 

```

Client.java:line 355

_What doesn't work:_

I would like to be able to execute a search request across multiple indexes  
defined at runtime.  
As such, instead of "myIndex\_one" and "myIndex\_two" I would like to have a  
list of indexes  
\<"myIndex\_1",..."myIndex\_n"\> defined at runtime and then just call  
List myIndexes = new ArrayList("myIndex1","myIndex2","myIndex8");  
client.prepareSearch(myIndexes).setQuery(query).execute().get();

But right now the ES JAVA Client doesn't have an overloaded prepareSearch  
method like:

```
SearchRequestBuilder prepareSearch(List<String> indices); 

```

_Question_:  
Is there any workaround for this? Or maybe I'm missing some method  
somewhere that might offer the same functionality?

Thanks  
Paul.

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 1, 2013, 9:26pm UTC](https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598/2 "2013-02-01T21:26:18Z")

</div>

Simply convert the list in a array of Strings. Java varargs expects an  
Array.

--  
Ivan

On Fri, Feb 1, 2013 at 1:10 PM, Paul Sabou [paul.sabou@gmail.com](mailto:paul.sabou@gmail.com) wrote:

> I'm having some issues with the limitations of the Java Client API when  
> dealing with search requests across multiple indexes.
> 
> _What works:_
> 
> - I have two indexes "myIndex\_one" and "myIndex\_two".
> - I can execute a search across multiple indexes if I know their name  
> before compilation
> 
> client.prepareSearch("myIndex\_one","myIndex\_two").setQuery(query).execute  
> ().get()
> 
> as I'm using the method:
> 
> ```
> SearchRequestBuilder prepareSearch(String... indices); //
> 
> ```
> 
> Client.java:line 355
> 
> _What doesn't work:_
> 
> I would like to be able to execute a search request across multiple  
> indexes defined at runtime.  
> As such, instead of "myIndex\_one" and "myIndex\_two" I would like to have a  
> list of indexes  
> \<"myIndex\_1",..."myIndex\_n"\> defined at runtime and then just call  
> List myIndexes = new ArrayList("myIndex1","myIndex2","myIndex8");  
> client.prepareSearch(myIndexes).setQuery(query).execute().get();
> 
> But right now the ES JAVA Client doesn't have an overloaded prepareSearch  
> method like:
> 
> ```
> SearchRequestBuilder prepareSearch(List<String> indices);
> 
> ```
> 
> _Question_:  
> Is there any workaround for this? Or maybe I'm missing some method  
> somewhere that might offer the same functionality?
> 
> Thanks  
> Paul.
> 
> --  
> 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:** ![Paul\_Sabou](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_sabou/32/1472_2.png) [@Paul\_Sabou](https://discuss.elastic.co/u/Paul_Sabou)\
**Post date:** [February 2, 2013, 11:26am UTC](https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598/3 "2013-02-02T11:26:30Z")

</div>

Thanks Ivan. Indeed this is the right way to use it.

I have been mislead by the fact that I'm trying to do that from Scala and  
I'm not familiar enough with the Scala vargs tricks.

As others might get stuck in this trivial issue, I'm posting the Scala  
solution below:

val indexes = List("myIndex\_one","myIndex\_two","myIndex\_three")  
val result = client.prepareSearch(indexes:\_\*).setQuery("some ES query")

Cheers  
Paul.

On Friday, February 1, 2013 10:26:18 PM UTC+1, Ivan Brusic wrote:

> Simply convert the list in a array of Strings. Java varargs expects an  
> Array.
> 
> --  
> Ivan
> 
> On Fri, Feb 1, 2013 at 1:10 PM, Paul Sabou \<[paul....@gmail.com](mailto:paul....@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > I'm having some issues with the limitations of the Java Client API when  
> > dealing with search requests across multiple indexes.
> > 
> > _What works:_
> > 
> > - I have two indexes "myIndex\_one" and "myIndex\_two".
> > - I can execute a search across multiple indexes if I know their name  
> > before compilation
> > 
> > client.prepareSearch("myIndex\_one","myIndex\_two").setQuery(query).execute  
> > ().get()
> > 
> > as I'm using the method:
> > 
> > ```
> > SearchRequestBuilder prepareSearch(String... indices); // 
> > 
> > ```
> > 
> > Client.java:line 355
> > 
> > _What doesn't work:_
> > 
> > I would like to be able to execute a search request across multiple  
> > indexes defined at runtime.  
> > As such, instead of "myIndex\_one" and "myIndex\_two" I would like to have  
> > a list of indexes  
> > \<"myIndex\_1",..."myIndex\_n"\> defined at runtime and then just call  
> > List myIndexes = new ArrayList("myIndex1","myIndex2","myIndex8");  
> > client.prepareSearch(myIndexes).setQuery(query).execute().get();
> > 
> > But right now the ES JAVA Client doesn't have an overloaded prepareSearch  
> > method like:
> > 
> > ```
> > SearchRequestBuilder prepareSearch(List<String> indices); 
> > 
> > ```
> > 
> > _Question_:  
> > Is there any workaround for this? Or maybe I'm missing some method  
> > somewhere that might offer the same functionality?
> > 
> > Thanks  
> > Paul.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > 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:53am UTC](https://discuss.elastic.co/t/how-to-search-multiple-indexes-with-the-java-client-when-the-index-names-are-known-only-at-runtime/10598/4 "2017-07-06T02:53:23Z")

</div>


