From 9bba9ee8d268adc8fc5b05ca81f6217188b2a913 Mon Sep 17 00:00:00 2001 From: Katrina Prosise Date: Wed, 17 Jul 2024 08:09:58 -0400 Subject: [PATCH] Add meilisearch for docs search Added search functionality. Two api keys are exposed, with limited scope. The search api, which is normal and required for basic search functionality. An additional key allows for getting the list of indexes. This allows for checking if an index matching the release exists. If no index exists, the default doc search is used. Known issues: as a limit to the number of indexes/releases will eventually present itself, older indexes will need to be deleted. As Sphinx is static, this presents a bit of an issue. However, once version selection is implemented, older versions will be rebuilt, meaning older releases with deleted search indexes will fallback to using the default Sphinx search. Additionally, local/PR CI builds will use a "dev" index. This will eventually be configured to use the most recent commit, however for now the index was created by scrapping "latest". Note this commit does NOT cover the creation of indexes, which at this time will be handled manually, and requires private keys. QA: Checked rendered output, and tested a number of cases by modifying the variables used. This commit addresses task FFTK-3215 This commit applies to task FFTK-3091 Signed-off-by: Katrina Prosise --- requirements.txt | 2 ++ source/_templates/searchbox.html | 33 ++++++++++++++++++++++++ source/conf.py | 44 +++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 source/_templates/searchbox.html diff --git a/requirements.txt b/requirements.txt index f94057bfd..d54c2e8ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,5 @@ sphinx_copybutton==0.5.2 sphinx_toolbox==3.5.0 sphinx-prompt==1.5.0 sphinx-reredirects +sphinx-autobuild +meilisearch diff --git a/source/_templates/searchbox.html b/source/_templates/searchbox.html new file mode 100644 index 000000000..8572e9ffb --- /dev/null +++ b/source/_templates/searchbox.html @@ -0,0 +1,33 @@ +{% if search_version == 'default' %} + +
+
+ + + +
+
+ +{% else %} + + + + + + + + +{% endif %} diff --git a/source/conf.py b/source/conf.py index f1de6b8a6..501d4e4fe 100644 --- a/source/conf.py +++ b/source/conf.py @@ -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 @@ -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.