Skip to content

Commit

Permalink
Better indicators search (#1593)
Browse files Browse the repository at this point in the history
<!--Please ensure the PR fulfills the following requirements! -->
<!-- If this is your first PR, make sure to add your details to the
AUTHORS.rst! -->
### Pull Request Checklist:
- [x] This PR addresses an already opened issue (for bug fixes /
features)
    - This PR fixes #1559
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] (If applicable) Documentation has been added / updated (for bug
fixes / features)
- [x] CHANGES.rst has been updated (with summary of main changes)
- [x] Link to issue (:issue:`number`) and pull request (:pull:`number`)
has been added

### What kind of change does this PR introduce?

* Exclude virtual modules by default (anuclim, icclim, cf), add checkbox
to include them
* Even when virtual modules are included, boost "official" indicators in
the results.

I bumped the MiniSearch version to be able to use the "wildcard" option,
which cover the case where no search query is given. This way, the
boosting official indicators is done even with an empty query.

### Does this PR introduce a breaking change?
No.

### Other information:
  • Loading branch information
aulemahal authored Jan 15, 2024
2 parents bcec43e + bbaa657 commit 7b36f2c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Internal changes
* The GitHub Workflows now use the `step-security/harden-runner` action to monitor source code, actions, and dependency safety. All workflows now employ more constrained permissions rule sets to prevent security issues. (:pull:`1577`, :pull:`1578`).
* Updated the CONTRIBUTING.rst directions to showcase the new versioning system. (:issue:`1557`, :pull:`1573`).
* The `codespell` library is now a development dependency for the `dev` installation recipe with configurations found within `pyproject.toml`. This is also now a linting step and integrated as a `pre-commit` hook. For more information, see the `codespell documentation <https://github.com/codespell-project/codespell>`_ (:pull:`1576`).
* Climate indicators search page now prioritizes the "official" indicators (atmos, land, seaIce and generic), virtual submodules can be added to search through checkbox option. (:issue:`1559`, :pull:`1593`).


v0.47.0 (2023-12-01)
Expand Down
20 changes: 16 additions & 4 deletions docs/_static/indsearch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Array of indicator objects */
let indicators = [];

let defModules = ["atmos", "generic", "land", "seaIce"];
/* MiniSearch object defining search mechanism */
let miniSearch = new MiniSearch({
fields: ['title', 'abstract', 'variables', 'keywords', 'id'], // fields to index for full-text search
Expand All @@ -9,6 +9,13 @@ let miniSearch = new MiniSearch({
boost: {'title': 3, 'variables': 2},
fuzzy: 0.1,
prefix: true,
boostDocument: (docID, term, storedFields) => {
if (defModules.indexOf(storedFields['module']) > -1) {
return 2;
} else {
return 1;
}
},
},
extractField: (doc, field) => {
if (field === 'variables') {
Expand Down Expand Up @@ -87,11 +94,16 @@ function indTemplate(ind) {

function indFilter() {
const input = document.getElementById("queryInput").value;
const incVirt = document.getElementById("incVirtMod").checked;
let opts = {};
if (!incVirt) {
opts["filter"] = (result) => (defModules.indexOf(result.module) > -1);
}
let inds = [];
if (input === "") {
inds = indicators;
if (input === "") { //Search wildcard so that boostDocument rules are applied.
inds = miniSearch.search(MiniSearch.wildcard, opts);
} else {
inds = miniSearch.search(input);
inds = miniSearch.search(input, opts);
}

const newTable = inds.map(indTemplate).join('');
Expand Down
4 changes: 4 additions & 0 deletions docs/_static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ code > .indName {
margin: 5px;
background-color: #ddd;
}

#incVirtModLbl {
display: inline;
}
2 changes: 1 addition & 1 deletion docs/_templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{% block scripts %}
{% if "indicators" in sourcename %}
<script src="https://cdn.jsdelivr.net/npm/minisearch@6.1.0/dist/umd/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/minisearch@6.3.0/dist/umd/index.min.js"></script>
<script type="text/javascript" src="./_static/indsearch.js"></script>
{% endif %}
{{ super() }}
Expand Down
2 changes: 1 addition & 1 deletion docs/indicators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ allows a simple free text search of all indicators. Click on the python names to
.. raw:: html

<input type="text" id="queryInput" onkeyup="indFilter()" placeholder="Search for titles, variables or keywords...">

<input type="checkbox" id="incVirtMod" onchange="indFilter()"><label id="incVirtModLbl" for="virtualModules">Include virtual submodules in results.</label>
<div id="indTable">
</div>

Expand Down

0 comments on commit 7b36f2c

Please sign in to comment.