FEATURE: hidden site setting to disable search prefix matching (#20058)

Many users seems surprised by prefix matching in search leading to
unexpected results.

Over the years we always would return results starting with a search term
and not expect exact matches.

Meaning a search for `abra` would find `abracadabra`

This introduces the Site Setting `enable_search_prefix_matching` which
defaults to true. (behavior unchanged)

We plan to experiment on select sites with exact matches to see if the
results are less surprising
This commit is contained in:
Sam 2023-01-30 15:44:40 +11:00 committed by GitHub
parent db5ad34508
commit 64f7b97d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -2192,6 +2192,9 @@ backups:
client: true
search:
enable_search_prefix_matching:
default: true
hidden: true
use_pg_headlines_for_excerpt:
default: false
hidden: true

View File

@ -1237,7 +1237,8 @@ class Search
end
def self.set_tsquery_weight_filter(term, weight_filter)
"'#{self.escape_string(term)}':*#{weight_filter}"
optional_star = SiteSetting.enable_search_prefix_matching ? "*" : ""
"'#{self.escape_string(term)}':#{optional_star}#{weight_filter}"
end
def self.escape_string(term)

View File

@ -2580,4 +2580,25 @@ RSpec.describe Search do
expect(result.categories.length).to eq(0)
end
end
context "when enable_search_prefix_matching is disabled" do
before do
SearchIndexer.enable
SiteSetting.enable_search_prefix_matching = false
end
fab!(:post) do
Fabricate(:post, topic: topic, raw: "this body of the post contains abracadabra")
end
it "omits prefix search results" do
SearchIndexer.index(post, force: true)
result = Search.execute("abra")
expect(result.posts.length).to eq(0)
result = Search.execute("abracadabra")
expect(result.posts.length).to eq(1)
end
end
end