Check out a free preview of the full Complete Intro to Databases course
The "Text Search Indexes" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:
Brian demonstrates how to use text search indexes in MongoDB. A text search is similar to a search one would conduct on a search engine, that would focus and match the key words of a given search.
Transcript from the "Text Search Indexes" Lesson
[00:00:00]
>> Last one here, let's do some full text search. So let's say we had a little like free form search box in our pet adoption app, right? Where a user can go in and type dog, they can type Havanese, they can type Luna. They can type any one of those kind of things in there.
[00:00:20]
We don't have one querable field where we can say, hey MongoDB, give me anything that contains Luna in it, right? And it would look across the name, the description, the location all those different things. That's called full text search, where you're doing text across multiple different columns or multiple different fields, or whatever you wanna call them.
[00:00:42]
It does have that ability to do with a text search index. So what we're gonna do is we're gonna say db.pets.createIndex again. But we're going to do this with type, text, breed, text, name, text. And what's this is gonna do is its gonna create a new index containing all of these things, so now I can just say, hey, pets, search for Luna.
[00:01:12]
And it's gonna give me anything that contains Luna across type, breed or name. It's key to know that collections can only support one text index. So you can't have multiple different types of texts index per collection. Each collection only gets one. Now the fun part is I can say db.pets.find, $text, which is a special operator for searching against the full text index.
[00:01:52]
And I'm gonna do a search for dog Havanese Luna. You can imagine this is something that a user would enter when they're looking for a pet, right I think that's right. Okay, cool. And so now what's interesting about this, all of these are right. You can see that they all contain dog.
[00:02:29]
They all contain one of these things, right? Dog Havanese Luna, so it's finding all the correct things. But you would expect that if I'm searching for dog Havanese Luna, what were the top result be? It'd be the one that most closely matches that right? The one that contains the most amount of these search items.
[00:02:45]
So by default MongoDB does not sort these for you. It does actually create what's called a text score. So you actually go have to go back and say, give me this and also sort by the one that matches most closely to my search terms. So you can do that by, same thing, but you have to give it a sort.
[00:03:08]
And that sort is by score, And score can be called whatever you want, it just has to be called something. But you have to give it this meta, $meta and you're gonna search by text score. I think that's right. Yep, okay. So now I searched again by that but if you look here at the top.
[00:03:42]
Now it's named Luna type dog breed Havanese. And so it's actually more closely trying to figure out which of these matches my search terms the best. That makes sense? So if you actually wanna see that, you can actually pull that score out so you can see the text score yourself.
[00:04:06]
You just have to give it a projection or not. You can just do it that way. So I'm going to say score and just put the same thing here. $meta: "textScore". It's through the projection. So you can see here, the first thing we're giving it is the search object.
[00:04:54]
The second thing, we have to project that score into the response, right, using the projections that we talked about earlier. And you can see here now it's giving these all a score of 3.2. I don't actually know what those scores mean, but you can see all these match it, but let's say like if we sort by text score.
[00:05:18]
Yeah, if it didn't match it as well, the text score would be different, and you can kinda sort by that. Yeah, that's how that works. Anyway, this is super useful if you have some sort of text field freeform text search. You can use these text scores to kind of pull things out.
[00:05:35]
Another nice thing about these texts score are these a full text search. It actually is language aware so it will drop things like the, and and, and it, right like those kind of stop words is what they're called. It's not just in English, it does it for English but it also has that ability for Dutch and German and Japanese and Chinese all that kind of stuff as well.
[00:05:58]
Or you can just have it do neutral, where it won't do any stop words whatsoever. All you have to do is you have to pass an extra flag to that Full Text Search to say, here's the language that I want you to do the full text search for.
[00:06:12]
You can even do it for multiple languages at a time. It actually works. If you want to do English and Spanish that you can do it that way as well. There's a lot more to full text search, there's a lot more superpowers that it has. This is just kind of the most basic way to do it.
[00:06:25]
So I left the link there in the course notes for you to go ahead and to go and explore various different ways to use full text search. Is the search case sensitive? No, pretty sure it's not, but let's give it a shot. Nope, it is not case sensitive.
[00:06:47]
I wanna say that there is a way to make it case sensitive, but I haven't tried. So the question is, is what happens if we insert another document? Do we have to reindex it? And the answer is no, it will continually index new things that you put into the database.
[00:07:01]
When you update it, it'll reindex itself. It automatically keeps its own up to indexes up to date.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops