so now it's time to implement the next feature which is a search feature now why search is important is because we are using data JPL right which is spring data jpa and if you look at the repo layer it's quite empty so we are not defining any method or we not even declaring the methods we we got all the methods from the jpa repository so when things are simple like C operations it can be done with the help of JP repository but let's say if you want to find something which is not a primary key because whatever we are doing now if you're searching for if you're searching for a product by the ID it is very easy right you can just use a product by ID and it works but what about if you want to search with some other things example if you want to search with the name or I the product name or the description or the amount the quantity I mean that's weird right why someone will search for buy quantity buy quantity doesn't matter you got the point right so if you have other fields to check with how do you make it work because Bight will not support example if I go back to my product service and let's say in any of the method here let's in the update itself just just to check what can do we do so if I say report dot there are multiple methods right uh one of the method is find my ID so if you have the ID you can do it but we don't have a method using which you can actually find by name or any other field of the database now what are the fields we have if you go to product uh we got description brand yeah so let's say if you want to search by a brand in that case what you can do is let's say when you search with a brand and uh of course you can have multiple products with the same brand in that case you can Define you can declare not define you can declare your own method which will return you the list of product and okay list of products and then you can def you can mention the method you can mention find by and you can mention the variable or the type which is brand and then you can you can pass the brand here okay so this is how basically you can achieve this now your GPA does support this right but then this is only for one field or maybe you can have two Fields you can also have here and and you can mention the next variable but let's say you have multiple things check with example if you want to search but then not just based on one field or maybe multiple field or maybe you have to you have to mention some customize query in that case you can literally write the query not the SQL query you can but I don't want to write the SQL query and that's why we we got something called a jpql which is jpql which stands for uh jpa query language so it is similar to SQL the only difference is in SQL we use tables here we have to use class name in SQL we use column names here we have to use the field names okay so those are those are the changes we have okay uh so the method which I want to do is which I want to write here is a method which will return the Leist of product and maybe I will have the method name as search products and then here you can pass the keyword on on which you want to search right so we can have this method but this will not work so by default JP will not understand and what search products means so and that's why you have to mention the query and the way you can do that is by writing atate query so you can write the query here okay now what is a query we'll see that in sometime but we have to mention the query in this in this brackets okay and of course in the query we're going to use keyword uh so example if you want to search something in uh in SQL so you say select star from the table it's a product where the product or the brand is equal to and then you you can use a like keyword and you can put that in single code right so what's a keyword you want to search with so that's something we we are going to use here but then if you want to achieve this we have to also change our front end right so let's start with front end and then we'll come back here so let me go back to my front end now this is a front end right now in this front end basically we don't have the search feature so for that I'm going to open the new front end so and you'll find the link in description so I will say open folder and this is the thing which is eom front end 4 so this is the fourth front end which we are using of course everything is iteration so nothing new uh and if I open that uh okay I can simply say npm install and by the time it is happening let's look at the place where it is searching again with you I'm doing this for the first time so I'll go to naar I'm assuming that it is naar and here it is uh so you can see there there will be search box and then every time you hit the keyboard or when whenever you hit a key it will call this function call handle change uh let's see where are we calling it yeah so if you can see uh we have handle change here so with every input every time you press the button it will hit this query again there are different way of implementing it maybe you can have a button so once you type the entire text you can hit the button uh which is much more efficient in terms of uh server loading because you're not going to search this server for every key but maybe we want to make that more user friendly just by typing few letters if you can get the suggestions uh so it is hitting that and it's requesting for this URL so this is what we have to work with so search and then the value okay so we are using question mark here in the front end we can also use slash but let's a question mark makes sense here so we are not going to use path variable because this for this we have to use something else again we'll see that so this is the URL we have to work with so install done let me just say run T and enter so it's running on this port number now maybe earlier port number is busy it says something went wrong uh because the server is not ready and this is what I was talking about so if I search something it should give some suggestions there's no button here to say go so you have to give the suggestion so now what I'm going to do is I'm going to go back to my server or the back end and in this basically I will go back to my product service we have we have return return this let's remove the extra line and go back to controller so basically we need a mapping for that particular search so I can create a public it will return the response entity which will return the list of products and let's say the meod name is search products now this will accept a string right so string keyword which is coming from the client side and for this we are going to do mapping as well so I will say request or get mapping for the URL so now we know the URL it is/ product slash okay we don't know the URL SL product SL search is the URL okay but then how we are going to accept uh this particular thing here which is name is value now in that case of course it should be keyword so searching for name okay looks like I'm using a wrong front end okay so we let me open the fifth one I think fifth is a correct one I was expecting that it will be keyword but I got name uh let me just check the fifth one and yeah so just keyword so this is the correct one not the earlier one uh so what I will do is I will just do the same thing for this which is npm install and npm runev so when this is using the same port number okay this is what I was looking for so we have changed the uh front page if the back end is not ready okay so my bad so I was using the wrong UI so I will share this link in the description the fifth one okay anyway so let's get back to the code and here I'm going to use the same thing because we are searching for keyword I mean we are passing the keyword in the URL so it will accept that in the same name okay once you have that the next thing you have to do is we have to basically call the service method right to search it now the service method is going to return you the list of products and I will say products is equal to and I will use a product service so I will say service dot uh search products unfortunately we don't have that method there but uh we'll will create one so I will say keyword so I want this method in the service but let's say if you got the products how will how will you return the value so I will say return and now we have to return the response entity not the products and in the response entity we have to return the products and then we have to also give the status which is dot okay and done but then we don't have this method so let's create one in this service and in the service we have creating this method now again service will not do anything service will simply say return and it will ask the repo hey repo search the products and since we have created the method in this repo first we have that there just pass this keyword so service job is nothing much here just pass it to the repo now in the repo we are going to do the actual work okay I'm just confirming if everything is good in here so looks good nothing wrong let's go back to repo and let's write our query okay now basically we have to use the jpql right not the SQL query so it looks similar to SQL so I will say select and we don't say star we say p now what is p here uh so I will say from product and mind you product is not a table here it's a class name and then this is the Allies for it so this p is allies for product and that's what we are using here and then we say where and now after this where we have to check for multiple fields we got for name br land so we'll do that here so I will say enter uh now the first thing we can do is we can check for the name field so I can say p. name because in the P we got the name right so we'll say p. name and let's match it with the keyword now how do we match it basically we can use like for matching the text and then we can use the uh percentage symbol because it might be having something front and back of it that's how we do it in SQL and in between we can pass the keyword now since keyword is basically this is what you have to it has to pick up right so we can use the uh colon So when you say colon it will search for this field in the parameters which you are passing so that it it sols but this may not work uh maybe because the text which you're searching for is small and in the database you got Capital so what you can do is you can convert everything here so you can say lower so these are some Inu functions we have and here also I want to lower it the keyword okay so we are lowering so these are the input functions we in jpql so we can use lower it will convert this into a small letters and even this will do it but the problem is we are concatenating this uh percentage percentage with the uh keyword right so instead of doing this we have to use the method so the method name is concat I want to concat this particular percentage with the keyword and this keyword with the percentage and since we are opening the brackets here that is closed here we are opening it here we'll close it here now this is only for the name what about uh you want to have some more things so you have to say all and enter more things here now since I already have all the things written so I will just use it here instead of typing so I want to add more lines I'll just paste it here looks like we got an extra bracket yeah so this looks good I'm just confirming if everything is correct it's It's tricky because even if you want to check what is happening basically we have to First add those products and check it uh and I have to do things multiple time so I will just check it once okay so from B where okay looks good looks good so this should work so basically what we are doing is we are converting every charact every text into lower and then comparing it with the keyword so now once we have done this uh let's restart the application or in fact we have not started even started this so let's restart and see if that search is working in fact what I want to also do is here every time you call this search I'll just print searching with and let's also specify the uh keyword on which it is searching and doing that let's restart okay okay so it's running on port number 88 so no problem let me just go back to my uh browser and let's refresh so you can see it's no product available so that means is connected and let's click on the search and search something let's say I want to search for Samsung phone if I say s okay this is not working let maybe let me just add a product here so I'm saying Sam phone Sam mobile Samsung 33 it's a phone stock is [Music] three date is this choose a file this is a phone open and now when I click on submit product added okay let's see if that works yeah it is working but now why is not printing anything on the console let me restart once again okay let's hit that now again when youart you will lose data and if I say s now what song why is not printing anything let me try for the homepage this is weird Okay it is saying hi but why is not doing for the search okay okay I think I got the problem the problem is we got here products not product okay I thought there's something wrong with the ID itself okay let me remove this high from the top oh suddenly I started questioning my Java knowledge now get back here and if I say s so you can say no product name with such name but it is searching with s now if I say something else there if I say sa a it will hit it with sa so every time you type something it will search but now to actually test it we have to add some product so I will go back here and let's say I'm saying Sam mobile or brand Samsung and some description budget phone and here price six uh 666 it's a phone stock is two date is current date or maybe 23 let's pick up the phone image and add so that's the first product you have which you have added and now we got home here let's add one more to test it I giving laptop brand is Asus and pass machine it's a laptop date selected image selected submit okay so you can see we got two products now how we are going to search let's say let's say I want to search for Sam and you can see when I'm searching for Capital a it is doing that right and if I click on Sam mobile we are getting this next if I say a phone so it still so shows Sam mobile because in the in the uh description we are saying it is phone right next we can also search by uh something else maybe gaming with the name s so we got gaming laptop we can search a brand which is Asus so we got gaming laptop so that's how your search is working again the UI is good and that's why you can see such beautiful text here but then you have to also make your backend work to get it work uh so at least you now you know how do we write our own queries with the help of jpql so what you do is you use select from similar to SQL the difference is instead of using a table name you have to use a class name and then uh these are not compulsory thing but then since we we wanted to make sure that the search should happen even if you don't follow the capital and small and that's why you got here and that's why we using lower and all this stuff so yeah that's it from searching uh I hope you enjoyed so now let's in the upcoming videos let's see something else and we'll have fun thank you so much for watching bye-bye