Skip to content

Commit 6f09f21

Browse files
sanders41alallema
authored andcommitted
Add vector search
1 parent 103f31d commit 6f09f21

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

meilisearch/index.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def get_stats(self) -> IndexStats:
243243
stats = self.http.get(f"{self.config.paths.index}/{self.uid}/{self.config.paths.stat}")
244244
return IndexStats(stats)
245245

246+
@version_error_hint_message
246247
def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
247248
"""Search in the index.
248249
@@ -251,7 +252,13 @@ def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dic
251252
query:
252253
String containing the searched word(s)
253254
opt_params (optional):
254-
Dictionary containing optional query parameters
255+
Dictionary containing optional query parameters.
256+
Note: The vector parameter is only available in Meilisearch >= v1.3.0, and is experimental
257+
Meilisearch v1.3.0. In order to use this feature in Meilisearch v1.3.0 you first need to
258+
enable the feature by sending a PATCH request to /experimental-features with
259+
{ "vectoreStore": true }. Because this feature is experimental it may be removed or
260+
updated causing breaking changes in this library without a major version bump so use
261+
with caution.
255262
https://www.meilisearch.com/docs/reference/api/search#search-in-an-index
256263
257264
Returns

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from typing import Optional
44

5+
import requests
56
from pytest import fixture
67

78
import meilisearch
@@ -126,6 +127,19 @@ def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
126127
return index_maker
127128

128129

130+
@fixture(scope="function")
131+
def index_with_documents_and_vectors(empty_index, small_movies):
132+
small_movies[0]["_vectors"] = [0.1, 0.2]
133+
134+
def index_maker(index_uid=common.INDEX_UID, documents=small_movies):
135+
index = empty_index(index_uid)
136+
task = index.add_documents(documents)
137+
index.wait_for_task(task.task_uid)
138+
return index
139+
140+
return index_maker
141+
142+
129143
@fixture(scope="function")
130144
def test_key(client):
131145
key_info = {
@@ -172,3 +186,18 @@ def get_private_key(client):
172186
keys = client.get_keys().results
173187
key = next(x for x in keys if "Default Search API" in x.name)
174188
return key
189+
190+
191+
@fixture
192+
def enable_vector_search():
193+
requests.patch(
194+
f"{common.BASE_URL}/experimental-features",
195+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
196+
json={"vectorStore": True},
197+
)
198+
yield
199+
requests.patch(
200+
f"{common.BASE_URL}/experimental-features",
201+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
202+
json={"vectorStore": False},
203+
)

tests/index/test_index_search_meilisearch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pylint: disable=invalid-name
22

3+
import pytest
4+
35

46
def test_basic_search(index_with_documents):
57
"""Tests search with a simple query."""
@@ -455,3 +457,13 @@ def test_attributes_to_search_on_search_no_match(index_with_documents):
455457
"How to Train Your Dragon", opt_params={"attributesToSearchOn": ["id"]}
456458
)
457459
assert response["hits"] == []
460+
461+
462+
@pytest.mark.usefixtures("enable_vector_search")
463+
def test_vector_search(index_with_documents_and_vectors):
464+
response = index_with_documents_and_vectors().search(
465+
"How to Train Your Dragon", opt_params={"vector": [0.1, 0.2]}
466+
)
467+
468+
assert response["hits"][0]["id"] == "287947"
469+
assert response["vector"] == [0.1, 0.2]

0 commit comments

Comments
 (0)