C # How to Use ES,

Source: Internet
Author: User
Tags automap createindex

C # How to Use ES,

Introduction to Elasticsearch

Elasticsearch (ES) is an open-source search engine based on Apache Lucene (TM, lucene can be considered as the most advanced search engine library with the best performance and the most comprehensive functions so far.

However, Lucene is only a library. To play its powerful role, you need to use C # To integrate it into your application. Lucene is very complicated. You need to have a deep understanding of the retrieval knowledge to understand how it works.
Elasticsearch uses Java to compile and use Lucene to create indexes and implement search functions. However, it aims to simplify full-text search and hide the complexity of Lucene through simple and coherent RESTful APIs.
However, Elasticsearch is not only a Lucene and full-text search engine, but also provides:

  • Distributed Real-time file storage. Each field is indexed and searchable.
  • Distributed search engine for real-time analysis
  • It can be expanded to hundreds of servers to process PB-level structured or unstructured data.

In addition, all these functions are integrated into a server. Your application can interact with it through simple RESTful APIs, clients in various languages, and even command lines. Elasticsearch is easy to use. It provides many reasonable default values and hides complicated search engine theories from beginners. It is out-of-the-box (ready for installation) and can be used in the production environment with little learning. Elasticsearch is licensed under Apache 2 license and can be downloaded, used, and modified for free.
With the accumulation of knowledge, you can customize the advanced features of Elasticsearch based on different problem fields. All of this is configurable and flexible.

The above content comes from [Baidu encyclopedia]

For more information about elasticsearch, see http://88250.b3log.org/full-text-search-elasticsearch#b3_solo_h3_0.

 

Use C # To operate ES

NEST is a high-level client that can map all request and response objects. It has a strong type query DSL (domain-specific language) and can be used. net features such as covariant, Auto Mapping Of POCOs, NEST is still using Elasticsearch.. Net client. The elasticsearch.net (NEST) Client provides a strong-type query DSL for easy use and source code download.

I. How to install NEST

Open the VS tool menu and run the following command on the NuGet Package Manager Console to install NEST:

Install-Package NEST

After installation, the following three DLL

Elasticsearch. Net. dll (2.4.4) Nest. dll (2.4.4) Newtonsoft. Json. dll (9.0)
2. Link elasticsearch

You can use a connection pool to link a single node or multiple nodes to an Elasticsearch cluster. Using a connection pool is more advantageous than connecting a single node to Elasticsearch, such as server Load balancer and failover.

Through single-point link:

1 var node = new Uri("http://myserver:9200");2 var settings = new ConnectionSettings(node);3 var client = new ElasticClient(settings);

 

Link through the connection pool:

 1 var nodes = new Uri[] 2 { 3     new Uri("http://myserver1:9200"), 4     new Uri("http://myserver2:9200"), 5     new Uri("http://myserver3:9200") 6 }; 7  8 var pool = new StaticConnectionPool(nodes); 9 var settings = new ConnectionSettings(pool);10 var client = new ElasticClient(settings);

 

NEST Index

To know which index the request needs to operate, Elasticsearch API expects to receive one or more index names as part of the request.

1. Specify Indexes

1. You can use ConnectionSettings to use. DefaultIndex () to specify the default index. NEST requests the default index when no specific index is specified in a request.

1 var settings = new ConnectionSettings()2     .DefaultIndex("defaultindex");

2. You can use ConnectionSettings to use. mapdefatypetypeindices () to specify the index mapped to the CLR type.

1 var settings = new ConnectionSettings()2     .MapDefaultTypeIndices(m => m3         .Add(typeof(Project), "projects")4     );

 

Note: Using. mapdefatypetypeindices () to specify an index has a higher priority than using. DefaultIndex () to specify an index, and is more suitable for simple objects (POCO)

3. You can also specify the index name for the request, for example:

1 var response = client.Index(student, s=>s.Index("db_test"));2 var result = client.Search<Student>(s => s.Index("db_test"));3 var result = client.Delete<Student>(null, s => s.Index("db_test"));4 ……

 

Note: When an index name is specified for a request, the priority is the highest, which is higher than the index specified in the preceding two methods.

4. Some Elasticsearch APIs (such as query) can use one or more index names or use the _ all special flag to send requests to multiple or all nodes on the NEST.

1 // request a single node 2 var singleString = Nest. indices. index ("db_studnet"); 3 var singleTyped = Nest. indices. index <Student> (); 4 5 ISearchRequest singleStringRequest = new SearchDescriptor <Student> (). index (singleString); 6 ISearchRequest singleTypedRequest = new SearchDescriptor <Student> (). index (singleTyped); 7 8 // request multiple nodes 9 var manyStrings = Nest. indices. index ("db_studnet", "db_other_student"); 10 var manyTypes = Nest. indices. index <Student> (). and <OtherStudent> (); 11 12 ISearchRequest manyStringRequest = new SearchDescriptor <Student> (). index (manyStrings); 13 ISearchRequest manyTypedRequest = new SearchDescriptor <Student> (). index (manyTypes); 14 15 // request all nodes 16 var indicesAll = Nest. indices. all; 17 var allIndices = Nest. indices. allIndices; 18 19 ISearchRequest indicesAllRequest = new SearchDescriptor <Student> (). index (indicesAll); 20 ISearchRequest allIndicesRequest = new SearchDescriptor <Student> (). index (allIndices );

 

2. Create an index

The Elasticsearch API allows you to configure indexes when creating indexes. For example:

1 var descriptor = new CreateIndexDescriptor("db_student")2     .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1));3 4 client.CreateIndex(descriptor);

 

The number of parts and the number of copies of the index are 5.

Iii. Deleting an index

Elasticsearch API allows you to delete indexes, for example:

1 var descriptor = new DeleteIndexDescriptor("db_student").Index("db_student");2 3 client.DeleteIndex(descriptor)

 

The name of the index to be deleted is "db_student". Here are more Delete cases:

1 // Delete All indexes under the node where the specified index is located. 2 var descriptor = new DeleteIndexDescriptor ("db_student"). AllIndices ();

 

NEST Mapping

NEST provides multiple ing methods. Here we will introduce the custom Attribute ing through Attribute.

1. Simple implementation

1. Define the POCO required by the business and specify the Attribute.

 1 [ElasticsearchType(Name = "student")] 2 public class Student 3 { 4     [Nest.String(Index = FieldIndexOption.NotAnalyzed)] 5     public string Id { get; set; } 6  7     [Nest.String(Analyzer = "standard")] 8     public string Name { get; set; } 9 10     [Nest.String(Analyzer = "standard")]11     public string Description { get; set; }12 13     public DateTime DateTime { get; set; }14 }

 

2. Then we use. AutoMap () to implement ing.

1 var descriptor = new CreateIndexDescriptor("db_student")2     .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1))3     .Mappings(ms => ms4         .Map<Student>(m => m.AutoMap())5     );6 7 client.CreateIndex(descriptor);

 

Note: You can use. Properties () to override the Attribute ing defined by Attribute.

II. Introduction to Attribute

1. StringAttribute

Attribute name Value Type Description
Analyzer String Analyzer name. values include standard, simple, whitespace, stop, keyward, pattern, language, snowball, and custom. For more information about analyzer, click Elasticsearch Analyzers.
Boost Double Weighted value. The higher the value, the higher the score.
NullValue String Default value when the data is NULL during document insertion
Index FieldIndexOption Whether to use analyzer. FieldIndexOption. Analyzed is used by default. You are not allowed to use the analyzer FieldIndexOption. NotAnalyzed.

2. NumberAttribute

Attribute name Value Type Description
Type NumberType The constructor parameter, which specifies the type of the current attribute, such as NumberType. Default, Float, Double, Integer, Long, Short, and Byte.
Boost Double Weighted value. The higher the value, the higher the score.
NullValue Double Default value when the data is NULL during document insertion

3. BooleanAttribute

Attribute name Value Type Description
Boost Double Weighted value. The higher the value, the higher the score.
NullValue Double Default value when the data is NULL during document insertion

4. DateAttribute

Attribute name Value Type Description
Boost Double Weighted value. The higher the value, the higher the score.
NullValue String Default value when the data is NULL during document insertion
Format String  

5. ObjectAttribute

Attribute name Value Type Description
Type String/Type The constructor parameter that specifies the type T of the current attribute.
Dynamic DynamicMapping  
NEST Search

NEST provides support for Lambda chained query DLS (domain-specific language). The following is a simple implementation and a brief description of each query.

1. Simple implementation

1. Define SearchDescriptor to facilitate the implementation of complex services in the project

1 var query = new Nest.SearchDescriptor<Models.ESObject>();2 3 var result = client.Search<Student>(x => query)

2. Search for documents whose title and content contain keys and whose author is not equal to "beautiful man"

 1 query.Query(q => 2     q.Bool(b => 3         b.Must(m => 4             m.MultiMatch(t => t.Fields(f => f.Field(obj => obj.Title).Field(obj => obj.Content)).Query(key)) 5         ) 6         .MustNot(m => 7             m.QueryString(t => t.Fields(f => f.Field(obj => obj.Author)).Query("wenli")) 8         ) 9     )10 );
 

Note:

If Elasticsearch uses the default word segmentation, the attribute of Title and Content is [Nest. String (Analyzer = "standard")]

If Elasticsearch uses IK word segmentation, the attribute of Title and Content is [Nest. String (Analyzer = "ikmaxword")] or [Nest. String (Analyzer = "ik_smart")]

Author's attribute is [Nest. String (Index = FieldIndexOption. NotAnalyzed)]. analyzer is prohibited.

3. Filter documents whose authors are equal to "historical rivers"

query.PostFilter(x => x.Term(t => t.Field(obj => obj.Author).Value("wenli")));

4. Filter documents from which the author is equal to "historical river" or "Friendship boat". Separate multiple authors with spaces.

1 query.PostFilter(x => x.QueryString(t => t.Fields(f => f.Field(obj => obj.Author)).Query("wenli yswenli")));

5. The number of filters is 1 ~ Documents between 100

1 query.PostFilter(x => x.Range(t => t.Field(obj => obj.Number).GreaterThanOrEquals(1).LessThanOrEquals(100)));

 

6. sort by score in reverse order

1 query.Sort(x => x.Field("_score", Nest.SortOrder.Descending));

 

7. Define highlight styles and fields

1 query.Highlight(h => h2     .PreTags("<b>")3     .PostTags("</b>")4     .Fields(5         f => f.Field(obj => obj.Title),6         f => f.Field(obj => obj.Content),7         f => f.Field("_all")8     )9 );

 

8. Assemble the query content and organize the data to facilitate the call in the previous section.

1 var list = result. Hits. Select (c => new Models. ESObject () 2 {3 Id = c. Source. Id, 4 Title = c. Highlights = null? C. Source. Title: c. Highlights. Keys. Contains ("title ")? String. join ("", c. highlights ["title"]. highlights): c. source. title, // highlighted Content. A record contains 5 Content = c. highlights = null? C. Source. Content: c. Highlights. Keys. Contains ("content ")? String. join ("", c. highlights ["content"]. highlights): c. source. content, // The highlighted Content, which appears several times in a record. 6 Author = c. source. author, 7 Number = c. source. number, 8 IsDisplay = c. source. isDisplay, 9 Tags = c. source. tags, 10 Comments = c. source. comments, 11 DateTime = c. source. dateTime, 12 })

 

II. Introduction to query DSL

Waiting for sorting ......
 

Elasticsearch.net Document

Document operations include adding/updating documents, updating local documents, deleting documents, and corresponding batch operation document methods.

1. Add/update documents and Perform Batch operations

Add/update a single document

1 Client.Index(student);

 

Batch Add/update documents

1 var list = new List<Student>();2 3 client.IndexMany<Student>(list);

 

Ii. Update a single document locally and Perform Batch operations

Update a single document locally

1 client.Update<Student, object>("002", upt => upt.Doc(new { Name = "wenli" }));

 

Partial update batch documentation

var ids = new List<string>() { "002" };var bulkQuest = new BulkRequest() { Operations = new List<IBulkOperation>() };foreach (var v in ids){    var operation = new BulkUpdateOperation<Student, object>(v);    operation.Doc = new { Name = "wenli" };    bulkQuest.Operations.Add(operation);}var result = client.Bulk(bulkQuest);

 

Iii. delete documents and Perform Batch operations

Delete a single document

1 client.Delete<Student>("001");

 

Batch delete documents

 1 var ids = new List<string>() { "001", "002" }; 2  3 var bulkQuest = new BulkRequest() { Operations = new List<IBulkOperation>() }; 4  5 foreach (var v in ids) 6 { 7     bulkQuest.Operations.Add(new BulkDeleteOperation<Student>(v)); 8 } 9 10 var result = client.Bulk(bulkQuest);

 

 


Reprinted please indicate the source of this article: http://www.cnblogs.com/yswenli/
More welcome to my github: https://github.com/yswenli
If you find any questions or suggestions in this article, feel free to contact us ~



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.