Skip to content

Instantly share code, notes, and snippets.

@alterEgo123
Created June 30, 2021 13:43
Show Gist options
  • Save alterEgo123/f2d01264b905d84a23a6f8593360eb7d to your computer and use it in GitHub Desktop.
Save alterEgo123/f2d01264b905d84a23a6f8593360eb7d to your computer and use it in GitHub Desktop.
Importing data to Typesense
import typesense
def init_client():
client = typesense.Client({
'nodes': [{
'host': 'localhost',
'port': '8108',
'protocol': 'http'
}],
'api_key': 'xyz',
# 'connection_timeout_seconds': 2
})
return client
def indexed_fields():
return {
'name': 'places',
'fields': [
{
'name': 'name',
'type': 'string'
},
{
'name': 'price',
'type': 'float'
}
],
'default_sorting_field': 'price'
}
def fill_collection(client, name, jsonl_path):
with open(jsonl_path, encoding='utf-8') as jsonl_file:
client.collections[name].documents.import_(jsonl_file.read(), {'action': 'create'})
if __name__ == '__main__':
client = init_client()
collection = "places"
collections = client.collections.retrieve()
for c in collections:
if c['name'] == collection:
break
else:
r = client.collections.create(indexed_fields())
fill_collection(client, collection, "./place.jsonl")
# search_parameters = {
# 'q': 'mo',
# 'query_by': 'name',
#
# }
#
# print(client.collections[collection].retrieve())
{ "_id": "5fae94adf56f4d5ed91240f6", "uuid": "7C442692-25BA-11EB-B464-BC5FF4BFAA62", "name": "تونس", "price": 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment