diff --git a/search.py b/search.py index 88aef38..6e6a720 100644 --- a/search.py +++ b/search.py @@ -39,7 +39,8 @@ class JukeboxSearch: # make a variable to easily reference the 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: """Add a cable to the Meilisearch index. @@ -57,6 +58,18 @@ class JukeboxSearch: for i in documents: taskinfo = self.add_document(i) 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): """Execute a search query on the Meilisearch index. @@ -98,3 +111,7 @@ class JukeboxSearch: :param partnum: The part number to search for.""" return self._filter_one(f"partnum = {partnum}") + +# entrypoint +if __name__ == "__main__": + jbs = JukeboxSearch() \ No newline at end of file