Part 2- Optimizing the Algolia record size
In Part 1 of this example, we showed you how to create a Dynamic Content Algolia webhook integration that creates a record in Algolia when a content item is created in Dynamic Content.
Algolia recommends that, for performance reasons, the maximum size of a record should not exceed 5KB. In this part we build on the webhook created in Part 1 and explain how you can refine the custom payload to optimize the size of the record created in Algolia by including only those properties that you want to be searchable.
In Part 3 we explain how you can index large content items by splitting the output into multiple records.
PrerequisitesLink copied!
This example uses the configuration and content type schemas included in Part 1, but the concepts will be the same whatever schemas you are using in your own projects.
Step 1: The Algolia record from Part 1Link copied!
If you worked through the steps included in Part 1, you will have a webhook integration that defines a custom payload to create a record in an Algolia index containing the entire content item that was created or updated in Dynamic Content.
For a blog content item the Algolia record will look something like that shown below.
By looking at the record structure in Algolia you can begin to see which fields you want to index. You can then include these fields in your custom payload.
Step 2: Add the updated custom payloadLink copied!
The following custom payload can be used to create a record for each blog article, including the information we want to be indexable: title, description, author names, tags, date, readTime and the content itself. Your payload should include all the fields that are used in your search configuration: searchAttributes, attributesForFaceting and customRanking, together with any fields that you need to display your search frontend. For the blog post content, you might choose to include just an excerpt to prevent your record from becoming too large.
If you're using the blog content type from Part 1, paste the handlebars shown above into your webhook's custom payload and save the webhook.
Some points to note about the custom payload:
- deliveryKey is a friendlier way of referring to the delivery key than _meta.deliveryKey
- in the custom payload we iterate through authors and create authors.name to use with
attributesForFaceting
- dateAsTimeStamp contains the date converted from a string to unix timestamp format so that it can be used for sorting. To sort using dateAsTimeStamp you can update your custom ranking as follows:
See the configuring your search indexes for more information about updating your search configuration when using Dynamic Content search.
- The image endpoint and name are concatenated to make path. This doesn't include mediaHost as this should be an environment variable in your app.
After updating your custom payload, if you have changed the names of any attributes, or added new ones, you may need to update your search settings. If you are using Dynamic Content search see search configuration, otherwise see the Algolia dashboard for these settings.
Step 3: Trigger the webhook and view the activity logLink copied!
The next step is to trigger the webhook in order to view the webhook body created from the custom payload.
- Create or update a blog content item to trigger the webhook.
- Open the webhook activity log and view the most recent webhook delivery.
The webhook delivery details should look something the following image and include the request body that was generated from the custom payload.
If you view the Algolia record it will now contain just the fields that you specified in the custom payload.
When to use the Algolia batch APILink copied!
The custom payload shown in this example can be used as long each blog article is not too long, but there may be cases where it exceeds the recommended maximum size of 5KB. In this case we recommend breaking the blog, or other large content items, into multiple records and using the Algolia batch API. This is covered in Part 3.
Related pagesLink copied!
Example: Search integration (Algolia), Part 1
Example: Search integration (Algolia), Part 3