API Reference / API Methods / Search / Search multiple indices

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
])

About this method

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

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

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 only use up to 50 queries in a multiple queries request.

Examples

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

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
  }
}
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

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.

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"
    }
  ]
}
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:

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?