Speaking of multikey indexes, there is a special kind of multikey index, a text index.
Let's say we have this text stored in a field in our document, could be the product description of a
product.
Now if you want to search that text, we saw before that we can use the regex operator and that
is however not a really great way of searching text,
it offers a very low performance. Better is to use a text index and a text index is a special kind of
index supported by mongodb which will essentially turn this text into an array of single words and
it will store it as such,
so it stores it essentially as if you had an array of these single words, one extra thing it does for
you is it removes all the stop words and it stems all words, so that you have an array of keywords essentially
and things like is or the or a are not stored there because that is typically something you don't search
for because it's all over the place,
the keywords are one matter for text searches typically.
So this is the text index,
now let's have a look at such a text index
and for that I'll, whoops not index, insert many products into a newly created products collection here.
So the first product will have a name or
let's say a title to mix it up, title, a book and it will have a description, this is an awesome
book about a young artist.
The second document I add here should also describe product will have a title of red t-shirt and there,
we'll have a, whoops we'll have a description of this t-shirt is red and it's pretty awesome.
So after finding that missing quotation mark, it now worked fine for me
and now if I have a look at my products and I pretty print this, here are my two products.
Now let's use a text index,
let's use it on the description field let's say. So first of all, I'll put our products again so that we
can see them
and now we can create a new text index, again with create index as we create all indexes
and I say I want to create it on description and now important,
don't add 1 or -1,
you could do this but then it would simply index this as a single field index and you could then search
for exactly this text to utilize the index but not for individual keywords. You need the text index
so that mongodb splits this up,
so using this will not work so
let's quickly drop that index like this and let's recreate the index but now not with a 1 but with the
special text keyword, text in quotation marks. This will create a text index which is a special kind of
index where mongodb will go ahead and as I mentioned, remove all the stop words and store all the
keywords in an array essentially,
so let's have a look at this.
This is the data we have in there in general
and now let's use products and find and
here
let's now use the special $text key
and search and for that, you pass a document as a value for $text and there, you need
$search.
Now you might be wondering why do I not need to specify the field in which I want to search,
why don't I have to add description,
instead we just add hey I want to search for some text.
The reason for that is that you may only have one text index per collection because text indexes are
pretty expensive as you can imagine,
if you have a lot of long text that has to be split up, you don't want to do this like 10 times per
collection and therefore, you only have one text index where this could look into.
You can actually merge multiple fields into one text index as I will show you in a second and you will
then look through all of them automatically
but you can only do it like this,
you can't say hey I want to search for text and description like this, this won't work.
So now for search, we simply enter the words we want to look for
like awesome and the casing is not important here by the way,
everything is stored as lowercase.
If I hit enter and I pretty print this, you'll see I find both products because in both products, we have
the term awesome.
Now let's repeat this
but let's now just search for the term, book.
So here I'll search for book
and now you see I only get well this text or this document where we have book in the text,
now what if I search for red book, I have red in the document.
Well then I find both again because this is now actually not treated as one connected phrase where
it would look for a red book but it simply looks for documents that have a red text or some red
in a text and for documents that have book in a text. Of course you can also search for a specific phrase
though, you can search for phrases by wrapping that phrase in double quotes and since we are in
double quotes already, we have to escape them with a backslash double quote.
We have this at the beginning and at the end of the phrase and now we don't find anything because
we have no red book phrase anywhere in our text, for example
awesome book would work though,
so if I look for the awesome book phrase, we would find this document because we have awesome book right
there.
Now this is really powerful and much faster than regular expressions,
so this is definitely the way to go if you need to look for keywords in text.