I bought Mastering Postgres because I use Postgres a lot and wanted to support Aaron and his content. I already learned more than I expected and have been very impressed with the quantity and work Aaron put in.Timo Strackfeldt
Shorten dev cycles with branching and zero-downtime schema migrations.
One of the things we need to think about when we're building out this search engine is the user experience. We've already kinda gotten there with the web search to tsquery. We kinda got there with the GIN index, so now we have some nice searching, some nice performance.
I think one of the final things that we need to think about for user experience is notifying or showing the user the matches as they're typing. If somebody is typing Star Wars and a title pops up, it would be nice if Star and Wars were highlighted so that you can see your search query lining up with all of the results and see which parts match your query. Fortunately, Postgres has a built-in highlighting function that I'm gonna show you how to use right now.
The highlighting function is called ts_headline, and so we're gonna do ts_headline and we can do this across different columns. We don't actually use the vectors either stored or generated. We don't use those vectors. We just use the original document as they call it. We're gonna pass in, again, we're gonna pass in English. The next thing is the document. In this case, we're gonna pass in title because this is just going to be the title highlight, so we can say as title_highlighted. That will be that. The final thing is we do need to pass in the tsquery. We're just gonna use our same query here. If we run that, we'll see that it worked almost immediately. We get Star and Wars highlighted along with Wars down here. Everything is surrounded with these bold markers, which is fine.
I would rather something maybe a little bit more semantic or something a little bit more controllable on the front-end. I'm just going to change it. Instead of using bold, we have the option, the fourth parameter is we can pass a bunch of different configuration options, including the opening mark, the closing mark, the minimum words, whether or not we should include the entire document in the result. All kinds of stuff like that. We're just gonna look at changing the opening and closing marks. We do that in this final argument here. We're gonna say StartSel equals, we're gonna say, let's do a start of the selection equals mark and a stop of the selection equals end mark. It's just comma separated. You have your option name with equals and then the value. I'll put a link down in the description because there are some other options that you can pass through here, some of which may be helpful.
These are the ones that I really care about. We're returning it with these mark tags, which is a little bit easier to style on the front-end and doesn't necessarily mean it's going to be bold. Maybe we do a yellow background or something like that. I do like that this ts_headline operates on the original document rather than the vectors, because as you'll remember, we're searching across title and plot, but we can return a title highlighted and then optionally return a plot highlighted as well.
If you do highlight over a large document, it is going to compress it down and leave some ellipses in there, which is controllable. But that's really nice 'cause we can get the entire title back and show those highlights and then have some sort of excerpt or snippet from the plot and just show those highlights, which makes for a really nice user interface.