Perform a search on several indices at the same time, with one method call.
The returned results are broken down by query.
This method can be used in several different kinds of situations.
Here are two typical usage scenarios:
You have multiple indices that serve different purposes.
This is typical when you want to search your main index
as well as an index that contains a history of searches (to be used for autocomplete).
You want to target one index and send it multiple queries,
where, for example, each query contains different settings or filters,
or the query itself is slightly adjusted.
Note that for 2., you will want to use the “stopIfEnoughMatches”
value of the strategy parameter.
You can only use up to 50 queries in a multiple queries request.
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`$queries=[['indexName'=>'categories','query'=>$myQueryString,'hitsPerPage'=>3],['indexName'=>'products','query'=>$myQueryString,'hitsPerPage'=>3,'facetFilters'=>'promotion'],['indexName'=>'products','query'=>$myQueryString,'hitsPerPage'=>10]];$results=$client->multipleQueries($queries);var_dump($results['results']);
1
2
3
4
5
6
7
8
9
10
# perform 3 queries in a single API call:# - 1st query targets index `categories`# - 2nd and 3rd queries target index `products`res=Algolia.multiple_queries([{index_name: 'categories',query: my_query_string,hitsPerPage: 3},{index_name: 'products',query: my_query_string,filters: '_tags:promotion'},{index_name: 'products',query: my_query_string,hitsPerPage: 10}])putsres['results']
constqueries=[{indexName:'categories',query:'search in categories index',params:{hitsPerPage:3}},{indexName:'products',query:'first search in products',params:{hitsPerPage:3,filters:'_tags:promotion'}},{indexName:'products',query:'another search in products',params:{hitsPerPage:10}}];// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`client.multipleQueries(queries).then(({results})=>{console.log(results);});
1
2
3
4
5
6
7
8
9
10
# perform 3 queries in a single API call:
# - 1st query targets index `categories`
# - 2nd and 3rd queries target index `products`
results=client.multiple_queries([{'indexName':'categories','query':myQueryString,'hitsPerPage':3},{'indexName':'products','query':myQueryString,'filters':'_tags:promotion'},{'indexName':'products','query':myQueryString,'hitsPerPage':3},])print(results['results'])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Perform 3 queries in a single API call:// - 1st query target index `categories`// - 2nd and 3rd queries target index `products`letqueries:[IndexedQuery]=[.init(indexName:"categories",query:Query("electronics").set(\.hitsPerPage,to:3)),.init(indexName:"products",query:Query("iPhone").set(\.hitsPerPage,to:3).set(\.filters,to:"_tags:promotions")),.init(indexName:"products",query:Query("Galaxy").set(\.hitsPerPage,to:10)),]client.multipleQueries(queries:queries){resultinifcase.success(letresponse)=result{print("Response: \(response)")}}
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`List<IndexQuery>queries=newArrayList<>();queries.add(newIndexQuery("categories",newQuery(myQueryString).setHitsPerPage(3)));queries.add(newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(3).set("filters","_tags:promotion")));queries.add(newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(10)));// Execute the sequence of queries until the end.finalMultipleQueriesStrategystrategy=MultipleQueriesStrategy.NONE;client.multipleQueriesAsync(queries,strategy,newCompletionHandler(){@OverridepublicvoidrequestCompleted(JSONObjectcontent,AlgoliaExceptionerror){// [...]}});
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`varindexQueries=newList<MultipleQueries>{newMultipleQueries{IndexName="categories",Params=newQuery(myQueryString){HitsPerPage=3}},newMultipleQueries{IndexName="products",Params=newQuery(myQueryString){HitsPerPage=3,Filters="_tags:promotion"}},newMultipleQueries{IndexName="products",Params=newQuery(myQueryString){HitsPerPage=10}},};MultipleQueriesRequestrequest=newMultipleQueriesRequest{Requests=indexQueries};varres=client.MultipleQueries(request);// Asynchronousvarres=awaitclient.MultipleQueriesAsync(request);
//Sync version// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`List<MultipleQueries>queries=Arrays.asList(newMultipleQueries("categories",newQuery(myQueryString).setHitsPerPage(3)),newMultipleQueries("products",newQuery(myQueryString).setHitsPerPage(3).setFilters("_tags:promotion")),newMultipleQueries("products",newQuery(myQueryString).setHitsPerPage(10)));MultipleQueriesResponse<Object>search=client.multipleQueries(newMultipleQueriesRequest(queries));//Async versionCompletableFuture<MultipleQueriesResponse<Object>>search=client.multipleQueriesAsync(queries);
1
2
3
4
5
6
7
8
9
// Perform 3 queries in a single API call:// - 1st query targets the `categories` index// - 2nd and 3rd queries target the `products` indexqueries:=[]search.IndexedQuery{search.NewIndexedQuery("categories",opt.Query("computer"),opt.HitsPerPage(3)),search.NewIndexedQuery("products",opt.Query("computer"),opt.HitsPerPage(3),opt.Filters("_tags:promotion")),search.NewIndexedQuery("products",opt.Query("computer"),opt.HitsPerPage(10)),}res,err:=client.MultipleQueries(queries,"")
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`client.execute{multiQueries(searchinto"categories"queryQuery(query=myQueryString,hitsPerPage=Some(3)),searchinto"products"queryQuery(query=myQueryString,hitsPerPage=Some(3),filters=Some("_tags:promotion")),searchinto"products"queryQuery(query=myQueryString,hitsPerPage=Some(10)))strategyMultiQueries.Strategy.stopIfEnoughMatches}
# perform 3 queries in a single API call:
# - 1st query targets index `categories`
# - 2nd and 3rd queries target index `products`
results=client.multiple_queries([{'indexName':'categories','query':my_query_string,'hitsPerPage':3},{'indexName':'products','query':my_query_string,'filters':'_tags:promotion'},{'indexName':'products','query':my_query_string,'hitsPerPage':10}],{'X-Forwarded-For':'94.228.178.246'})print(results['results'])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Perform 3 queries in a single API call:// - 1st query target index `categories`// - 2nd and 3rd queries target index `products`letqueries:[IndexedQuery]=[.init(indexName:"categories",query:Query("electronics").set(\.hitsPerPage,to:3)),.init(indexName:"products",query:Query("iPhone").set(\.hitsPerPage,to:3).set(\.filters,to:"_tags:promotions")),.init(indexName:"products",query:Query("Galaxy").set(\.hitsPerPage,to:10)),]varrequestOptions=RequestOptions()requestOptions.headers["X-Algolia-UserToken"]="user123"client.multipleQueries(queries:queries,requestOptions:requestOptions){resultinifcase.success(letresponse)=result{print("Response: \(response)")}}
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`List<IndexQuery>queries=newArrayList<>();queries.add(newIndexQuery("categories",newQuery(myQueryString).setHitsPerPage(3)));queries.add(newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(3).set("filters","_tags:promotion")));queries.add(newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(10)));// Execute the sequence of queries until the end.finalMultipleQueriesStrategystrategy=MultipleQueriesStrategy.NONE;client.multipleQueriesAsync(queries,strategy,newRequestOptions().setHeader("X-Algolia-User-ID","94.228.178.246"),newCompletionHandler(){@OverridepublicvoidrequestCompleted(JSONObjectcontent,AlgoliaExceptionerror){// [...]}});
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`varindexQueries=newList<MultipleQueries>{newMultipleQueries{IndexName="categories",Params=newQuery(myQueryString){HitsPerPage=3}},newMultipleQueries{IndexName="products",Params=newQuery(myQueryString){HitsPerPage=3,Filters="_tags:promotion"}},newMultipleQueries{IndexName="products",Params=newQuery(myQueryString){HitsPerPage=10}},};varrequestOptions=newRequestOptions{Headers=newDictionary<string,string>{{"X-Algolia-User-ID","userID1"}}};MultipleQueriesRequestrequest=newMultipleQueriesRequest{Requests=indexQueries};varres=client.MultipleQueries(request,requestOptions);// Asynchronousvarres=awaitclient.MultipleQueriesAsync(request,requestOptions);
//Sync version// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`List<IndexQuery>queries=Arrays.asList(newIndexQuery("categories",newQuery(myQueryString).setHitsPerPage(3)),newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(3).setFilters("_tags:promotion")),newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(10)));MultiQueriesResultsearch=client.multipleQueries(queries,newRequestOptions().addExtraHeader("X-Algolia-User-ID","user123"));//Async version// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`List<IndexQuery>queries=Arrays.asList(newIndexQuery("categories",newQuery(myQueryString).setHitsPerPage(3)),newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(3).setFilters("_tags:promotion")),newIndexQuery("products",newQuery(myQueryString).setHitsPerPage(10)));CompletableFuture<MultiQueriesResult>search=client.multipleQueries(queries,newRequestOptions().addExtraHeader("X-Algolia-User-ID","user123"));
1
2
3
4
5
6
7
8
9
10
11
12
13
// Perform 3 queries in a single API call:// - 1st query targets the `categories` index// - 2nd and 3rd queries target the `products` indexqueries:=[]search.IndexedQuery{search.NewIndexedQuery("categories",opt.Query("computer"),opt.HitsPerPage(3)),search.NewIndexedQuery("products",opt.Query("computer"),opt.HitsPerPage(3),opt.Filters("_tags:promotion")),search.NewIndexedQuery("products",opt.Query("computer"),opt.HitsPerPage(10)),}extraHeaders:=opt.ExtraHeaders(map[string]string{"X-Algolia-User-ID":"userID1",})res,err:=client.MultipleQueries(queries,"",extraHeaders)
// perform 3 queries in a single API call:// - 1st query targets index `categories`// - 2nd and 3rd queries target index `products`client.execute{multiQueries(searchinto"categories"queryQuery(query=myQueryString,hitsPerPage=Some(3)),searchinto"products"queryQuery(query=myQueryString,hitsPerPage=Some(3),filters=Some("_tags:promotion")),searchinto"products"queryQuery(query=myQueryString,hitsPerPage=Some(10)))strategyMultiQueries.Strategy.stopIfEnoughMatchesoptionsRequestOptions(extraHeaders=Some(Map("X-Algolia-User-ID"=>"user123")))}
none: Execute the sequence of queries until the end.
This is recommended when each query is of equal importance, meaning all
records of all queries need to be returned.
stopIfEnoughMatches: Execute queries one by one, but stop as soon
as the cumulated number of hits is at least hitsPerPage.
This is recommended when each query is an alternative,
and where, if the first returns enough records,
there is no need to perform the remaining queries.
requestOptions
type: key/value mapping
default: No request options
Optional
A mapping of request options to send along with the query.
queries ➔
query object
{
indexName: indexName,
query: "search query",
params: {
searchParameter1: value1, // one or several searchParameters
searchParameter2: value2
}
}
In this section we document the JSON response returned by the API.
Each language will encapsulate this response inside objects specific to the language and/or the implementation.
So the actual type in your language might differ from what is documented.