Skip to content

Commit

Permalink
Merge pull request #118 from CloudCannon/fix/filtering-on-no-results
Browse files Browse the repository at this point in the history
Fix filters causing results to return for invalid search terms
  • Loading branch information
bglw authored Oct 26, 2022
2 parents ca55910 + 2c4441e commit 086f22b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 19 additions & 1 deletion pagefind/features/filtering.feature
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,22 @@ Feature: Filtering
}
"""
Then There should be no logs
Then The selector "[data-results]" should contain "0"
Then The selector "[data-results]" should contain "0"

Scenario: Filtering on a search term with no results returns nothing
When I evaluate:
"""
async function() {
let pagefind = await import("/_pagefind/pagefind.js");
let search = await pagefind.search("Pontification", {
filters: {
color: "Orange"
}
});
document.querySelector('[data-results]').innerText = search.results.length;
}
"""
Then There should be no logs
Then The selector "[data-results]" should contain "0"
9 changes: 8 additions & 1 deletion pagefind_web/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ impl SearchIndex {
let mut maps = Vec::new();
let mut unique_maps = Vec::new();
let mut words = Vec::new();
for term in stems_from_term(term) {
let split_term = stems_from_term(term);

for term in split_term.iter() {
let mut word_maps = Vec::new();
for (word, word_index) in self.find_word_extensions(&term) {
words.extend(word_index);
Expand All @@ -119,6 +121,11 @@ impl SearchIndex {
maps.push(result);
}
}
// In the case where a search term was passed, but not found,
// make sure we cause the entire search to return no results.
if !split_term.is_empty() && maps.is_empty() {
maps.push(BitSet::new());
}

if let Some(filter) = filter_results {
maps.push(filter);
Expand Down

0 comments on commit 086f22b

Please sign in to comment.