Guides / Sending and managing data / Prepare your records for indexing

Creating and using nested attributes

With nested attributes, you can add subcategories to your attributes. For example, instead of having a single attribute price, you might set up different prices as subcategories: price.net, price.gross, price.margin. Use ‘dot notation’ to separate the parent attribute from its child.

How to create nested attributes

You can create nested attributes by adding a JSON object to your records when you send them to Algolia. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[
  {
    "country": "CA",
    "price": {
      "net": 2.30,
      "gross": 2.62
    }
  },
  {
    "country": "US",
    "price": {
      "net": 1.99,
      "gross": 1.75
    }
  }
]

You can’t add nested attributes to searchable attributes in the dashboard. Use the setSettings method on an API client or the Algolia CLI algolia settings set command instead.

An example of filtering nested attributes with an 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);
});

An example of filtering nested attributes with the Algolia CLI

1
algolia search --indexName=your_index_name --filters='country:US AND price.gross < 2.0'

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.

Did you find this page helpful?