Add checks to updating filterable attributes to avoid hitting weird edge cases

This commit is contained in:
Camryn Thomas 2024-03-12 16:08:47 -05:00
parent 0f2c19e811
commit a63faba2aa
Signed by: cptlobster
GPG Key ID: 6D341D688163A176

View File

@ -39,7 +39,8 @@ class JukeboxSearch:
# make a variable to easily reference the index # make a variable to easily reference the index
self.idxref = self.client.index(self.index) self.idxref = self.client.index(self.index)
self.idxref.update_filterable_attributes(filterable_attrs) # update filterable attributes if needed
self.update_filterables(filterable_attrs)
def add_document(self, document: dict) -> TaskInfo: def add_document(self, document: dict) -> TaskInfo:
"""Add a cable to the Meilisearch index. """Add a cable to the Meilisearch index.
@ -57,6 +58,18 @@ class JukeboxSearch:
for i in documents: for i in documents:
taskinfo = self.add_document(i) taskinfo = self.add_document(i)
return taskinfo return taskinfo
def update_filterables(self, filterables: list):
"""Update filterable attributes and wait for database to fully index. If the filterable attributes matches the
current attributes in the database, don't update (saves reindexing).
:param filterables: List of all filterable attributes"""
existing_filterables = self.idxref.get_filterable_attributes()
if len(set(existing_filterables).difference(set(filterables))) > 0:
taskref = self.idxref.update_filterable_attributes(filterables)
self.client.wait_for_task(taskref.index_uid)
def search(self, query: str, filters: str = None): def search(self, query: str, filters: str = None):
"""Execute a search query on the Meilisearch index. """Execute a search query on the Meilisearch index.
@ -98,3 +111,7 @@ class JukeboxSearch:
:param partnum: The part number to search for.""" :param partnum: The part number to search for."""
return self._filter_one(f"partnum = {partnum}") return self._filter_one(f"partnum = {partnum}")
# entrypoint
if __name__ == "__main__":
jbs = JukeboxSearch()