how to implement elasticsearch in .net core | elasticsearch Curd

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get start Demo First of elastic search uh Cur operation okay so these are the some operation like get postp delete so we'll Implement actually net core application net core B API and we integrate our you know elastic search Okay so let's get start with the get operation so first I'll just try execute so as of now there is a no it is a 200 but there is a no response because this index whatever index I choose in last sck search that Index right now does not have any data okay okay so what need to do I'll just post some data so let me try ex uh execute your post request okay remove that ID because this ID will be generated automatically and just add some fake data like for example Cop blog and uh description is cop description and author with the kkp okay let me let me execute okay see getting 200 with document create successfully which is response body we have send it back to here okay now back to your get all details get all request okay execute now this time I'm getting the response body okay so yeah whatever data I just created see I just created with this unique ID okay now what need to do I need to update something okay so let me update let try out pass it to data uh ID whatever ID I want to update and if I want to cop to for example elastic search for example it's a short elastic search and then description ES and Kiana okay and then author is karik okay let me show you in a our request body okay this is our request body for update with this this is important actually this is ID uh which is uh will update on this in a index okay so let me execute see the response response is 200 with this document update successfully and let's check this data the let let's check the data with this get uh you know get by ID um based on this whatever ID okay so let me add pass it to ID and check this whatever data we updated that is updated or not see this is updated with 200 tonse okay previously it was C but yeah now I just change to elastic search and whatever the informations okay and finally I can show you in this our uh here like this is a kibana dashboard okay so whatever elastic search is running see this is a running our elastic search locally so whatever we have our search elastic search index that we can show in the Kiana so let me show you with this refresh once you refresh see whatever data I have in this particular index you are able to see so this index I'm just using and let me show you in a Json format as well see this is the data okay so let me delete as well this one so that we can complete our all the operations okay so let me delete this record and use the delete end point and try out pass the ID execute and delete successfully so let me check first here as well data exist or not execute now see 200 with no response similar I can see in our dashboard Das just refresh so this uh index does not have any data okay so that's all for this demo okay so let's move to implementation so before move to implementation if you not aware how we can you know uh that uh installation and know set up your elastic search and Kiana so I'm recommend to you go to my channel okay net real world example okay so go to my channel and you can able to see over here this is my channel net real word example and you are able to see the setup of Kian and elastic search okay so here you will able to see go to the video section okay so here you able to see that see the installation key one and elastic search very easily so you can use this one uh proceed this one so that you can install your kibana and elastic search in your locally okay so that you can uh Implement your uh application with this local okay so let's open then uh your visual studio so let's create a new project use sp. net core B API choose next uh name it call es card of operation yes card operation that's fine and the application name is API and solution name only just this okay and choose next okay use that uh use controller if you don't want to use that controller API so you can keep leave it over there okay and just create a next I using same Net 7 next now your application is ready okay okay so first first things what you need to do you need to add some packages okay so what packages are required first you required two packages so go to your manage packages and go to browse and just search for elastic search.net okay this one let me zoom in so this is the package which you need to install so click here installed okay once done then choose another one next okay so this is that one also you require this package as well so click here install okay done then go to your application solution okay in the project okay so what need to today need to do next first you have to just create you know some service okay so I just create a one service where actually I use that uh generic uh generic service which is actually insert update delete we can do our operations right so let me create a folder use the name as a Services create a service use interface choose that interface name is elastic search okay we'll create a you know genetic uh uh Services of elastic so you need to pass it to that t Okay and then what you need to do you need to just create some method okay so instead of wasting time I just copy paste those method I'll show you okay see remove this one okay so this is for create which is accept whatever document generic document entity type whatever entity or whatever document you have you can just pass it over here based on this it will returning that string and also get document by get document a sync also passing the ID and return the result also get all the documents it will returning in numerable of list and this is update document it will update your documents and delete documents it will delete your documents it's very straightforward and simple okay let me zoom in for you so these are four operations which will Implement our services okay so let me create the Implement service name it is is like elastic service add okay what need to do you have to just make it your generic Service as well so make it a generic and then what you need to do you just inherit your interface elastic search okay and then here also just pass it to generic type and then use where so that whatever entity okay and using only class okay so this is done and then what you need to do you just need to implement whatever method uh whatever signature in this uh interface right so let me Implement over using this uh features Visual Studio features okay Implement interface so once you do it will automatically uh give the implementations okay now we have to do our it is a default implementation but yeah you have to complete or you have to do your own implementation okay so first implementation I need to do over here so uh before that what I need to do I need to inject actually our service okay so what is our service I need to inject our uh elastics client okay so first let me create a Constructor Okay add your elastic search client okay so elastic client let me bring the name space okay it is using that using. Nest okay and also just create a field okay this is done now inside this elastic search elastic client we are able to get all the methods like that all the operations for example I'll show you one plastic search and then if I want to use index index. documents Asing so this one for create one okay or if you want to get something so get document so see get a SNS that will get on okay so similar I'll use for that here for create so I'll use that index dot document a sync okay so this one for async asynchronized method and pass it to our document so whatever document I'll just pass I'll need to pass it over here okay and then is this method just returning something okay let's see let's just store something over here and response and and then what need to do I need to return okay whatever response it is of valid okay because this whatever will uh this method actually once created so it will uh return the response and that response have a a property is valid okay so it is not visible the because see this is a synchronized method okay so what we need to do we have to use that asynchronized method so you have to use that await okay and then use that async and then you will get all the method like like that is is valid okay if it is valid so what you need to do you just pass it to your message okay what what you want to return so I want to return this else it is fail right so that's all similar I need to do for delete okay okay I need to get everything right so let me Implement for delete one so similar I use that delete I need to call that delete elastic search delete method delete a sync okay once it's delete uh what need to do let me change to message type A sync okay so what response this response I'll check and then return to here okay it is Sim it is similar like that right so this is providing by that Nest client okay so similar we have to implement for that get all documents okay so it is very simple again okay let me show the codes okay so change it to that elastic client elastic search client okay and here see I use that search but yeah I can use the search async as well use that as uh sync await await okay and similar I can return return uh return that search response whatever search search response do documents because this object returning a documents whatever data I have so that is collection as a document okay it's simple and what I need to implement next I need to implement get document by a sync okay it is very simple let me change it to our injectable property so this is one right one so change it over here for a sync okay and now what need to do it is very simple and just store over here similar I need to the response The Source okay so let me return response do source source this one okay this is also done okay now the last one is update okay so I need to update so this one little bit uh little bit different but yeah it is also very easy okay so what need to do first again let me show you instead of wasting time uh writing a code I'll just copy paste okay and make it to a sync method okay and change it to our client uh elastic search client okay and then call it to update a sync method and use that new document okay whatever document we have just pass it to as a new document and use the Lambda function and use the document okay the document method and inside the doc method you can pass your document okay like look like that also you can use see there's also use the mechanism like you know retry on conflict suppose it have some conf confliction so it will try to three times and then it will Ex exit Okay and similar what I need to do I need to just make sure if it is a valid means that whatever response it is a true so just return to document update or neither it's fail okay so that's all for insert update delete okay so all the method that's completed let me zoom in for you one more time so create done delete done get all done get document get documents means only single with the with the ID okay and the last one is update document okay so that's all implementation done so now what what need to do I need to just uh create our controller okay so whatever it is a existing controller so I just leave it that controller okay I just remove and just create a new controller so that I can Implement our all the uh endpoint okay so let me create a controller okay API controller empty controller okay and just make it [Music] to yes okay add so this is a new controller it adds nothing okay so let me call uh one by one okay so first I'll just call it to our get end point okay so before that what I need to do I need to inact whatever I service just created right so this service I just created so this service handling all the things or this is the abstraction okay so that I need to inject over here controller and then I'll use it okay so create a con uh Constructor of that controller okay so I'll just create it that one and copy to your method uh their interface and pass it to inject over here and pass it to here and then it's shortcut to you can just create your field create a field and assign to your Constructor okay so this is a very simple and then what you need to do you have to just pass your document okay so because it is a type so what kind of document you want to know perform your operation so you can just pass it to over here okay because it is a zenic type so I need to just create a model of the document so what you can to do you can just create another folder and call it to models okay inside here I need to create that model OKAY create a new class and call it to my document okay for example but yeah uh you can use whatever you want okay call it to here and then here I need to implement something means I need to uh add some uh property over here so that whatever property I will use so those uh data I can insert update delete okay so I can create an object okay so it's very simple let me copy P to you inad of wasting time okay so it is very simple I just create ID name description and author okay and I use that you know default uh value okay so that whatever if I miss if I if I not pass any data of that ID So based on this it will run generate a new random number and it will add over here but yeah you can pass you can just create your own logic whatever you want you can just pass it because that index need to create a you know that unique ID okay so it will create automatically but yeah you can just uh create your on as well okay so that is done go back use that my my document okay and bring the name space okay so my document is here and then similar you have to just injected passing your document as well okay so now injecting successfully okay now I can create uh our endpoint so it's very simple I just create first endpoint of get my ID okay get all okay so this is a get documents I use that controller if it is not familiar of this net core API so I especially recommend to again go to my you know my channel and you can visit my one of the session where I just explain everything of net core API and controller and routing everything's like I just explain okay so it is very simple just I call whatever injecting service I just call it and get this method and return back to here okay so this is done for get now I need to done for post so this is for post again it is very simple uh pass it to the document in a body okay and just call it to our create document which will create our uh the data in elastic search okay and just return back similar I'll use for that get by ID so this is simple again and pass it to Route and using that read okay and then pass it the ID and call that service and that service make sure it is there then it is there then okay if it is not then it is not found okay similar I'll use that put okay so it is for update document so passes the update document inside the request body pass it to our you know our service and then service return to result okay and finally for delete okay so this is for delete okay so again pass it the route data as a name and then passes the ID and that you know connect with the over here and then call it to delete service and the return back to user okay so the controller also done okay so service done controller done now I need to configure okay so where I need to configure because see I just creating service I injecting over here so uh it's actually the dependency okay so this service are dependent on this controller right so we have to resolve some dependency so go to the program. Cs file here I need to uh configure our El search dependency as well as this Services dependencies okay so this is very important actually if you are not about this dependency or things so I'm especially recommend one more time go to my dependency uh uh you know session you are able to get you know full idea like what is the dependency how we can resolve the dependency what are the dependencies uh ways actually we can do with this practical example okay so go back okay and now what you need to do first first you just you know get your uh elastic search URL okay so there's are many way actually you can get like you can install your app setting and you can get but yeah for the demos perspective I just use uh directly over here okay instead of this actually I will use that uh I will use that directly okay so what I need to do first I need to just create a new connection ction setting actually settings okay so this is one new connection settings and here see the new connection setting actually it is uh coming from our uh okay using the nest client okay and here the first parameter like see first parameter it is asking for there's a nine overload method okay so you can just pass URI and you can just you know many way actually you can register so what I will do I'll just pass my URI okay so let me create a new URI so create a new URI okay and inside I can just pass our Ur string okay so what is my URI string my URI string is this one okay this is my elastic search URI okay so go back back your here and just pass it to your elastic search URL okay this is done and then what you need to do just call it to default index default index and here you can pass your index okay this is one way uh just for demonstration I'll show you but you can just use uh know that index call to store over here you can get it over here and also you can use this is your service level as well okay so in this service level you can just modify your you know that all the implementation here you can just pass you know your index as well so that if you required you can just let me know in the description I'll just you know uh uh show the another session okay but yeah not this session this session I'll just keep it to simple so that you can understand okay so uh what I need to create a new index so a new index like that elastic search uh like demo okay so es demo I put it over here and then that's it okay and keep it to uh store a variable where and the settings okay I keep it to settings and then what I need to do I just uh create a client as well okay so let me create a client so let me create uh client of elastic search client so new new elastic client okay and then that elastics client what expecting it is expecting actually that connections okay the settings actually so you can just use this overload method uh let me do that okay so this one so you can just pass it to to your settings okay connection is string settings so let me use this settings okay not here you can just pass it to inside the lastic search client okay so this is done and then finally what need to do you need to just use that add Singleton okay so that your uh service can you know lifetime only just one lifetime create a one instance only okay so use that uh Builder dot Builder do Services dot at single turn and then pass it to your client okay so this is uh way to resolve your dependency of elastic search okay so this is done now another thing what need to do uh see uh that elastics client actually inject in our service right so so if you go to to our services so see this elastic search actually I injected so that's why I have to resolve this dependency first so that dependency I resolve but yeah now see I I have created another service which is dependent on this controller okay so if you go to controller so that is also dependency dependent okay so that also need to resolve so it's nice way another uh another here like very simple for example I use that simply Builder the services add scope but this time I'll use that different uh registration dependency right so uh that is actually we required over here for the demos perspective okay so I'll just create elastic search Okay pass it to document and also there is a implementation okay so these two things is very important let me zoom in for you okay so this is a configuration okay so this is a very important and that is done okay so finally uh we covered everything so let me rebuild okay so that we can make sure uh everything is working fine okay now it is succeed okay and uh let me open a dashboard Let me refresh okay so this is a old index but this index is not appearing over here because we just create we'll create a new Index right so okay so if you I'll show you again uh go to manage okay and all resource see here we don't have that es uh es demo Index right so that will be created once we send some request of here right so let me close the old demo session okay and go to our implementation here okay so our index name is would be this one let me show you es demo okay which is not present as of now in our indexes okay so these are all the index indexes okay which I have okay so what I need to do I'll just run our application okay let me run okay now see it is running and my locally also and it is a swager which is provide the all the you know handling the request okay there's a many way you can use the postman and any anything okay but yeah uh for the demonstration process it is very look feel and very good actually so what I need to do just first create execute okay see there is a no data response is 200 there's a no data that is expected okay what I need to do I just expand this okay now just create a some data okay use that post method try out and remove that ID okay pass it to for example uh elastic search Okay elastic search and Es description and kic okay and then execute once you execute it will get that response see you will get the 200 and also the response document create successfully okay okay now move it to our here and Let me refresh and check it like those indexes has been created or not okay so move it to again uh manage the data View and go to all resource and Es demo see now it is available which was not previously okay so what I need to do I'll just create a index so that I can visible in this kibana as well kibana so what I will do just for say name example mean same name I'll just create it for the visualization okay and point out to this index okay and then save it okay once you confirm you able to see here okay it is selected and now you're able to see the data okay now see one data is inserted for that name is es okay let me show in the Json format and a little bit zoom in for you see name is yes discuss this and author is this right so let me add one more data then one I'll just put one more data you will able to see over here okay so let me back to our here okay change it to as2 for example uh angular okay and then angular angular description and author is same okay let me execute see the response it is fine mine and if you go here it will automatically refresh actually but yeah sometime it will not but yeah you can just refresh see once I refresh I am able to see another data okay same things I can check in here as well in this get all the get all documents okay execute now this time able to see two records right and now what I will do next I will just use I will just update so what I need to update so suppose this one okay this one 788 so if you go to 788 so it have elastic search right so let change it to elastic search to let sech to uh for example B API okay okay and then change it to web API description and author is karik and just execute okay and go back to dashboard Let me refresh see now it change okay also similar you can check check over here as well just execute see it is got changes right also check the another endpoint for example get by the ID pass it to ID get the data yes I have received the data okay so that is also fine work working fine the get API and finally I want to delete okay so let me try to delete as well so that we can make sure our delete also working fine or not okay so this one it's successfully now check here first with the endpoint okay get all the end points now see only one left data is left go back to dashboard now here the previous Lads are two okay now refresh now it's keep only one right so that is working fine absolutely okay okay so one Endo just I'll show you in the debugging mode so that you can understand as well okay so go to the elastic search controller okay put the debugger Point as well and go to the Services because how is working you you have to know like what is the flow actually okay so let me debuger Point here as well okay and let me hit enter uh execute okay once you execute you will able to see the de point once you have1 it will go to your you know elastic search services and here it will call your client and call the search and pass the document okay and then finally you will return the see OB object okay and see this is that object which is we trying to show you like is valid okay okay so I am not able to show you in a zoom mode I think this one yeah I'm not able to show you in the zoom mode okay but yeah I think it's fine for visibility so this is a way and also you will see the menu property you can get it like okay it is successful or not and what is The Happening everything you can capture okay so let me show you see this is the document collection as well you able to see the data as well using the do documents finally F1 f 10 finally get the data and show the data in your API okay like this way so uh that's all for this session okay and uh keep tuning and uh don't forget to subscribe like and share thank you
Info
Channel: DotNet Real world example
Views: 2,080
Rating: undefined out of 5
Keywords: elasticsearch, asp.net core, web api, crud operation, database, programming, tutorial, development, coding, dotnet, asp.net core web api, crud operation in elasticsearch using asp.net core web api, elasticsearch integration, nest elasticsearch c# example, asp.net core elasticsearch logging, elasticsearch in asp.net c#, elasticsearch in asp.net core, elasticsearch integration example, how to integrate elasticsearch, how to implement elasticsearch in .net core
Id: _VEe-XksRlY
Channel Id: undefined
Length: 36min 56sec (2216 seconds)
Published: Mon Nov 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.