In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the ".net Api how to use Elasticsearch to store documents" related knowledge, the editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this ".net Api how to use Elasticsearch to store documents" article can help you solve the problem.
What is Elasticsearch?
Elasticsearch is a distributed, highly scalable, high real-time search and data analysis engine. It can easily make a large number of data have the ability to search, analyze and explore. Taking full advantage of the horizontal scalability of Elasticsearch can make data more valuable in a production environment. The implementation principle of Elasticsearch is mainly divided into the following steps: first, the user submits the data to the Elasticsearch database, then the corresponding sentence is segmented by the word segmentation controller, and the weight and the result of the word segmentation are stored in the data together. When the user searches for the data, the result is ranked and scored according to the weight, and then the returned result is presented to the user.
In short, this database can be very flexible on your stored data word segmentation and query, can flexibly deal with field content, filter the data you want, more importantly, the database is distributed, can reduce the loss of data caused by a database down.
Hereinafter referred to as Elasticsearch for Es database. Preface | Elasticsearch: authoritative Guide | Elastic
Configure Nest with Nest using Es database
In the C # environment, there is an official extension package Nest for Es, which allows us to use the Es database easily and quickly. First of all, after we have created a new project, we need to install the NEST package for the project in Nuget package management.
After installing the NEST package, you need to create a new Es configuration class EsConfig.cs. Here we only use the simplest account, password and database address.
/ ES configuration class / public class EsConfig {/ account / public string username {get; set;} / password / public string password {get; set } / ES address / public string url {get; set;}}
Once you have the configuration class, you need to configure Es when the program starts. First of all, create a new Es client interface class IElasticsearchClient.cs.
/ ES client / public interface IElasticsearchClient {/ get ElasticClient / ElasticClient GetClient (); / specify index to get ElasticClient / ElasticClient GetClient (string indexName);}
In configuring the implementation class ElasticsearchClient.cs for the interface, we use the form of dependency injection of IOptions to configure the configuration file, which is usually applicable to the configuration of API type projects.
We can also use the form of direct _ EsConfig = Configuration.GetSection ("EsConfig"). Get (); to read the configuration file for configuration
/ ES client / public class ElasticsearchClient: IElasticsearchClient {public EsConfig _ EsConfig; / constructor / public ElasticsearchClient (IOptions esConfig) {_ EsConfig = esConfig.Value } / get elastic client / public ElasticClient GetClient () {if (_ EsConfig = = null | | _ EsConfig.url = = null | | _ EsConfig.url = = ") {throw new Exception (" urls can not be null ") } return GetClient (_ EsConfig.url, "") } / specify index to get ElasticClient / public ElasticClient GetClient (string indexName) {if (_ EsConfig = = null | | _ EsConfig.url = = null | | _ EsConfig.url = = ") {throw new Exception (" urls can not be null ") } return GetClient (_ EsConfig.url, indexName) } / get ElasticClient / private ElasticClient GetClient (string url, string defaultIndex = "") {if (string.IsNullOrWhiteSpace (url)) {throw new Exception ("urls can not be null") according to url } var uri = new Uri (url); var connectionSetting = new ConnectionSettings (uri); if (! string.IsNullOrWhiteSpace (url)) {connectionSetting.DefaultIndex (defaultIndex);} connectionSetting.BasicAuthentication (_ EsConfig.username, _ EsConfig.password); / / set account password return new ElasticClient (connectionSetting) } / obtain ElasticClient / private ElasticClient GetClient according to urls (string [] urls, string defaultIndex = "") {if (urls = = null | | urls.Length
< 1) { throw new Exception("urls can not be null"); } var uris = urls.Select(p =>New Uri (p)) .ToArray (); var connectionPool = new SniffingConnectionPool (uris); var connectionSetting = new ConnectionSettings (connectionPool); if (! string.IsNullOrWhiteSpace (defaultIndex)) {connectionSetting.DefaultIndex (defaultIndex);} return new ElasticClient (connectionSetting);}}
Since it's dependency injection, don't forget to inject it in Startup.cs.
Services.Configure (Configuration.GetSection ("EsConfig")); operate the database
Usually before we operate the database, we usually have a "build database", "build table" and other operations, in Es we can be understood as an introduction to the creation of index foundation | Elasticsearch: authoritative Guide | Elastic.
Since the Es database does not have a good IDE to manage it, we usually use code to create the table, so first create a new Es extension class to create the table ElasticClientExtension.cs
/ ElasticClient extension class / public static class ElasticClientExtension {/ create an index / public static bool CreateIndex (this ElasticClient elasticClient, string indexName = "", int numberOfShards = 10 Int numberOfReplicas = 1) where T: class {if (string.IsNullOrWhiteSpace (indexName)) {indexName = typeof (T) .Name } if (elasticClient.Indices.Exists (indexName) .Exists) {return false } else {var indexState = new IndexState () {Settings = new IndexSettings () {NumberOfReplicas = numberOfReplicas, NumberOfShards = numberOfShards,},} Var response = elasticClient.Indices.Create (indexName, p = > p.InitializeUsing (indexState) .Map (p = > p.AutoMap ()); return response.Acknowledged;}
Then we need to create our method for the index, I use an index to create a new method, a new ElaticSearchBase.cs, here we assume that we want to create an index called XXX, first we need to define a class called XXX, I have created a new primary key and a xml field to store xml data. Then I create a new client Client_XXX in ElaticSearchBase.cs that is dedicated to connecting to the XXX index. If there is a xxx in the database, then connect directly, if it does not exist, then create a new connection.
Public class XXX {public int xid {get; set;} [Text (Name = "xml")] public string xml {get; set;}} public class ElaticSearchBase {private IElasticsearchClient _ client; public ElaticSearchBase (IElasticsearchClient client) {_ client = client } / XXX document index / public ElasticClient Client_XXX = > GetXXX (); private ElasticClient GetXXX () {/ / if there is a xxx in the database, connect directly; if it does not exist, connect var client = _ client.GetClient ("XXX") If (! client.Indices.Exists ("XXX") .Exists) {client.CreateIndex ("XXX");} return client;}}
Next we come to the practical part:
Add private ElaticSearchBase _ es = new ElaticSearchBase (_ client); / / instantiate object var xxx1 = new XXX () {tempid = 1je XML = "xmlstring"}; / / Store xxx1var res = _ es.Client_XXX.IndexDocument (xxx1); / / get Es primary key var esid = res.Id
Query
Var res = _ es.Client_XXX.Get (esid)
Delete
Var res = _ es.Client_XXX.Delete (esid)
Modify
/ / instantiate the object var xxx2 = new XXX () {tempid = 2 xmlstring2 = "xmlstring2"}; var upRes= _ es.Client_XXX.Update (ex.xmlid, upt = > upt.Doc (xxx2)); this is the end of the introduction on "how to use Elasticsearch to store documents in .net Api". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.