Multi-collection indexes
Overview
A multi-collection index is an index which includes documents from more than one collection.
| Indexes which cover multiple collections may be less performant than those which cover a single index. If possible, it’s a better practice to organize your collections and queries so that multi-collection indexes are not necessary. |
Example
For demonstration purposes, let’s create two collections named fruit
and flowers.
Collections created!
The following example adds some documents to the fruit collection:
Fruit items created!
Now let’s add some documents to the flowers collection:
Flower items created!
To make these two collections searchable by their color field, we can
create an index which specifies both collections in the source field
and color in the terms field.
{
ref: Index("fruit_and_flowers_search_by_color"),
ts: 1642632647330000,
active: false,
serialized: true,
name: "fruit_and_flowers_search_by_color",
source: [Collection("fruit"), Collection("flowers")],
terms: [
{
field: ["data", "color"]
}
],
partitions: 1
}
The following example searches the index fruit_and_flowers_search_by_color
for documents with the string red in the color field.
{
data: [
{
ref: Ref(Collection("flowers"), "1"),
ts: 1648232012790000,
data: { type: 'rose', color: 'red' }
},
{
ref: Ref(Collection("fruit"), "1"),
ts: 1648232012610000,
data: { type: 'apple', color: 'red' }
},
{
ref: Ref(Collection("flowers"), "3"),
ts: 1648232012790000,
data: { type: 'carnation', color: 'red' }
}
]
}