Merge remote-tracking branch 'origin/dthomas_meilisearch' into dthomas_meilisearch
This commit is contained in:
commit
0f2c19e811
@ -235,8 +235,8 @@ def flatten(tables):
|
||||
print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",")
|
||||
|
||||
# if the item has at least two commas in it, split it
|
||||
if tables[table][key].count(',') >= 2:
|
||||
out[fullkeyname] = map(lambda x: x.strip(), tables[table][key].split(","))
|
||||
if tables[table][key].count(',') > 0:
|
||||
out[fullkeyname] = list(map(lambda x: x.strip(), tables[table][key].split(",")))
|
||||
print("\"" + keyname + "\":", "\"" + str(out[fullkeyname]) + "\",")
|
||||
|
||||
|
||||
|
65
search.py
65
search.py
@ -1,38 +1,52 @@
|
||||
"""Interactions with the Meilisearch API for adding and searching cables."""
|
||||
from meilisearch import Client
|
||||
from meilisearch.task import TaskInfo
|
||||
from meilisearch.errors import MeilisearchApiError
|
||||
import json
|
||||
|
||||
DEFAULT_URL = "http://localhost:7700"
|
||||
DEFAULT_APIKEY = "fluffybunnyrabbit" # I WOULD RECOMMEND SOMETHING MORE SECURE
|
||||
DEFAULT_INDEX = "cables"
|
||||
DEFAULT_FILTERABLE_ATTRS = ["partnum", "uuid", "position"] # default filterable attributes
|
||||
|
||||
|
||||
class JukeboxSearch:
|
||||
"""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.
|
||||
|
||||
: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
|
||||
secure if you can access this endpoint publicly, but you can ignore this and set Meilisearch's default API key
|
||||
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
|
||||
url = url or DEFAULT_URL
|
||||
api_key = api_key or DEFAULT_APIKEY
|
||||
filterable_attrs = filterable_attrs or DEFAULT_FILTERABLE_ATTRS
|
||||
self.index = index or DEFAULT_INDEX
|
||||
self.client = Client(url, api_key)
|
||||
# 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)
|
||||
# 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:
|
||||
"""Add a cable to the Meilisearch index.
|
||||
|
||||
:param document: Dictionary containing all the cable data.
|
||||
: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):
|
||||
"""Add a list of cables to the Meilisearch index.
|
||||
@ -44,6 +58,43 @@ class JukeboxSearch:
|
||||
taskinfo = self.add_document(i)
|
||||
return taskinfo
|
||||
|
||||
def query(self):
|
||||
"""Execute a search query on the Meilisearch index."""
|
||||
pass
|
||||
def search(self, query: str, filters: str = None):
|
||||
"""Execute a search query on the Meilisearch index.
|
||||
|
||||
: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}")
|
||||
|
Loading…
x
Reference in New Issue
Block a user