From 063666326ef69f1f1136ec3b3c91abc855a2ad86 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Wed, 26 Oct 2022 16:42:14 +1300 Subject: [PATCH] Finish filter-only feature by fixing empty excerpts --- pagefind/features/filtering.feature | 22 ++++++++++++++++++++++ pagefind_web/src/lib.rs | 2 +- pagefind_web/src/search.rs | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pagefind/features/filtering.feature b/pagefind/features/filtering.feature index 8ed946e8..3d5ffc42 100644 --- a/pagefind/features/filtering.feature +++ b/pagefind/features/filtering.feature @@ -159,6 +159,28 @@ Feature: Filtering Then There should be no logs Then The selector "[data-results]" should contain "/cheeka/, /theodore/" + Scenario: Filtering without search term returns an unprocessed excerpt + When I evaluate: + """ + async function() { + let pagefind = await import("/_pagefind/pagefind.js"); + + // Run a search so that some index words are loaded + let unused = await pagefind.search("cat"); + + let search = await pagefind.search(null, { + filters: { + color: ["Black", "White"] + } + }); + let data = await Promise.all(search.results.map(result => result.data())); + + document.querySelector('[data-results]').innerText = data.map(d => d.excerpt).join(', '); + } + """ + Then There should be no logs + Then The selector "[data-results]" should contain "Black White Cat." + @skip # Currently only an AND filtering is supported. Need to restructure to support boolean logic Scenario: Filtering to multiple values diff --git a/pagefind_web/src/lib.rs b/pagefind_web/src/lib.rs index 04b880cc..5ea42cc5 100644 --- a/pagefind_web/src/lib.rs +++ b/pagefind_web/src/lib.rs @@ -120,7 +120,7 @@ pub fn add_synthetic_filter(ptr: *mut SearchIndex, filter: &str) -> *mut SearchI #[wasm_bindgen] pub fn request_indexes(ptr: *mut SearchIndex, query: &str) -> String { let indexes = try_request_indexes(ptr, query, false); - if indexes.is_empty() { + if indexes.is_empty() && !query.trim().is_empty() { debug!({ "No index chunks found with strict boundaries. Loading all possible extension chunks." }); diff --git a/pagefind_web/src/search.rs b/pagefind_web/src/search.rs index 34431255..02793905 100644 --- a/pagefind_web/src/search.rs +++ b/pagefind_web/src/search.rs @@ -210,6 +210,9 @@ impl SearchIndex { } fn stems_from_term(term: &str) -> Vec> { + if term.trim().is_empty() { + return vec![]; + } let stemmer = Stemmer::try_create_default(); term.split(' ') .map(|word| match &stemmer {