🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Methods / Search

Search multiple indices

Required API Key: any key with the search ACL
Method signature
$client->multipleQueries(array queries)

$client->multipleQueries(array queries, [
  // All the following parameters are optional
  'strategy' => string,
  // + any requestOptions
])

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

About this method

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:

  1. 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).
  2. 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 use this method for a search query or facet search by specifying the type parameter to be “default” or “facet”.

You can only use up to 50 queries in a multiple queries request.

Examples

Read the Algolia CLI documentation for more information.

Multiple queries

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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']);

Multiple queries and send extra HTTP headers

1
2
3
4
5
6
7
$queries = [/* queries */];

$results = $client->multipleQueries($queries, [
  'X-Forwarded-For' => '94.228.178.246'
]);

var_dump($results['results']);

Parameters

Parameter Description
queries
type: list of query object
Required

A list of queries to execute.

strategy
type: string|enum
default: none
Optional

The strategy of the query.

Can be one of the following values:

  • 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 requestOptions to send along with the query.

queries ➔ query object

{
  indexName: "indexName",
  type: "default", // optional
  query: "search query",
  params: {
    searchParameter1: value1, // one or several search parameters
    searchParameter2: value2
  }
}
Parameter Description
indexName
type: string
Required

Name of the index to target.

query
type: string
Required

The query to search in the index.

type
type: string
default: default
Optional

The type of query to perform. Can be either default for a regular search, or facet for a facet value search.

params
type: key/value mapping
default: No search parameters
Optional

A key/value mapping of any search parameters

Response

This section shows the JSON response returned by the API. Each API client encapsulates this response inside objects specific to the programming language, so that the actual response might be different. You can view the response by using the getLogs method. Don’t rely on the order of attributes in the response, as JSON doesn’t guarantee the ordering of keys in objects.

JSON format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{
  "results": [
    {
      "hits": [
        {
          "firstname": "Jimmie",
          "lastname": "Barninger",
          "objectID": "433",
          "_highlightResult": {
            "firstname": {
              "value": "<em>Jimmie</em>",
              "matchLevel": "partial"
            },
            "lastname": {
              "value": "Barninger",
              "matchLevel": "none"
            },
            "company": {
              "value": "California <em>Paint</em> & Wlpaper Str",
              "matchLevel": "partial"
            }
          }
        }
        ],
        "page": 0,
        "nbHits": 1,
        "nbPages": 1,
        "hitsPerPage": 20,
        "processingTimeMS": 1,
        "query": "jimmie paint",
        "params": "query=jimmie+paint&attributesToRetrieve=firstname,lastname&hitsPerPage=50"
        "index": "people"
    },
    {
      "hits": [
        {
          "firstname": "Jimmie",
          "lastname": "Barninger",
          "objectID": "433",
          "_highlightResult": {
            "firstname": {
              "value": "<em>Jimmie</em>",
              "matchLevel": "partial"
            },
            "lastname": {
              "value": "Barninger",
              "matchLevel": "none"
            },
            "company": {
              "value": "California <em>Paint</em> & Wlpaper Str",
              "matchLevel": "partial"
            }
          }
        }
        ],
        "page": 0,
        "nbHits": 1,
        "nbPages": 1,
        "hitsPerPage": 20,
        "processingTimeMS": 1,
        "query": "jimmie paint",
        "params": "query=jimmie+paint&attributesToRetrieve=firstname,lastname&hitsPerPage=50"
        "index": "famous_people"
    }
  ]
}
Field Description
results
list of result

List of result in the order they were submitted, one element for each query.

{
  "results": [
    {
      [...],
      index: 'indexName'
      processed: true
    },
    [...]
  ]
}

results ➔ result

The results object contains the hits object, and is therefore the same as the hits of the search method.

Each result also includes the following additional fields:

Field Description
index
string

The name of the targeted index.

processed
boolean

Whether the query was processed. Only returned when strategy = stopIfEnoughmatches.

Did you find this page helpful?