Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make teacher search work on word prefixes #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class Teacher < ApplicationRecord
teacher_reference_number: true

# Scopes
scope :search, ->(query_string) { where("teachers.search @@ websearch_to_tsquery('unaccented', ?)", query_string) }
scope :search, lambda { |query_string|
where(
"teachers.search @@ to_tsquery('unaccented', ?)",
FullTextSearch::Query.new(query_string).search_by_all_prefixes
)
}

def to_param
trn
Expand Down
19 changes: 19 additions & 0 deletions lib/full_text_search/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module FullTextSearch
class Query
def initialize(string)
@string = string
end

# splits a string and adds the suffix ':*', which allows
# us to search for partial words, so 'Jo' would match 'John'
# and 'Joey'
#
# the segments are joined with '&' so 'Jo Sm' would return
# 'John Smith' but not 'Joan Jones'
#
# https://www.postgresql.org/docs/current/datatype-textsearch.html#DATATYPE-TSQUERY
def search_by_all_prefixes
@string.split.map { |w| w + ":*" }.join(" & ")
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/full_text_search/query_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe FullTextSearch::Query do
subject { FullTextSearch::Query.new(string).search_by_all_prefixes }

context 'when the string has no words' do
let(:string) { '' }

it { is_expected.to be_empty }
end

context 'when the string has one word (Cletus)' do
let(:string) { 'Cletus' }

it { is_expected.to eql('Cletus:*') }
end

context 'when the string has many words (Cletus Van Damme)' do
let(:string) { 'Cletus Van Damme' }

it { is_expected.to eql('Cletus:* & Van:* & Damme:*') }
end
end
32 changes: 24 additions & 8 deletions spec/models/teacher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
describe 'scopes' do
describe '#search' do
it "searches the 'search' column using a tsquery" do
expect(Teacher.search('Joey').to_sql).to end_with(%{WHERE (teachers.search @@ websearch_to_tsquery('unaccented', 'Joey'))})
expect(Teacher.search('Joey').to_sql).to end_with(%{WHERE (teachers.search @@ to_tsquery('unaccented', 'Joey:*'))})
end

describe 'basic matching' do
Expand All @@ -53,13 +53,6 @@
expect(results).to include(target)
expect(results).not_to include(other)
end

it "supports web search syntax" do
results = Teacher.search('Wilkerson -Reese')

expect(results).to include(target)
expect(results).not_to include(other)
end
end

describe 'matching with accents' do
Expand All @@ -77,6 +70,29 @@
expect(results).to include(target)
end
end

describe 'matching a prefix' do
let!(:target) { FactoryBot.create(:teacher, trs_first_name: "Dewey", trs_last_name: "Wilkerson", corrected_name: nil) }
let!(:other) { FactoryBot.create(:teacher, trs_first_name: "Reese", trs_last_name: "Wilkerson", corrected_name: nil) }

it 'matches on the start of a word' do
results = Teacher.search('Dew')

expect(results).to include(target)
end

it 'matches on multiple starts of words' do
results = Teacher.search('Dew Wil')

expect(results).to include(target)
end

it 'only on multiple starts when all match part of the name' do
results = Teacher.search('Dew Wil')

expect(results).not_to include(other)
end
end
end
end
end
Loading