Now we're almost done with text indexes, now I want to drop my index again because now I want to show
even more interesting things you can set up when adding the index.
So for that first of all, I need to get the name by getting all indexes,
this is now the name of my text index and now I can drop it by that name by running db products drop index
and then adding that name here, now the index is gone. And I did drop it because I now want to create a new
index with products create index, a new text index of course.
There I want to include my title as a text index and my description as a text index
but now I'll pass some config here because there are two interesting things I can configure about text
indexes.
The first one is the default language,
I can manually assign default language here to a new value.
You saw earlier that the default language it assumed was English and if that is the case,
well you can of course leave that but you could also set this to a different language here and I
could add German here.
Now you're not free to enter whatever you want here,
there is a list of supported languages
and in the last lecture, you find a link to those languages.
Now the field here is not just some cosmetics, we can later use it when searching for text but most importantly
what it will do here is it will define how words are stemmed,
so how prefixes and so on are removed and it will also define what stop words are removed,
so words like is or a are removed in English, in German it would be iste and deya. So the language here
is something you should specify when you know which kind of language or which language will be
stored in your text and you should pretty much know that for any application you're building. Now I'll set
it back to English which is my default and I wouldn't need to specify it here but I'll set it here
explicitly to make it clear that you can set the default language and that this will affect which words
end up in your index.
Now another cool value you can set here is that you can define different weightings for the different
fields you're merging together,
so here I'm merging together title and description.
Now maybe I want to merge them but I want to say yeah, the description should be a higher weight and the
weights will become important when mongodb calculates the score of the result because you might
have awesome in the title but that should not count as much as in the description.
And to set up such weights, we can add a weights key here
in the config object, in the document we pass as a second argument to create index and there, weights
itself holds a document as a value
and in this document, you can now reference your field names like title and simply assign weights that
are relative to each other.
So we could have title 1, description 10 and now description would be worth 10 times as much as title
or would weigh in 10 times as much.
So if I hit enter, we now have this text index created, if I now find all products in my collection here,
you see it looks like this
and now, let's again search with a text index.
So I will use my products to find with $text and now there, we saw the search keyword
already,
now besides the search keyword which I'll fill out in a second,
you can also specify the language, German here.
This is more interesting if you use a different way of storing the language because you can even have
different languages for different documents,
that is an approach which I'll highlight in the last lecture of this module, you'll find the article
there to this advanced set up. Interesting for us here also is that you can also turn on case sensitivity
with case sensitive set to true,
now the default is false, with true
you could suddenly search in a case sensitive way,
so that's just another side note.
Well let's go back to just the text and let's search for red,
now obviously this works,
let's now also add the score here with
$meta text score
and let me also add pretty to make it easier to read and you might not remember the exact number we
had earlier but it was not 6.6666,
it was a different number earlier
and this is because we now added some weights and I can prove this if I simply drop my index,
so first of all I get my indexes to get the name,
here it is,
then I quickly drop the index here
like this
and then I re-add the index with create index
but now without assigning weights, so I will remove that weights assignment here at the end,
I only set the default language which I also could omit,
it doesn't matter.
So now we have a text index working in the same way as the other one but without weights
and now if I execute that same find query, you'll see the score is a different one,
it's not 6.6666 but 1.2 6 because the score is now calculated differently because weights don't
play a role and weights can of course help you well put the weight on the field that holds your most
important key words.
This is the text index, as I mentioned,
you'll find some further articles in the last lecture of this module but the vast majority of important
features were covered here and managing the text index might not be your primary task,
being able to use it with things like the meta score and understanding how it works, that it might
search across multiple fields is definitely something you need to know and hopefully, something which
became clearer
here.