references/saved-searches.md
3.6 KB查看排版效果| 1 | # Saved Searches |
| 2 | |
| 3 | ## Retrieving Saved Searches |
| 4 | |
| 5 | ```python |
| 6 | # Get all saved search metadata (not results) |
| 7 | searches = zot.everything(zot.searches()) |
| 8 | # Returns list of dicts with name, key, conditions, version |
| 9 | |
| 10 | for search in searches: |
| 11 | print(search['data']['name'], search['data']['key']) |
| 12 | ``` |
| 13 | |
| 14 | `searches()` returns metadata. The Web API does not execute saved searches. The current **local** API supports `/api/users/0/searches/{searchKey}/items` (or the group equivalent), but Pyzotero 1.15.2 has no dedicated wrapper for that route. |
| 15 | |
| 16 | ## Creating Saved Searches |
| 17 | |
| 18 | Each condition dict must have `condition`, `operator`, and `value`: |
| 19 | |
| 20 | ```python |
| 21 | conditions = [ |
| 22 | { |
| 23 | 'condition': 'title', |
| 24 | 'operator': 'contains', |
| 25 | 'value': 'machine learning' |
| 26 | } |
| 27 | ] |
| 28 | zot.saved_search('ML Papers', conditions) |
| 29 | ``` |
| 30 | |
| 31 | ### Multiple Conditions (AND logic) |
| 32 | |
| 33 | ```python |
| 34 | conditions = [ |
| 35 | {'condition': 'itemType', 'operator': 'is', 'value': 'journalArticle'}, |
| 36 | {'condition': 'tag', 'operator': 'is', 'value': 'unread'}, |
| 37 | {'condition': 'date', 'operator': 'isInTheLast', 'value': '3 years'}, |
| 38 | ] |
| 39 | zot.saved_search('Recent Unread Articles', conditions) |
| 40 | ``` |
| 41 | |
| 42 | ## Deleting Saved Searches |
| 43 | |
| 44 | ```python |
| 45 | # Get search keys first |
| 46 | searches = zot.everything(zot.searches()) |
| 47 | keys = [s['data']['key'] for s in searches if s['data']['name'] == 'Old Search'] |
| 48 | # The SDK's delete_saved_search() omits the required library-version |
| 49 | # precondition in 1.15.2. For REMOTE libraries, issue the documented request: |
| 50 | if keys: |
| 51 | if len(keys) > 50: |
| 52 | raise ValueError('Review and delete at most 50 search keys per request') |
| 53 | version = zot.last_modified_version() |
| 54 | response = zot.client.delete( |
| 55 | f'https://api.zotero.org/{zot.library_type}/{zot.library_id}/searches', |
| 56 | params={'searchKey': ','.join(keys)}, |
| 57 | headers={**zot.default_headers(), 'If-Unmodified-Since-Version': str(version)}, |
| 58 | ) |
| 59 | response.raise_for_status() |
| 60 | assert response.status_code == 204 |
| 61 | ``` |
| 62 | |
| 63 | ## Discovering Valid Operators and Conditions |
| 64 | |
| 65 | ```python |
| 66 | # All available operators |
| 67 | operators = zot.show_operators() |
| 68 | |
| 69 | # All available conditions |
| 70 | conditions = zot.show_conditions() |
| 71 | |
| 72 | # Operators valid for a specific condition |
| 73 | title_operators = zot.show_condition_operators('title') |
| 74 | # A set of supported operator strings in this SDK release. |
| 75 | ``` |
| 76 | |
| 77 | ## Common Condition/Operator Combinations |
| 78 | |
| 79 | | Condition | Common Operators | |
| 80 | |-----------|-----------------| |
| 81 | | `title` | `contains`, `doesNotContain`, `is`, `isNot` | |
| 82 | | `tag` | `is`, `isNot` | |
| 83 | | `itemType` | `is`, `isNot` | |
| 84 | | `date` | `isBefore`, `isInTheLast`, `is`, `isNot` | |
| 85 | | `creator` | `contains`, `is` | |
| 86 | | `publicationTitle` | `contains`, `is` | |
| 87 | | `year` | `is`, `isNot`, `contains` | |
| 88 | | `collection` | `is`, `isNot` | |
| 89 | | `fulltextContent` | `contains` | |
| 90 | |
| 91 | `show_operators()` is a mapping; `show_conditions()` is a keys view; `show_condition_operators()` returns a set. These use the SDK's bundled condition definitions, not live server discovery. Check `saved_search()`'s creation result for `failed`, as with `create_items()`. The direct remote delete above is illustrative and does not inherit Pyzotero's backoff wrapper; handle 429/503 and their `Retry-After` headers before another request. Never send it to the local API without local authorization and server-ID handling. |
| 92 | |
| 93 | Sources: [search writes](https://www.zotero.org/support/dev/web_api/v3/write_requests#search_requests), [local search execution](https://www.zotero.org/support/dev/web_api/v3/local_api). |
| 94 | |
| 95 | The SDK 1.15.2 condition table rejects `date/isAfter` despite listing `isAfter` in its global operators. Use a permitted condition/operator pair or an explicitly constructed direct API request after checking the server contract. |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

