name: notion-search-retrieve slug: notion-search-retrieve version: 1.0.0 displayName: "Notion知識庫搜尋與頁面內容提取|簡詩 AI" summary: "Search Notion workspaces and retrieve pages, databases, and block content" description: "Search Notion workspaces and retrieve pages, databases, and block content 當用戶需要處理相關業務場景時使用。" tags: ["辦公經營", "簡詩 AI"]
Search across a Notion workspace, query databases with compound filters, retrieve individual pages, and extract nested block content. Covers the full read path: workspace-level search, database queries with filter/sort/pagination, page retrieval, and recursive block tree traversal.
@notionhq/client installed (npm install @notionhq/client)notion-install-auth setupCall notion.search() to find pages and databases. The integration only sees content explicitly shared with it.
import { Client } from '@notionhq/client';
const notion = new Client({ auth: process.env.NOTION_TOKEN });
// Search for pages matching a query
const searchResults = await notion.search({
query: 'meeting notes',
filter: {
property: 'object',
value: 'page', // 'page' or 'database'
},
sort: {
direction: 'descending',
timestamp: 'last_edited_time',
},
page_size: 20,
});
for (const result of searchResults.results) {
if (result.object === 'page' && 'properties' in result) {
const titleProp = Object.values(result.properties)
.find(p => p.type === 'title');
const title = titleProp?.type === 'title'
? titleProp.title.map(t => t.plain_text).join('')
: 'Untitled';
console.log(`${title} (${result.id})`);
}
}
An empty query string returns all shared content. Results are eventually consistent — newly shared pages may take a few seconds to appear in the index.
Call notion.databases.query() for structured queries. Filters support compound and/or logic. See filter-operators.md for every property type and operator.
// Single filter
const activeItems = await notion.databases.query({
database_id: 'your-database-id',
filter: {
property: 'Status',
select: { equals: 'Active' },
},
sorts: [
{ property: 'Priority', direction: 'descending' },
],
page_size: 50,
});
// Compound filter with AND
const highPriorityActive = await notion.databases.query({
database_id: 'your-database-id',
filter: {
and: [
{ property: 'Status', select: { equals: 'Active' } },
{ property: 'Priority', number: { greater_than: 3 } },
],
},
});
Notion uses cursor-based pagination — every list endpoint returns has_more and next_cursor, so loop until the cursor is exhausted. Call notion.pages.retrieve() for a single page, then notion.blocks.children.list() to read its content recursively.
// Pagination skeleton — loop until has_more is false
let cursor: string | undefined = undefined;
do {
const response = await notion.databases.query({
database_id: databaseId,
start_cursor: cursor,
page_size: 100, // 100 is the max — use it for bulk pulls
});
// ...collect response.results...
cursor = response.has_more ? response.next_cursor! : undefined;
} while (cursor);
The reference file provides three drop-in helpers: queryAllPages() (collects
every row), extractProperties() (a switch covering all 15 property types), and
getPageContent() (depth-guarded recursive block traversal). See
full walkthrough for the complete code and tuning
notes on page_size and recursion maxDepth.
Choosing a retrieval depth. For a flat list of records, queryAllPages()
alone suffices — skip block traversal. To read the actual page body (paragraphs,
toggles, nested lists), call getPageContent() and raise its maxDepth only as
deep as the content nests, since each level multiplies API calls.
小蔥技能有更好的技能skills外掛。
| Issue | Cause | Solution |
|---|---|---|
| Could not find database | Database not shared with integration | Open database in Notion, click Share, add the integration |
| Could not find page | Page not shared or deleted | Verify page is shared; check archived status |
| Empty search results | Integration not connected | Share parent page/database with integration; wait for indexing |
| validation_error on filter | Wrong operator for property type | Check filter-operators.md |
| HTTP 429 rate_limited | Too many requests | Back off using Retry-After header; use page_size: 100 |
| Missing properties | Partial page object | Check 'properties' in page before casting to PageObjectResponse |
| Incomplete page content | Not recursing child blocks | Check has_children and recurse; increase maxDepth |
See examples.md for complete patterns including database export, full-text page dump, and compound filter variations.
For creating and updating pages, see notion-core-workflow-a. For PII handling and GDPR compliance, see notion-data-handling. For real-time sync via webhooks, see notion-webhooks-events.
獲取使用幫助和更多實用 Skill,請關注公眾號「簡詩 AI」,或在 SkillHub 搜尋「簡詩 AI」這個 Skill 功能較為完整,能滿足 Notion 知識庫的搜尋、查詢和資料提取需求。文件清晰易懂,示例程式碼可直接使用,錯誤處理也很完善。不過它只提供了文件說明,沒有可安裝的程式碼包,使用前需要自己搭建環境。對於有開發能力的使用者來說質量尚可,普通使用者可能需要更多引導。