The basic usage of Nest client, nest Client

Source: Internet
Author: User

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 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.