跳到主要内容

Zotero 文献库

收藏0

用 pyzotero 程序化管理 Zotero 文献库:检索条目、新建与更新条目、整理分类与标签、导出引用格式、上传 PDF 附件,支持 Web API 与本地 API。

让 AI 安装

星光尘缘LV2最新版本 0.1.0更新于 2026-11-24下载 0 次

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)
7searches = zot.everything(zot.searches())
8# Returns list of dicts with name, key, conditions, version
9
10for 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
18Each condition dict must have `condition`, `operator`, and `value`:
19
20```python
21conditions = [
22 {
23 'condition': 'title',
24 'operator': 'contains',
25 'value': 'machine learning'
26 }
27]
28zot.saved_search('ML Papers', conditions)
29```
30
31### Multiple Conditions (AND logic)
32
33```python
34conditions = [
35 {'condition': 'itemType', 'operator': 'is', 'value': 'journalArticle'},
36 {'condition': 'tag', 'operator': 'is', 'value': 'unread'},
37 {'condition': 'date', 'operator': 'isInTheLast', 'value': '3 years'},
38]
39zot.saved_search('Recent Unread Articles', conditions)
40```
41
42## Deleting Saved Searches
43
44```python
45# Get search keys first
46searches = zot.everything(zot.searches())
47keys = [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:
50if 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
67operators = zot.show_operators()
68
69# All available conditions
70conditions = zot.show_conditions()
71
72# Operators valid for a specific condition
73title_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
93Sources: [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
95The 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.

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。