fix error when index does not exist

This commit is contained in:
Camryn Thomas 2024-03-01 20:37:22 -06:00
parent 6edd0b4ef0
commit 4561b1c1a3
Signed by: cptlobster
GPG Key ID: 6D341D688163A176

View File

@ -1,6 +1,7 @@
"""Interactions with the Meilisearch API for adding and searching cables.""" """Interactions with the Meilisearch API for adding and searching cables."""
from meilisearch import Client from meilisearch import Client
from meilisearch.task import TaskInfo from meilisearch.task import TaskInfo
from meilisearch.errors import MeilisearchApiError
import json import json
DEFAULT_URL = "http://localhost:7700" DEFAULT_URL = "http://localhost:7700"
@ -24,7 +25,9 @@ class JukeboxSearch:
self.index = index or DEFAULT_INDEX self.index = index or DEFAULT_INDEX
self.client = Client(url, api_key) self.client = Client(url, api_key)
# create the index if it does not exist already # create the index if it does not exist already
if self.client.get_index(self.index) is None: try:
self.client.get_index(self.index)
except MeilisearchApiError as _:
self.client.create_index(self.index) self.client.create_index(self.index)
def add_document(self, document: dict) -> TaskInfo: def add_document(self, document: dict) -> TaskInfo: