add search functions to JukeboxSearch
This commit is contained in:
		| @@ -235,7 +235,7 @@ def flatten(tables): | |||||||
|                 print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",") |                 print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",") | ||||||
|  |  | ||||||
|             # if the item has at least two commas in it, split it |             # if the item has at least two commas in it, split it | ||||||
|             if tables[table][key].count(',') >= 2: |             if tables[table][key].count(',') > 0: | ||||||
|                 out[fullkeyname] = list(map(lambda x: x.strip(), tables[table][key].split(","))) |                 out[fullkeyname] = list(map(lambda x: x.strip(), tables[table][key].split(","))) | ||||||
|                 print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",") |                 print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",") | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										60
									
								
								search.py
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								search.py
									
									
									
									
									
								
							| @@ -7,21 +7,28 @@ import json | |||||||
| DEFAULT_URL = "http://localhost:7700" | DEFAULT_URL = "http://localhost:7700" | ||||||
| DEFAULT_APIKEY = "fluffybunnyrabbit" # I WOULD RECOMMEND SOMETHING MORE SECURE | DEFAULT_APIKEY = "fluffybunnyrabbit" # I WOULD RECOMMEND SOMETHING MORE SECURE | ||||||
| DEFAULT_INDEX = "cables" | DEFAULT_INDEX = "cables" | ||||||
|  | DEFAULT_FILTERABLE_ATTRS = ["partnum", "uuid", "position"] # default filterable attributes | ||||||
|  |  | ||||||
|  |  | ||||||
| class JukeboxSearch: | class JukeboxSearch: | ||||||
|     """Class for interacting with the Meilisearch API.""" |     """Class for interacting with the Meilisearch API.""" | ||||||
|     def __init__(self, url: str = None, api_key: str = None, index: str = None): |     def __init__(self, | ||||||
|  |                  url: str = None, | ||||||
|  |                  api_key: str = None, | ||||||
|  |                  index: str = None, | ||||||
|  |                  filterable_attrs: list = None): | ||||||
|         """Connect to Meilisearch and perform first-run tasks as necessary. |         """Connect to Meilisearch and perform first-run tasks as necessary. | ||||||
|  |  | ||||||
|         :param url: Address of the Meilisearch server. Defaults to ``http://localhost:7700`` if unspecified. |         :param url: Address of the Meilisearch server. Defaults to ``http://localhost:7700`` if unspecified. | ||||||
|         :param api_key: API key used to authenticate with Meilisearch. It is highly recommended to set this as something |         :param api_key: API key used to authenticate with Meilisearch. It is highly recommended to set this as something | ||||||
|         secure if you can access this endpoint publicly, but you can ignore this and set Meilisearch's default API key |         secure if you can access this endpoint publicly, but you can ignore this and set Meilisearch's default API key | ||||||
|         to ``fluffybunnyrabbit``. |         to ``fluffybunnyrabbit``. | ||||||
|         :param index: The name of the index to configure. Defaults to ``cables`` if unspecified.""" |         :param index: The name of the index to configure. Defaults to ``cables`` if unspecified. | ||||||
|  |         :param filterable_attrs: List of all the attributes we want to filter by.""" | ||||||
|         # connect to Meilisearch |         # connect to Meilisearch | ||||||
|         url = url or DEFAULT_URL |         url = url or DEFAULT_URL | ||||||
|         api_key = api_key or DEFAULT_APIKEY |         api_key = api_key or DEFAULT_APIKEY | ||||||
|  |         filterable_attrs = filterable_attrs or DEFAULT_FILTERABLE_ATTRS | ||||||
|         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 | ||||||
| @@ -29,13 +36,17 @@ class JukeboxSearch: | |||||||
|             self.client.get_index(self.index) |             self.client.get_index(self.index) | ||||||
|         except MeilisearchApiError as _: |         except MeilisearchApiError as _: | ||||||
|             self.client.create_index(self.index) |             self.client.create_index(self.index) | ||||||
|  |         # make a variable to easily reference the index | ||||||
|  |         self.idxref = self.client.index(self.index) | ||||||
|  |  | ||||||
|  |         self.idxref.update_filterable_attributes(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. | ||||||
|  |  | ||||||
|         :param document: Dictionary containing all the cable data. |         :param document: Dictionary containing all the cable data. | ||||||
|         :returns: A TaskInfo object for the addition of the new document.""" |         :returns: A TaskInfo object for the addition of the new document.""" | ||||||
|         return self.client.index(self.index).add_documents(document) |         return self.idxref.add_documents(document) | ||||||
|  |  | ||||||
|     def add_documents(self, documents: list): |     def add_documents(self, documents: list): | ||||||
|         """Add a list of cables to the Meilisearch index. |         """Add a list of cables to the Meilisearch index. | ||||||
| @@ -47,6 +58,43 @@ class JukeboxSearch: | |||||||
|             taskinfo = self.add_document(i) |             taskinfo = self.add_document(i) | ||||||
|         return taskinfo |         return taskinfo | ||||||
|  |  | ||||||
|     def query(self): |     def search(self, query: str, filters: str = None): | ||||||
|         """Execute a search query on the Meilisearch index.""" |         """Execute a search query on the Meilisearch index. | ||||||
|         pass |          | ||||||
|  |         :param query: Seach query | ||||||
|  |         :param filters: A meilisearch compatible filter statement. | ||||||
|  |         :returns: The search results dict. Actual results are in a list under "hits", but there are other nice values that are useful in the root element.""" | ||||||
|  |         if filters: | ||||||
|  |             q = self.idxref.search(query, {"filter": filters}) | ||||||
|  |         else: | ||||||
|  |             q = self.idxref.search(query) | ||||||
|  |         return q | ||||||
|  |      | ||||||
|  |     def _filter_one(self, filter: str): | ||||||
|  |         """Get the first item to match a filter. | ||||||
|  |          | ||||||
|  |         :param filter: A meilisearch compatible filter statement. | ||||||
|  |         :returns: A dict containing the results; If no results found, an empty dict.""" | ||||||
|  |         q = self.search("", filter) | ||||||
|  |         if q["estimatedTotalHits"] != 0: | ||||||
|  |             return ["hits"][0] | ||||||
|  |         else: | ||||||
|  |             return dict() | ||||||
|  |  | ||||||
|  |     def get_position(self, position: str): | ||||||
|  |         """Get a part by position. | ||||||
|  |          | ||||||
|  |         :param partnum: The position to search for.""" | ||||||
|  |         return self._filter_one(f"position = {position}") | ||||||
|  |      | ||||||
|  |     def get_uuid(self, uuid: str): | ||||||
|  |         """Get a specific UUID. | ||||||
|  |          | ||||||
|  |         :param uuid: The UUID to search for.""" | ||||||
|  |         return self._filter_one(f"uuid = {uuid}") | ||||||
|  |  | ||||||
|  |     def get_partnum(self, partnum: str): | ||||||
|  |         """Get a specific part number. | ||||||
|  |          | ||||||
|  |         :param partnum: The part number to search for.""" | ||||||
|  |         return self._filter_one(f"partnum = {partnum}") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user