|
10 | 10 | 'connection_timeout_seconds': 2 |
11 | 11 | }) |
12 | 12 |
|
| 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 | + |
13 | 33 | # 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"]}) |
15 | 35 | print(key) |
16 | 36 |
|
17 | 37 | # Try to fetch it back |
18 | 38 | print(client.keys[key['id']].retrieve()) |
19 | 39 |
|
| 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 | + |
20 | 66 | # 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 | +})) |
22 | 95 |
|
23 | 96 | # Delete the key |
24 | 97 | print(client.keys[key['id']].delete()) |
|
0 commit comments