1 00:00:02,130 --> 00:00:07,890 Now we're almost done with text indexes, now I want to drop my index again because now I want to show 2 00:00:07,890 --> 00:00:12,450 even more interesting things you can set up when adding the index. 3 00:00:12,480 --> 00:00:19,060 So for that first of all, I need to get the name by getting all indexes, 4 00:00:19,280 --> 00:00:27,830 this is now the name of my text index and now I can drop it by that name by running db products drop index 5 00:00:28,330 --> 00:00:34,790 and then adding that name here, now the index is gone. And I did drop it because I now want to create a new 6 00:00:34,790 --> 00:00:40,050 index with products create index, a new text index of course. 7 00:00:40,040 --> 00:00:49,440 There I want to include my title as a text index and my description as a text index 8 00:00:49,770 --> 00:00:55,140 but now I'll pass some config here because there are two interesting things I can configure about text 9 00:00:55,140 --> 00:00:56,090 indexes. 10 00:00:56,100 --> 00:00:58,230 The first one is the default language, 11 00:00:58,230 --> 00:01:02,370 I can manually assign default language here to a new value. 12 00:01:02,370 --> 00:01:08,480 You saw earlier that the default language it assumed was English and if that is the case, 13 00:01:08,490 --> 00:01:14,820 well you can of course leave that but you could also set this to a different language here and I 14 00:01:14,820 --> 00:01:16,550 could add German here. 15 00:01:16,590 --> 00:01:19,430 Now you're not free to enter whatever you want here, 16 00:01:19,440 --> 00:01:21,400 there is a list of supported languages 17 00:01:21,600 --> 00:01:25,570 and in the last lecture, you find a link to those languages. 18 00:01:26,100 --> 00:01:33,540 Now the field here is not just some cosmetics, we can later use it when searching for text but most importantly 19 00:01:33,540 --> 00:01:38,290 what it will do here is it will define how words are stemmed, 20 00:01:38,520 --> 00:01:46,230 so how prefixes and so on are removed and it will also define what stop words are removed, 21 00:01:46,230 --> 00:01:55,020 so words like is or a are removed in English, in German it would be iste and deya. So the language here 22 00:01:55,530 --> 00:02:01,290 is something you should specify when you know which kind of language or which language will be 23 00:02:01,290 --> 00:02:05,880 stored in your text and you should pretty much know that for any application you're building. Now I'll set 24 00:02:05,880 --> 00:02:11,280 it back to English which is my default and I wouldn't need to specify it here but I'll set it here 25 00:02:11,280 --> 00:02:17,070 explicitly to make it clear that you can set the default language and that this will affect which words 26 00:02:17,280 --> 00:02:20,350 end up in your index. 27 00:02:20,350 --> 00:02:28,810 Now another cool value you can set here is that you can define different weightings for the different 28 00:02:29,500 --> 00:02:31,310 fields you're merging together, 29 00:02:31,540 --> 00:02:34,670 so here I'm merging together title and description. 30 00:02:34,670 --> 00:02:40,420 Now maybe I want to merge them but I want to say yeah, the description should be a higher weight and the 31 00:02:40,420 --> 00:02:46,180 weights will become important when mongodb calculates the score of the result because you might 32 00:02:46,180 --> 00:02:51,590 have awesome in the title but that should not count as much as in the description. 33 00:02:52,030 --> 00:02:56,890 And to set up such weights, we can add a weights key here 34 00:02:58,000 --> 00:03:05,830 in the config object, in the document we pass as a second argument to create index and there, weights 35 00:03:05,890 --> 00:03:08,900 itself holds a document as a value 36 00:03:09,680 --> 00:03:15,560 and in this document, you can now reference your field names like title and simply assign weights that 37 00:03:15,560 --> 00:03:17,210 are relative to each other. 38 00:03:17,270 --> 00:03:25,640 So we could have title 1, description 10 and now description would be worth 10 times as much as title 39 00:03:25,730 --> 00:03:28,870 or would weigh in 10 times as much. 40 00:03:28,880 --> 00:03:37,640 So if I hit enter, we now have this text index created, if I now find all products in my collection here, 41 00:03:37,670 --> 00:03:39,250 you see it looks like this 42 00:03:39,650 --> 00:03:43,580 and now, let's again search with a text index. 43 00:03:43,580 --> 00:03:53,050 So I will use my products to find with $text and now there, we saw the search keyword 44 00:03:53,050 --> 00:03:53,910 already, 45 00:03:53,920 --> 00:03:57,930 now besides the search keyword which I'll fill out in a second, 46 00:03:58,360 --> 00:04:03,820 you can also specify the language, German here. 47 00:04:04,010 --> 00:04:10,220 This is more interesting if you use a different way of storing the language because you can even have 48 00:04:10,220 --> 00:04:13,260 different languages for different documents, 49 00:04:13,280 --> 00:04:17,630 that is an approach which I'll highlight in the last lecture of this module, you'll find the article 50 00:04:17,630 --> 00:04:23,990 there to this advanced set up. Interesting for us here also is that you can also turn on case sensitivity 51 00:04:25,020 --> 00:04:28,710 with case sensitive set to true, 52 00:04:28,710 --> 00:04:31,520 now the default is false, with true 53 00:04:31,530 --> 00:04:34,230 you could suddenly search in a case sensitive way, 54 00:04:34,350 --> 00:04:36,310 so that's just another side note. 55 00:04:36,410 --> 00:04:42,400 Well let's go back to just the text and let's search for red, 56 00:04:42,440 --> 00:04:43,950 now obviously this works, 57 00:04:43,950 --> 00:04:48,250 let's now also add the score here with 58 00:04:48,260 --> 00:04:50,920 $meta text score 59 00:04:51,600 --> 00:04:58,080 and let me also add pretty to make it easier to read and you might not remember the exact number we 60 00:04:58,080 --> 00:05:01,670 had earlier but it was not 6.6666, 61 00:05:01,800 --> 00:05:03,550 it was a different number earlier 62 00:05:03,780 --> 00:05:12,390 and this is because we now added some weights and I can prove this if I simply drop my index, 63 00:05:12,460 --> 00:05:17,620 so first of all I get my indexes to get the name, 64 00:05:17,630 --> 00:05:19,230 here it is, 65 00:05:19,300 --> 00:05:22,360 then I quickly drop the index here 66 00:05:23,500 --> 00:05:24,210 like this 67 00:05:24,400 --> 00:05:27,220 and then I re-add the index with create index 68 00:05:27,250 --> 00:05:33,220 but now without assigning weights, so I will remove that weights assignment here at the end, 69 00:05:33,280 --> 00:05:35,710 I only set the default language which I also could omit, 70 00:05:35,710 --> 00:05:37,420 it doesn't matter. 71 00:05:37,420 --> 00:05:42,700 So now we have a text index working in the same way as the other one but without weights 72 00:05:42,700 --> 00:05:47,820 and now if I execute that same find query, you'll see the score is a different one, 73 00:05:47,860 --> 00:05:54,880 it's not 6.6666 but 1.2 6 because the score is now calculated differently because weights don't 74 00:05:54,880 --> 00:06:00,820 play a role and weights can of course help you well put the weight on the field that holds your most 75 00:06:00,820 --> 00:06:02,980 important key words. 76 00:06:02,980 --> 00:06:05,460 This is the text index, as I mentioned, 77 00:06:05,530 --> 00:06:11,560 you'll find some further articles in the last lecture of this module but the vast majority of important 78 00:06:11,560 --> 00:06:17,860 features were covered here and managing the text index might not be your primary task, 79 00:06:17,950 --> 00:06:23,380 being able to use it with things like the meta score and understanding how it works, that it might 80 00:06:23,380 --> 00:06:28,360 search across multiple fields is definitely something you need to know and hopefully, something which 81 00:06:28,360 --> 00:06:29,150 became clearer 82 00:06:29,160 --> 00:06:29,420 here.