Skip to content

Commit eb2a470

Browse files
Add new guide on /similar route (#3173)
--------- Co-authored-by: Louis Dureuil <[email protected]>
1 parent 1a02920 commit eb2a470

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

.code-samples.meilisearch.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,39 @@ search_parameter_reference_retrieve_vectors_1: |-
14231423
"embedder": "EMBEDDER_NAME"
14241424
}
14251425
}'
1426+
related_results_embedder_1: |-
1427+
curl -X PATCH 'MEILISEARCH_URL/indexes/movies/settings'
1428+
-H 'Content-Type: application/json'
1429+
-H 'Authorization: Bearer MEILISEARCH_API_KEY'
1430+
--data-binary '{
1431+
"embedders": {
1432+
"movies-text": {
1433+
"source": "openAi",
1434+
"apiKey": "OPENAI_API_KEY",
1435+
"model": "text-embedding-3-small",
1436+
"documentTemplate": "A movie titled '{{doc.title}}' released in {{ doc.release_date }}. The movie genres are: {{doc.genres}}. The story is about: {{doc.overview|truncatewords: 20}}"
1437+
}
1438+
}
1439+
}'
1440+
related_results_search_1: |-
1441+
curl -X POST 'MEILISEARCH_URL/indexes/INDEX_NAME/search' \
1442+
-H 'content-type: application/json' \
1443+
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
1444+
--data-binary '{
1445+
"q": "batman",
1446+
"hybrid": {
1447+
"embedder": "EMBEDDER_NAME"
1448+
}
1449+
}'
1450+
related_results_similar_1: |-
1451+
curl \
1452+
-X POST 'MEILISEARCH_URL/indexes/movies/similar' \
1453+
-H 'Content-Type: application/json' \
1454+
-H 'Authorization: Bearer DEFAULT_SEARCH_API_KEY' \
1455+
--data-binary '{
1456+
"id": 192,
1457+
"embedder": "EMBEDDER_NAME"
1458+
}'
14261459
14271460
### Code samples for experimental features
14281461
experimental_get_metrics_1: |-
Loading

config/sidebar-learn.json

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"label": "Configure a REST embedder",
5555
"slug": "configure_rest_embedder"
5656
},
57+
{
58+
"source": "learn/ai_powered_search/retrieve_related_search_results.mdx",
59+
"label": "Retrieve related search results",
60+
"slug": "retrieve_related_search_results"
61+
},
5762
{
5863
"source": "learn/ai_powered_search/document_template_best_practices.mdx",
5964
"label": "Document template best practices",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Retrieve related search results
3+
description: This guide shows you how to use the similar documents endpoint to create an AI-powered movie recommendation workflow.
4+
---
5+
6+
This guide shows you how to use the [similar documents endpoint](/reference/api/similar) to create an AI-powered movie recommendation workflow.
7+
8+
First, you will create an embedder and add documents to your index. You will then perform a search, and use the top result's primary key to retrieve similar movies in your database.
9+
10+
## Prerequisites
11+
12+
- A running Meilisearch project
13+
- A [tier >=2](https://platform.openai.com/docs/guides/rate-limits#usage-tiers) OpenAI API key
14+
15+
## Create a new index
16+
17+
Create an index called `movies` and add this <ButtonLink as="a" id="downloadMovie" href="/movies.json" download="movies.json">`movies.json`</ButtonLink> dataset to it. If necessary, consult the [getting started](/learn/getting_started/cloud_quick_start) for more instructions on index creation.
18+
19+
Each document in the dataset represents a single movie and has the following structure:
20+
21+
- `id`: a unique identifier for each document in the database
22+
- `title`: the title of the movie
23+
- `overview`: a brief summary of the movie's plot
24+
- `genres`: an array of genres associated with the movie
25+
- `poster`: a URL to the movie's poster image
26+
- `release_date`: the release date of the movie, represented as a Unix timestamp
27+
28+
## Configure an embedder
29+
30+
Next, use the Cloud UI to configure an OpenAI embedder:
31+
32+
![Animated image of the Meilisearch Cloud UI showing a user clicking on "add embedder". This opens up a modal window, where the user fills in the name of the embedder, chooses OpenAI as its source. They then select a model, input their API key, and type out a document template.](/assets/images/similar-guide/01-add-embedder-ui.gif)
33+
34+
You may also use the `/settings/embedders` API subroute to configure your embedder:
35+
36+
<CodeSamples id="related_results_embedder_1" />
37+
38+
Replace `MEILISEARCH_URL`, `MEILISEARCH_API_KEY`, and `OPENAI_API_KEY` with the corresponding values in your application.
39+
40+
Meilisearch will start generating the embeddings for all movies in your dataset. Use the returned `taskUid` to [track the progress of this task](/learn/async/asynchronous_operations). Once it is finished, you are ready to start searching.
41+
42+
## Perform a hybrid search
43+
44+
With your documents added and all embeddings generated, you can perform a search:
45+
46+
<CodeSamples id="related_results_search_1" />
47+
48+
This request returns a list of movies. Pick the top result and take note of its primary key in the `id` field. In this case, it's the movie "Batman" with `id` 192.
49+
50+
## Return similar documents
51+
52+
Pass "Batman"'s `id` to your index's [`/similar` route](/reference/api/similar), specifying `movies-text` as your embedder:
53+
54+
<CodeSamples id="related_results_similar_1" />
55+
56+
Meilisearch will return a list of the 20 documents most similar to the movie you chose. You may then choose to display some of these similar results to your users, pointing them to other movies that may also interest them.
57+
58+
## Conclusion
59+
60+
Congratulations! You have successfully built an AI-powered movie search and recommendation system using Meilisearch by:
61+
62+
- Setting up a Meilisearch project and configured it for AI-powered search
63+
- Implementing hybrid search combining keyword and semantic search capabilities
64+
- Integrating Meilisearch's similarity search for movie recommendations
65+
66+
In a real-life application, you would now start integrating this workflow into a front end, like the one in this [official Meilisearch blog post](https://www.meilisearch.com/blog/add-ai-powered-search-to-react).

reference/api/similar.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Retrieve documents similar to a specific search result.
2626
| Parameter | Type | Default value | Description |
2727
| ---------------------------------------------------------------------------- | ---------------- | ------------- | ---------------------------------------------- |
2828
| **`id`** | String or number | `null` | Identifier of the target document (mandatory) |
29-
| **[`embedder`](/reference/api/search#hybrid-search)** | String | `"default"` | Embedder to use when computing recommendations. Mandatory |
29+
| **[`embedder`](/reference/api/search#hybrid-search)** | String | `null` | Embedder to use when computing recommendations. Mandatory |
3030
| **[`attributesToRetrieve`](/reference/api/search#attributes-to-retrieve)** | Array of strings | `["*"]` | Attributes to display in the returned documents|
3131
| **[`offset`](/reference/api/search#offset)** | Integer | `0` | Number of documents to skip |
3232
| **[`limit`](/reference/api/search#limit)** | Integer | `20` | Maximum number of documents returned |

0 commit comments

Comments
 (0)