Skip to content

Commit

Permalink
Fix search 'autocomplete' behaviour
Browse files Browse the repository at this point in the history
Search results will no longer disappear and reappear again as a user is
typing a word in.

Removing the stemmer from the search index pipeline stops the search
term from being truncated when a search is made.

This fixes alphagov#222.

Description of issue
--------------------

I noticed that certain searches have surprising behaviour as the search
term was typed in; for instance, when searching for "documentation":

- searching "d" gives 5 results
- searching "do" gives 0 results
- searching "doc", "docu", "docum", "docume", "documen", "document"
  gives 1 result
- searching "documenta", "documentat", "documentati", "documentatio"
  gives 0 results
- searching "documentation" gives 1 result

This is surprising because search is incremental, meaning the results
change a lot while typing a word.

It is could also be confusing when searching for a particular long word;
for instance, if someone was searching for references to "documentation"
and there are a lot of results for "document" but none for "documenta"
they might think that means they are no uses of the word "documentation"
in the website.

Fix
---

This problem had already been identified in the Design System website,
see commit alphagov/govuk-design-system@531d3c3. Thanks @36degrees for
flagging that this fix existed.

The fix is to remove the stemmer from the lunr pipeline. This is easily
done with the fork of [middleman-search] that the tech-docs-gem uses.

This commit also adds a simple test to prevent future changes breaking
this functionality.

[middleman-search]: https://github.com/alphagov/middleman-search
  • Loading branch information
lfdebrux committed Sep 6, 2021
1 parent 40a8821 commit 1bd57e6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [#236: Fix search 'autocomplete' behaviour](https://github.com/alphagov/tech-docs-gem/pull/236)

## 2.4.2

- [#251 Fix missing `<ul>` in single page navigation](https://github.com/alphagov/tech-docs-gem/pull/251)
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_tech_docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def active_page(page_path)
url: { index: false, store: true },
}

search.pipeline_remove = %w[stopWordFilter]
search.pipeline_remove = %w[stemmer stopWordFilter]

search.tokenizer_separator = '/[\s\-/]+/'
end
Expand Down
8 changes: 8 additions & 0 deletions spec/javascripts/search-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ describe('Search', function () {
]
expect(searchResults).toEqual(expectedResults)
})

it('autocompletes words', function () {
module.search('documenta', function (incomplete) {
module.search('documentation', function (complete) {
expect(incomplete).toEqual(complete)
})
})
})
})

describe('the processContent method', function () {
Expand Down

0 comments on commit 1bd57e6

Please sign in to comment.