Elasticsearch Introduction
Elasticsearch (ES) is a Lucene-based open source search engine, which is stable, reliable, fast, and also has a good level of scalability, is specifically designed for the distributed environment.
What is Elasticsearch?
Elasticsearch, an open source search engine based on Apache Lucene (TM), can be thought of as the most advanced, best-performing and most functional search engine library in the world, whether in open source or proprietary domain.
However, Lucene is just a library. To play a powerful role, you need to integrate it into your app using C #. Lucene is very complex, and you need to understand the search knowledge to understand how it works.
Elasticsearch is also written in Java and uses Lucene to index and search, but its purpose is to make full-text search simple and to hide the complexities of lucene through simple, consistent restful APIs.
However, Elasticsearch is not only Lucene and full-text search engine, it also provides:
Distributed real-time file storage with each field indexed and searchable
Distributed search engine for real-time analysis
Can scale to hundreds of servers, processing petabytes of structured or unstructured data
And, all of these features are integrated into a single server, and your app can interact with it through a simple restful API, a client in a variety of languages, or even a command line. Getting Started Elasticsearch is very simple, it provides many reasonable defaults, and hides the complex search engine theory for beginners. It is available out of the box (installation ready to use) and requires little learning to be used in a production environment. Elasticsearch is licensed under Apache 2 License and can be downloaded, used and modified for free.
With the accumulation of knowledge, you can customize Elasticsearch's advanced features according to different problem areas, all of which are configurable and flexible to configure.
Using C # to manipulate ES
Nest is a high-level client that can map all request and response objects, have a strongly typed query DSL (domain-specific language), and can use. NET features such as covariant, Auto Mapping of Pocos, Nest is still used internally by the Elasticsearch.net client. Elasticsearch.net (NEST) client provides a strong type query DSL, convenient for users to use, source code download.
First, how to install nest
Open the Tools menu for VS, and through the NuGet Package Manager console, enter the following command to install Nest
Install-package NEST
The following three DLLs are referenced after installation
Elasticsearch.Net.dll (2.4.4) Nest.dll (2.4.4) Newtonsoft.Json.dll (9.0 versions)
Second, link Elasticsearch
You can use connection pooling to link to a elasticsearch cluster through a single node or by specifying multiple nodes, using a connection pool that is more advantageous than a single node linking to Elasticsearch, such as support for load balancing, failover, and so on.
Via a single-point link:
var node = new Uri ("http://myserver:9200");
var settings = new ConnectionSettings (node);
var client = new Elasticclient (settings);
Through connection pooling Links:
var nodes = new uri[]
{
New Uri ("http://myserver1:9200"),
New Uri ("http://myserver2:9200"),
New Uri ("http://myserver3:9200")
};
var pool = new Staticconnectionpool (nodes);
var settings = new ConnectionSettings (pool);
var client = new Elasticclient (settings);
NEST Index
In order to know which index the request needs to manipulate, the Elasticsearch API expects to receive one or more index names as part of the request.
One, the specified index
1. You can specify the default index by using ConnectionSettings. Defaultindex (). When a specific index is not specified in a request, Nest will request a default index.
var settings = new ConnectionSettings ()
. Defaultindex ("Defaultindex");
2. ConnectionSettings can be used with. Mapdefaulttypeindices () to specify an index that is mapped to a CLR type.
var settings = new ConnectionSettings ()
. Mapdefaulttypeindices (m = m
. Add (typeof (Project), "projects")
);
Note: Pass. Mapdefaulttypeindices () specifies that the index should take precedence over the pass. Defaultindex () specifies the index and is more suitable for simple objects (POCO)
3. You can also display the specified index name for the request, for example:
var response = client. Index (Student, S=>s.index ("Db_test"));
var result = client. Search<student> (s = S.index ("Db_test"));
var result = client. Delete<student> (NULL, S = = S.index ("Db_test"));
Note: This priority is the highest when the actual specified index name for the request is higher than the index specified in both of these ways.
4. Some Elasticsearch APIs (such as query) can use one or more index names or send requests using the _all special flag, requesting multiple or all nodes on the nest
Request a single node
var singlestring = Nest.Indices.Index ("db_studnet");
var singletyped = nest.indices.index<student> ();
Isearchrequest singlestringrequest = new searchdescriptor<student> (). Index (singlestring);
Isearchrequest singletypedrequest = new searchdescriptor<student> (). Index (singletyped);
Request multiple nodes
var manystrings = Nest.Indices.Index ("Db_studnet", "db_other_student");
var manytypes = nest.indices.index<student> (). And<otherstudent> ();
Isearchrequest manystringrequest = new searchdescriptor<student> (). Index (manystrings);
Isearchrequest manytypedrequest = new searchdescriptor<student> (). Index (manytypes);
Request all nodes
var indicesall = Nest.Indices.All;
var allindices = Nest.Indices.AllIndices;
Isearchrequest indicesallrequest = new searchdescriptor<student> (). Index (Indicesall);
Isearchrequest allindicesrequest = new searchdescriptor<student> (). Index (allindices);
Second, create an index
The Elasticsearch API allows you to create indexes while configuring the index, for example:
var descriptor = new Createindexdescriptor ("Db_student")
. Settings (s = s.numberofshards (5). Numberofreplicas (1));
Client. CreateIndex (descriptor);
This specifies that the number of shards for this index is 5 and the number of replicas is 1.
Third, delete the index
The Elasticsearch API allows you to delete an index, for example:
var descriptor = new Deleteindexdescriptor ("Db_student"). Index ("Db_student");
Client. Deleteindex (Descriptor)
Here is the index name "db_student" to be deleted, and the following are more delete use cases:
Deletes all indexes under the node that contains the specified index
var descriptor = new Deleteindexdescriptor ("Db_student"). Allindices ();
NEST Mapping
Nest provides a variety of mapping methods, described here under attribute custom mapping.
First, simple implementation
1. Define the Poco required by the business and specify the required attribute
[Elasticsearchtype (Name = "student")]
public class Student
{
[Nest.string (Index = fieldindexoption.notanalyzed)]
public string Id {get; set;}
[Nest.string (Analyzer = "standard")]
public string Name {get; set;}
[Nest.string (Analyzer = "standard")]
public string Description {get; set;}
Public datetime datetime {get; set;}
}
2, then we pass. Automap () to implement the mapping
var descriptor = new Createindexdescriptor ("Db_student")
. Settings (s = s.numberofshards (5). Numberofreplicas (1))
. Mappings (ms = MS
. map<student> (M = M.automap ())
);
Client. CreateIndex (descriptor);
Note: Pass. Properties () can override mappings defined through attribute
Ii. Introduction of attribute
1, Stringattribute
2, Numberattribute
3, Booleanattribute
4, Dateattribute
5, Objectattribute
NEST Search
Nest provides support for lambda chain query DLS (domain-specific language), following a simple implementation and a brief description of each query.
First, simple implementation
1, define searchdescriptor, facilitate the implementation of complex business in the project
var query = new nest.searchdescriptor<models.esobject> ();
var result = client. search<student> (x = query)
2. Retrieve the title and content containing key, and the author is not equal to "Pretty Woman" document
Query. Query (q =
Q.bool (b =
B.must (M =
M.multimatch (t = t.fields (f = f.field (obj = obj). Title). Field (obj = obj. Content)). Query (Key))
)
. Mustnot (M =
M.querystring (t = t.fields (f = f.field (obj = obj). Author)). Query ("Wenli"))
)
)
);
Attention:
If Elasticsearch uses the default participle, the title and content attribute are [nest.string (Analyzer = "standard")]
If Elasticsearch is using IK participle, the title and content attribute are [nest.string (analyzer = "Ikmaxword")] or [nest.string (analyzer = "ik _smart ")]
Author's attribute is [nest.string (Index = fieldindexoption.notanalyzed)] and disables the use of analyzers
3, filter author equal to "History Creek" document
Query. Postfilter (x = x.term (t = = T.field (obj = obj). Author). Value ("Wenli"));
4, filter the author equals "History Creek" or equal to "friendship boat" document, matching multiple authors between spaces separated by space
Query. Postfilter (x = x.querystring (t = t.fields (f + f.field (obj = = obj). Author)). Query ("Wenli Yswenli"));
5. Filter the number of documents between 1~100
Query. Postfilter (x = x.range (t = = T.field (obj = obj). Number). Greaterthanorequals (1). Lessthanorequals (100)));
6, sort, according to the score flashback arrangement
Query. Sort (x = X.field ("_score", Nest.SortOrder.Descending));
7. Define highlighting Styles and fields
Query. Highlight (H = h
. Pretags ("<b>")
. Posttags ("</b>")
. Fields (
f = F.field (obj = obj. Title),
f = F.field (obj = obj. Content),
f = F.field ("_all")
)
);
8, assemble the contents of the query, organize the data, convenient to call the previous paragraph
var list = result. Hits.select (c = new Models.esobject ()
{
Id = C.source.id,
Title = C.highlights = = null? C.source.title:c.highlights.keys.contains ("Title")? String. Join ("", c.highlights["title"]. Highlights): C.source.title,//highlight content, a record that appears several times
Content = C.highlights = = null? C.source.content:c.highlights.keys.contains ("Content")? String. Join ("", c.highlights["content"]. Highlights): C.source.content,//highlight content, a record that appears several times
Author = C.source.author,
Number = C.source.number,
Isdisplay = C.source.isdisplay,
Tags = C.source.tags,
Comments = C.source.comments,
DateTime = C.source.datetime,
})
Second, query DSL introduction
To be sorted out ...
Elasticsearch.net Document
Document actions include adding/updating documents, updating documents locally, deleting documents, and corresponding bulk operations documentation methods.
I. Add/update documents and bulk operations
Add/update a single document
Client.index (student);
Bulk Add/Update documents
var list = new list<student> (); 2 3 client. Indexmany<student> (list);
Second, the partial Update single document and batch operation
Update a single document locally
Client. Update<student, Object> ("002", UpT = UpT. Doc (New {Name = "Wenli"});
Update Bulk documents locally
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. deleting documents and bulk operations
Delete a single document
Client. Delete<student> ("001");
Delete a document in bulk
var ids = new List<string> () {"001", "002"};
var bulkquest = new Bulkrequest () {Operations = new list<ibulkoperation> ()};
foreach (Var v in IDs)
{
BULKQUEST.OPERATIONS.ADD (New bulkdeleteoperation<student> (v));
}
var result = client. Bulk (bulkquest);