Skip to content
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

Add meilisearch for docs search #726

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ sphinx_copybutton==0.5.2
sphinx_toolbox==3.5.0
sphinx-prompt==1.5.0
sphinx-reredirects
sphinx-autobuild
meilisearch
33 changes: 33 additions & 0 deletions source/_templates/searchbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% if search_version == 'default' %}

<div role="search">
<form id="rtd-search-form" class="wy-form" action="{{ pathto('search') }}" method="get">
<input type="text" name="q" placeholder="{{ _('Using builtin search') }}" aria-label="{{ _('Search docs') }}" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>

{% else %}

<div id="search"></div>

<script src="https://unpkg.com/meilisearch-docsearch@latest/dist/index.global.js"></script>
<script>

const { docsearch } = window.__docsearch_meilisearch__;
docsearch({
container: "#search",
host: "{{ meilisearch_host }}",
apiKey: "{{ meilisearch_search_key }}",
indexUid: "{{ search_version }}",
});

</script>

<link
rel="stylesheet"
href="https://unpkg.com/meilisearch-docsearch@latest/dist/index.css"
/>

{% endif %}
44 changes: 43 additions & 1 deletion source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,46 @@
# (This doesn't affect links to artifacts.)
mp_tags = ''

# -- Search Configuration ------------------------------------------------

meilisearch_index_key = os.environ.get('MEILISEARCH_INDEX_KEY')
meilisearch_search_key = os.environ.get('MEILISEARCH_SEARCH_KEY')
meilisearch_host = os.environ.get('MEILISEARCH_HOST_URL')

if not meilisearch_index_key or not meilisearch_host or not meilisearch_search_key:
search_version = 'default'

else:
if mp_version.startswith('git-'):
search_version = 'dev'

else:

import meilisearch

# Using Api key restricted to get/indexes, then seeing if the index exists.
# This means an empty index must be created prior to deploying a new version.
# If no index exists, the default Sphinx search will be used.
client = meilisearch.Client(meilisearch_host, meilisearch_index_key)
results = client.get_raw_indexes()

# A very un-python loop, but it works
i=0
index_name = 'default'
while i < len(results["results"]) and index_name != mp_version:
index_name = results["results"][i]["uid"]
i += 1
if index_name == mp_version:
search_version = mp_version
else:
search_version = 'default'

# pass to template engine's context
html_context = {'search_version': search_version,
'meilisearch_host': meilisearch_host,
'meilisearch_search_key': meilisearch_search_key,
}

# -- General configuration ------------------------------------------------

# Derive the subscriber tags to use for this build from the
Expand Down Expand Up @@ -285,7 +325,9 @@
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
html_sidebars = {
'**': ['searchbox.html']
}

# Additional templates that should be rendered to pages, maps page names to
# template names.
Expand Down
Loading