Skip to content

Commit 5263457

Browse files
committed
Show more key operations.
1 parent 6aeb803 commit 5263457

File tree

1 file changed

+75
-2
lines changed

1 file changed

+75
-2
lines changed

examples/key_operations.py

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,88 @@
1010
'connection_timeout_seconds': 2
1111
})
1212

13+
client.collections['key_collection'].delete()
14+
15+
# Create a collection with admin key
16+
create_response = client.collections.create({
17+
"name": "key_collection",
18+
"fields": [
19+
{"name": "title", "type": "string"},
20+
{"name": "ratings_count", "type": "int32"},
21+
],
22+
"default_sorting_field": "ratings_count"
23+
})
24+
25+
# Add a document
26+
document = {
27+
'id': '1',
28+
'title': 'The Hunger Games',
29+
'ratings_count': 200
30+
}
31+
client.collections['key_collection'].documents.create(document)
32+
1333
# Create a new key
14-
key = client.keys.create({"description": "Search-only key.", "actions": ["documents:search"], "collections": ["*"]})
34+
key = client.keys.create({"description": "Search-only key.", "actions": ["documents:search"], "collections": ["key_collection"]})
1535
print(key)
1636

1737
# Try to fetch it back
1838
print(client.keys[key['id']].retrieve())
1939

40+
# Create a search client using this key
41+
search_client = typesense.Client({
42+
'api_key': key['value'],
43+
'nodes': [{
44+
'host': 'localhost',
45+
'port': '8108',
46+
'protocol': 'http'
47+
}],
48+
'connection_timeout_seconds': 2
49+
})
50+
51+
# Try to create a collection from a new client using this search key (should fail)
52+
try:
53+
create_response = search_client.collections.create({
54+
"name": "key_collection2",
55+
"fields": [
56+
{"name": "title", "type": "string"},
57+
{"name": "ratings_count", "type": "int32"},
58+
],
59+
"default_sorting_field": "ratings_count"
60+
})
61+
62+
print(create_response)
63+
except Exception as e:
64+
print(e)
65+
2066
# Generate scoped search key
21-
print(client.keys.generate_scoped_search_key(key['value'], {"filter_by": "user_id:1080"}))
67+
scoped_search_key = client.keys.generate_scoped_search_key(key['value'], {"filter_by": "ratings_count:<100"})
68+
print(scoped_search_key)
69+
70+
# Search collection
71+
print(search_client.collections['key_collection'].documents.search({
72+
'q': 'hunger',
73+
'query_by': 'title',
74+
'sort_by': 'ratings_count:desc'
75+
}))
76+
77+
# Create a scoped client and search with that
78+
scoped_search_client = typesense.Client({
79+
'api_key': scoped_search_key,
80+
'nodes': [{
81+
'host': 'localhost',
82+
'port': '8108',
83+
'protocol': 'http'
84+
}],
85+
'connection_timeout_seconds': 2
86+
})
87+
88+
# Search again, will be restricted to `ratings_count:<100` so no record will be returned
89+
print(scoped_search_client.collections['key_collection'].documents.search({
90+
'q': 'hunger',
91+
'query_by': 'title',
92+
'filter_by': 'ratings_count:>100', # trying to override the embedded filter
93+
'sort_by': 'ratings_count:desc'
94+
}))
2295

2396
# Delete the key
2497
print(client.keys[key['id']].delete())

0 commit comments

Comments
 (0)