|
| 1 | +--- |
| 2 | +title: Document template best practices — Meilisearch documentation |
| 3 | +description: This guide shows you what to do and what to avoid when writing a `documentTemplate`. |
| 4 | +--- |
| 5 | + |
| 6 | +# Document template best practices |
| 7 | + |
| 8 | +When using AI-powered search, Meilisearch generates prompts by filling in your embedder's `documentTemplate` with each document's data. The better your prompt is, the more relevant your search results. |
| 9 | + |
| 10 | +This guide shows you what to do and what to avoid when writing a `documentTemplate`. |
| 11 | + |
| 12 | +## Sample document |
| 13 | + |
| 14 | +Take a look at this document from a database of movies: |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "id": 2, |
| 19 | + "title": "Ariel", |
| 20 | + "overview": "Taisto Kasurinen is a Finnish coal miner whose father has just committed suicide and who is framed for a crime he did not commit. In jail, he starts to dream about leaving the country and starting a new life. He escapes from prison but things don't go as planned...", |
| 21 | + "genres": [ |
| 22 | + "Drama", |
| 23 | + "Crime", |
| 24 | + "Comedy" |
| 25 | + ], |
| 26 | + "poster": "https://image.tmdb.org/t/p/w500/ojDg0PGvs6R9xYFodRct2kdI6wC.jpg", |
| 27 | + "release_date": 593395200 |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Do not use the default `documentTemplate` |
| 32 | + |
| 33 | +Use a custom `documentTemplate` value in your embedder configuration. |
| 34 | + |
| 35 | +The default `documentTemplate` includes all searchable fields with non-`null` values. In most cases, this adds noise and more information than the embedder needs to provide relevant search results. |
| 36 | + |
| 37 | +## Only include highly relevant information |
| 38 | + |
| 39 | +Take a look at your document and identify the most relevant fields. A good `documentTemplate` for the sample document could be: |
| 40 | + |
| 41 | +``` |
| 42 | +"A movie called {{doc.title}} about {{doc.overview}}" |
| 43 | +``` |
| 44 | + |
| 45 | +In the sample document, `poster` and `id` contain data that has little semantic importance and can be safely excluded. The data in `genres` and `release_date` is very useful for filters, but say little about this specific film. |
| 46 | + |
| 47 | +This leaves two relevant fields: `title` and `overview`. |
| 48 | + |
| 49 | +## Keep prompts short |
| 50 | + |
| 51 | +For the best results, keep prompts somewhere between 15 and 45 words: |
| 52 | + |
| 53 | +``` |
| 54 | +"A movie called {{doc.title}} about {{doc.overview | truncatewords: 20}}" |
| 55 | +``` |
| 56 | + |
| 57 | +In the sample document, the `overview` alone is 49 words. Use Liquid's [`truncate`](https://shopify.github.io/liquid/filters/truncate/) or [`truncatewords`](https://shopify.github.io/liquid/filters/truncatewords/) to shorten it. |
| 58 | + |
| 59 | +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. |
| 60 | + |
| 61 | +## Conclusion |
| 62 | + |
| 63 | +In this article you saw the main steps to generating prompts that lead to relevant AI-powered search results: |
| 64 | + |
| 65 | +- Do not use the default `documentTemplate` |
| 66 | +- Only include relevant data |
| 67 | +- Truncate long fields |
0 commit comments