This is some prep work for the upcoming addition of a teacher list for ABs. We will want to provide basic searching and filtering, and this change implenents searching.
Searches that contain 7 digit numbers will search by TRN. If the search term contains multiple 7 digit numbers all will be returned.
Blank searches will return all teachers, this will be the default state when someone navigates to the page
Searches with strings of text will use PostgreSQL's full text indexing. For now this is lexeme based, which is really fast. If we want to support matching when something almost matches we can easily implement that too with trigrams via pg_tgrm.
Changes
Add search column for teachers
Add search scope to Teacher model
Add Teachers::Search class with basic searching
How it works
Under the hood, there's a new generated field that's ignored by Rails on the Teacher record called search.
If we pull it directly from the database we can see it looks like this:
That allows us to convert our search string to a tsquery and match.
I'm using PostgreSQL's websearch_to_tsvector function here which gives us some nice stuff for free, like you can search how you might on Google. For example,Bill or Ben and records that match either Bill or Ben are returned. Also you can prepend a - on a word to negate it, like Flags -Paving.
Most people won't notice this stuff and it'll just act like a normal search.
This is some prep work for the upcoming addition of a teacher list for ABs. We will want to provide basic searching and filtering, and this change implenents searching.
Changes
How it works
Under the hood, there's a new generated field that's ignored by Rails on the Teacher record called
search
.If we pull it directly from the database we can see it looks like this:
It contains a tsvector which is used by the full text search for matching. The field is indexed which makes matching really fast (see this doc I wrote a few years ago).
That allows us to convert our search string to a
tsquery
and match.I'm using PostgreSQL's
websearch_to_tsvector
function here which gives us some nice stuff for free, like you can search how you might on Google. For example,Bill or Ben
and records that match either Bill or Ben are returned. Also you can prepend a-
on a word to negate it, likeFlags -Paving
.Most people won't notice this stuff and it'll just act like a normal search.
So, we can search like this:
Or in Rails
And under the hood it's all indexed and fast.