-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add fast client-side search to Airflow documentation #59658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84f4cb6
Add fast client-side search to Airflow documentation
kaxil 94716d5
Make results more accurate
kaxil 609494f
fixup! Make results more accurate
kaxil ca2b291
Replace sphinx search completely
kaxil eca9d4a
Add comment for uv pip check
kaxil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
|
|
||
| # Pagefind Search Extension | ||
|
|
||
| A Sphinx extension providing fast, self-hosted search for Apache Airflow documentation using [Pagefind](https://pagefind.app/). | ||
|
|
||
| ## Features | ||
|
|
||
| - **Automatic indexing** when docs are built | ||
| - **Cmd+K search** with modern UI and keyboard shortcuts | ||
| - **Self-hosted** - No third-party services | ||
| - **Content weighting** - Prioritizes titles and headings for better relevance | ||
| - **Optimized ranking** - Tuned for exact phrase matching and title matches | ||
| - **Playground support** - Debug and tune search behavior | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Search Interface | ||
|
|
||
| - **Keyboard**: Press `Cmd+K` (Mac) or `Ctrl+K` (Windows/Linux) | ||
| - **Mouse**: Click the search button in the header | ||
| - **Navigation**: Arrow keys to navigate, Enter to select, Esc to close | ||
|
|
||
| ## Configuration | ||
|
|
||
| In Sphinx's `conf.py`: | ||
|
|
||
| ```python | ||
| # Enable/disable search (default: True) | ||
| pagefind_enabled = True | ||
|
|
||
| # Verbose logging (default: False) | ||
| pagefind_verbose = False | ||
|
|
||
| # Content selector (default: "main") | ||
| pagefind_root_selector = "main" | ||
|
|
||
| # Exclude selectors (default: see below) | ||
| # These elements won't be included in the search index | ||
| pagefind_exclude_selectors = [ | ||
| ".headerlink", # Permalink icons | ||
| ".toctree-wrapper", # Table of contents navigation | ||
| "nav", # All navigation elements | ||
| "footer", # Footer content | ||
| ".td-sidebar", # Left sidebar | ||
| ".breadcrumb", # Breadcrumb navigation | ||
| ".navbar", # Top navigation bar | ||
| ".dropdown-menu", # Dropdown menus (version selector, etc.) | ||
| ".docs-version-selector", # Version selector widget | ||
| "[role='navigation']", # ARIA navigation landmarks | ||
| ".d-print-none", # Print-hidden elements (usually UI controls) | ||
| ".pagefind-search-button", # Search button itself | ||
| ] | ||
|
|
||
| # File pattern (default: "**/*.html") | ||
| pagefind_glob = "**/*.html" | ||
|
|
||
| # Exclude patterns (default: []) | ||
| # Path patterns to exclude from indexing (e.g., auto-generated API docs) | ||
| # Note: File-by-file indexing is used when patterns are specified (slightly slower but precise) | ||
| # Pagefind does NOT automatically exclude underscore-prefixed directories | ||
| pagefind_exclude_patterns = [ | ||
| "_api/**", # Exclude API documentation | ||
| "_modules/**", # Exclude source code modules | ||
| "release_notes.html", # Exclude specific files | ||
| "genindex.html", # Exclude generated index | ||
| ] | ||
|
|
||
| # Content weighting (default: True) | ||
| # Uses lightweight regex to add data-pagefind-weight attributes to titles and headings | ||
| pagefind_content_weighting = True | ||
|
|
||
| # Enable playground (default: False) | ||
| # Creates a playground at /_pagefind/playground/ for debugging search | ||
| pagefind_enable_playground = False | ||
|
|
||
| # Custom records for non-HTML content (default: []) | ||
| pagefind_custom_records = [ | ||
| { | ||
| "url": "/downloads/guide.pdf", | ||
| "content": "PDF content...", | ||
| "language": "en", | ||
| "meta": {"title": "Guide PDF"}, | ||
| } | ||
| ] | ||
| ``` | ||
|
|
||
| ### Ranking Optimization | ||
|
|
||
| The extension uses optimized ranking parameters in `search.js`: | ||
|
|
||
| - **termFrequency: 1.0** - Standard term occurrence weighting | ||
| - **termSaturation: 0.7** - Moderate saturation to prevent over-rewarding repetition | ||
| - **termSimilarity: 7.5** - Maximum boost for exact phrase matches and similar terms | ||
| - **pageLength: 0** - No penalty for longer pages (important for reference documentation) | ||
|
|
||
| Combined with content weighting (10x for titles, 9x for h1, 7x for h2), these settings ensure exact title matches rank highly even for very long pages. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### Build Process | ||
|
|
||
| 1. Sphinx builds HTML documentation | ||
| 2. Extension copies CSS/JS to `_static/` | ||
| 3. Extension injects search modal HTML into pages | ||
| 4. (Optional) Extension adds content weights to HTML files | ||
| 5. Extension builds Pagefind index with configured options | ||
|
|
||
| ### Runtime | ||
|
|
||
| 1. User presses Cmd+K or clicks search button | ||
| 2. JavaScript loads Pagefind library dynamically | ||
| 3. Search executes client-side | ||
| 4. Results rendered in modal | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Search not working | ||
|
|
||
| 1. Check Pagefind is installed: `python -c "import pagefind; print('OK')"` | ||
| 2. Check index exists: `ls generated/_build/docs/apache-airflow/stable/_pagefind/` | ||
| 3. Enable verbose logging: `pagefind_verbose = True` in `conf.py` | ||
|
|
||
| ### Index not created | ||
|
|
||
| - Ensure `pagefind_enabled = True` in `conf.py` | ||
| - Check build logs for errors | ||
| - If `pagefind[bin]` unavailable, use: `npx pagefind --site <build-dir>` | ||
|
|
||
| ### Poor search ranking | ||
|
|
||
| 1. Enable playground: `pagefind_enable_playground = True` in `conf.py` | ||
| 2. Rebuild docs and access playground at `/_pagefind/playground/` | ||
| 3. Test queries and analyze ranking scores | ||
| 4. Ensure `pagefind_content_weighting = True` (default) | ||
| 5. Check that titles and headings contain expected keywords | ||
|
|
||
| ### Debugging with Playground | ||
|
|
||
| The playground provides detailed insights: | ||
|
|
||
| - View all indexed pages | ||
| - See ranking scores for each result | ||
| - Analyze impact of different search terms | ||
| - Verify content weighting is applied | ||
| - Test ranking parameter changes | ||
|
|
||
| Example, access at: `http://localhost:8000/docs/apache-airflow/stable/_pagefind/playground/` |
103 changes: 103 additions & 0 deletions
103
devel-common/src/sphinx_exts/pagefind_search/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Sphinx extension for Pagefind search integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sphinx.application import Sphinx | ||
|
|
||
| from sphinx_exts.pagefind_search.builder import build_index_finished, copy_static_files | ||
|
|
||
|
|
||
| def register_templates(app: Sphinx, config) -> None: | ||
| """Register template directory for searchbox override.""" | ||
| template_dir = str(Path(__file__).parent / "templates") | ||
| # Prepend so our template overrides Sphinx's default searchbox | ||
| if template_dir not in config.templates_path: | ||
| config.templates_path.insert(0, template_dir) | ||
|
|
||
|
|
||
| def inject_search_html(app: Sphinx, pagename: str, templatename: str, context: dict, doctree) -> None: | ||
| """Inject Pagefind search modal HTML into page context.""" | ||
| template_dir = Path(__file__).parent / "templates" | ||
| modal_file = template_dir / "search-modal.html" | ||
|
|
||
| if not modal_file.exists(): | ||
| return | ||
|
|
||
| modal_content = modal_file.read_text() | ||
|
|
||
| from jinja2 import Template | ||
|
|
||
| modal_template = Template(modal_content) | ||
| search_modal_html = modal_template.render(pathto=context.get("pathto")) | ||
|
|
||
| context["pagefind_search_modal"] = search_modal_html | ||
|
|
||
| if "body" in context: | ||
| context["body"] = search_modal_html + context["body"] | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> dict[str, Any]: | ||
| """Setup the Pagefind search extension.""" | ||
| app.add_config_value("pagefind_enabled", True, "html", [bool]) | ||
| app.add_config_value("pagefind_verbose", False, "html", [bool]) | ||
| app.add_config_value("pagefind_root_selector", "main", "html", [str]) | ||
| app.add_config_value( | ||
| "pagefind_exclude_selectors", | ||
| [ | ||
| ".headerlink", | ||
| ".toctree-wrapper", | ||
| "nav", | ||
| "footer", | ||
| ".td-sidebar", | ||
| ".breadcrumb", | ||
| ".navbar", | ||
| ".dropdown-menu", | ||
| ".docs-version-selector", | ||
| "[role='navigation']", | ||
| ".d-print-none", | ||
| ".pagefind-search-button", | ||
| ], | ||
| "html", | ||
| [list], | ||
| ) | ||
| app.add_config_value("pagefind_glob", "**/*.html", "html", [str]) | ||
| app.add_config_value("pagefind_exclude_patterns", [], "html", [list]) | ||
| app.add_config_value("pagefind_custom_records", [], "html", [list]) | ||
| app.add_config_value("pagefind_content_weighting", True, "html", [bool]) | ||
| app.add_config_value("pagefind_enable_playground", False, "html", [bool]) | ||
|
|
||
| app.add_css_file("css/pagefind.css") | ||
| app.add_js_file("js/search.js") | ||
|
|
||
| # Register template directory for searchbox override | ||
| app.connect("config-inited", register_templates) | ||
| app.connect("html-page-context", inject_search_html) | ||
| app.connect("build-finished", copy_static_files, priority=100) | ||
| app.connect("build-finished", build_index_finished, priority=900) | ||
|
|
||
| return { | ||
| "version": "1.0.0", | ||
| "parallel_read_safe": True, | ||
| "parallel_write_safe": False, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.