Slackbot
09/16/2023, 8:57 PMJason Bosco
09/16/2023, 8:58 PMEd
09/16/2023, 8:59 PMEd
09/16/2023, 8:59 PMEd
09/16/2023, 9:00 PMEd
09/16/2023, 9:01 PMdef upload_and_delete_typesense():
client = create_client()
# new_collection_name = "amso_zh"
for lang in fe_languages:
new_collection_name = f"tke_{lang}"
upsert_docs, ids = import_and_upsert_documents(
client, f"algolia_jobs-{lang}.json", new_collection_name
)
deleted = delete_document(client, new_collection_name, ids)
pprint(deleted)Ed
09/16/2023, 9:06 PMEd
09/16/2023, 9:08 PMEd
09/16/2023, 9:21 PMdef import_and_upsert_documents(client, json_file, collection_name):
# Open and read the JSON data from the file
with open(json_file) as f:
json_data = json.load(f)
# get all 'id' fields
ids = [
doc["data"]["idClient"] for doc in json_data
] # we are doing this here since we already opened the file
# Convert list of JSON objects to JSONL format
jsonl_data = "\n".join(json.dumps(doc) for doc in json_data)
# Import documents into the collection
upsert = client.collections[collection_name].documents.import_(
jsonl_data.encode("utf-8"), {"action": "upsert"}
)
return upsert, idsJason Bosco
09/16/2023, 10:39 PMEd
09/16/2023, 10:50 PMJason Bosco
09/16/2023, 10:51 PMEd
09/16/2023, 10:57 PMEd
09/16/2023, 10:57 PM