Skip to content

Commit e4eabbc

Browse files
authored
Add section about using if guards in documentTemplate guide (#3347)
1 parent 3f909f6 commit e4eabbc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

learn/ai_powered_search/document_template_best_practices.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,25 @@ In the sample document, the `overview` alone is 49 words. Use Liquid's [`truncat
5656

5757
Short prompts do not have enough information for the embedder to properly understand the query context. Long prompts instead provide too much information and make it hard for the embedder to identify what is truly relevant about a document.
5858

59+
## Add guards for missing fields
60+
61+
Some documents might not contain all the fields you expect. If your template directly references a missing field, Meilisearch will throw an error when indexing documents.
62+
63+
To prevent this, use Liquid’s `if` statements to add guards around fields:
64+
65+
```
66+
{% if doc.title %}
67+
A movie called {{ doc.title }}
68+
{% endif %}
69+
```
70+
71+
This ensures the template only tries to include data that already exists in a document. If a field is missing, the embedder still receives a valid and useful prompt without errors.
72+
5973
## Conclusion
6074

6175
In this article you saw the main steps to generating prompts that lead to relevant AI-powered search results:
6276

6377
- Do not use the default `documentTemplate`
6478
- Only include relevant data
6579
- Truncate long fields
80+
- Add guards for missing fields

0 commit comments

Comments
 (0)