The basic usage of Nest client, nest Client
Install the related Dll of Nest through Nuget, and then we can start,
1. initialize the Nest Client
string indexName = "customer";Uri uri = new Uri("http://localhost:9200");ConnectionSettings settings = new ConnectionSettings(uri);settings.DisableDirectStreaming().DefaultIndex(indexName);ElasticClient client = new ElasticClient(settings);
2. Create a Customer Index
dynamic response = client.GetIndex(indexName); if(!response.IsValid) { response = client.CreateIndex("customer"); Console.WriteLine(response); }
3. Create a document
Var company = new Company {CompanyID = 2, Name = "IBM"}; // create a document and specify the index, and the Document ID response = client. index (company, I => I. index (indexName ). id (company. companyID); // create a document and specify the index, document type, and Document ID. ElasticSearch will generate a random ID response = client. index (company, I => I. index (indexName); // The specified Index is not displayed here, so the client used to initialize the default Index. If there is no default Index, the error response = client will occur. index (company );
4. modify document 1
Modify the document according to CompanyID. If a file with a CompanyID of 2 exists in the store, it will replace the old one with a new one. Otherwise, it will be created.
Therefore, it is more appropriate to create a style modification.
Company = new Company {CompanyID = 2, Name = "Lenovo"}; // modify the document response = client. index (company, I => I. id (company. companyID ));
5. Modify the document of the specified version number (implementation of optimistic locks)
Company = new Company {CompanyID = 2, Name = "Lenovo"}; // modify the document response = client of the specified version. index (company, I => I. id (company. companyID ). version (1 ));
6. delete a document
// Delete the document response = client according to specific conditions. delete <Company> (company. companyID, d => d. index (indexName); // Delete the document response = client. delete <Company> (company. companyID );
7. delete an index
// Delete the index response = client. DeleteIndex (indexName) according to the name );
8. Create documents in batches
The corresponding index is specified here. If the specified index is not displayed, the default index is used,
// Batch create the document BulkDescriptor descriptor = new BulkDescriptor (); descriptor. index <Company> (op => op. document (new Company {CompanyID = 1, Name = "IBM "}). index ("a"); descriptor. index <Company> (op => op. document (new Company {CompanyID = 2, Name = "IBM "}). index ("B"); descriptor. index <Company> (op => op. document (new Company {CompanyID = 3, Name = "IBM "}). index ("c"); response = client. bulk (descriptor );