Create nested attributes
With nested attributes, you can add subcategories to your attributes.
For example, instead of having a single attribute price
,
you can set up different prices as subcategories: price.net
, price.gross
, price.margin
.
To refer to nested attributes, separate parent and child attribute with a .
.
To create nested attributes, add them as nested JSON objects to your records. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"country": "CA",
// price is a nested attribute
"price": {
"net": 2.30,
"gross": 2.62
}
},
{
"country": "US",
// price is a nested attribute
"price": {
"net": 1.99,
"gross": 1.75
}
}
]
Example: filtering nested attributes
If you want to filter a search using nested attributes,
use the filters
parameter in your search request
and separate the parent and child attribute by a .
.
For example, with the JavaScript API client:
1
2
3
4
5
6
7
const index = client.initIndex('your_index_name');
index.search('', {
filters: 'country:US AND price.gross < 2.0'
}).then(({ hits }) => {
console.log(hits);
});
Where you can use nested attributes
You can use nested attributes wherever you might use a regular attribute, such as searchableAttributes
and attributesForFaceting
.
Just make sure you use the appropriate dot notation such as price.country
to refer to them.
There’s no limit on the number of nested attributes apart from the default restrictions on record size.
The depth of nesting is also unlimited: you could use something like price.net.us.ca
.