Using ElasticSearch in. Net Core

Keywords: ElasticSearch

There are two versions of ElasticSearch in. Net, Elasticsearch.Net (low level)   and   NEST (Advanced). NEST is recommended. The lower version is more flexible. The water is too deep to grasp. It should be noted that the version number used must be consistent with the ElasticSearch server version number.

1, Connection pool

1.1 SingleNodeConnectionPool single node connection pool

It is suitable for the case of only one node. When the connection pool type is not explicitly specified in the ConnectionSettings constructor, it defaults to this connection pool. This connection pool does not mark whether the node is alive or not

1.2 static connection pool

It is suitable for multiple nodes. It maintains a static list of node hosts. Before use, ping the node to confirm whether the node is alive. If a node fails to process a request, it will be marked as a dead node, and the node will be "closed". After "closed", it will process the request again. If it still fails, the "closed" time will be longer.

1.3 SniffingConnectionPool sniffing connection pool

It inherits from the static connection pool and has the Ping feature of the static connection pool. The difference is that it is dynamic. The user provides hosts seeds, while the client will sniff these hosts and discover other nodes of the cluster. When a node is added or removed from the cluster, the client updates accordingly.

1.4 StickyConnectionPool viscous connection pool

Select an available node as the request master node, which supports ping and does not support sniffing

  1.5   StickySniffingConnectionPool   Sticky sniff connection pool

Select an available node as the request master node, which supports ping and sniffing

2, Infuse

  It is officially recommended to inject ElasticClient in a single case.

            services.AddSingleton<IElasticClient>(provider =>
                var connectionPool = new SingleNodeConnectionPool(new Uri(""));
                var connectionSetting = new ConnectionSettings(connectionPool).DisableDirectStreaming();
                return new ElasticClient(connectionSetting);

3, Add, delete, modify and query

3.1 add

3.1.1 single addition

var user = new User()
    Id = 1,
    Age = 18,
    Name = "MicroHeart",
    Gender = true
//First kind
var resp = client.Index(user, s => s.Index(indexName));//Specifies the operation index name //Second connectionSettings.DefaultIndex(indexName);//Specifies the default index name var client = new ElasticClient(connectionSettings); resp = client.IndexDocument(user);//Operation default index

3.1.2 batch addition

//First kind
var resp = client.IndexMany(new List<User>() { user }); //Operation default index
resp = client.IndexMany(new List<User>() { user }, indexName); //Operation specifies the index
resp = client.Bulk(b => b.Index(indexName).IndexMany(users, (desc, user) => desc.Index(user.Gender ? "boy" : "girl")));
var bulkAllObservable = client.BulkAll(users, b => b
                            .BackOffTime("30s") //Retry interval
                            .BackOffRetries(2)  //retry count
                            .Size(1000) //Number of documents per request

IndexMany: all documents are requested in a single HTTP, so this is not the recommended method for very large document collections.

Bulk: all documents are requested in a single HTTP. If you need to index and control many documents, you can use this method. In the example, the boy is inserted into the "boy" index and the girl is added to the "girl" index

BulkAll: it is suitable for multiple large document collections. In multiple HTTP requests, documents are operated in batches, with built-in retry mechanism.

All the above insertion methods are based on_ The value of Id determines whether the document exists (here, because the user has an attribute of Id, the default Es takes the attribute value of Id as the value of _Id). If the document already exists, it will be updated, otherwise it will be added.

Judge whether the addition is successful by returning the IsValid property of the object.


3.2 deletion

var resp = client.Delete<User>(1, d => d.Index(indexName));//Single delete
var resp = client.DeleteByQuery<User>(x => x.Index(indexName).Query(q => q.Range(r => r.Field(f => f.Age).LessThan(18)))); //Delete children younger than 18

The returned object contains

Deleted: indicates the number of deleted.  

Failures: delete failed collections.


3.3 query

3.3.1 get single by Id

var resp = client.Get<User>(3, d => d.Index(indexName));

The returned type is   Getresponse < user >, including the following properties.

Index: indicates the index,

type: the value is fixed to "_doc",

Found: indicates whether the document is found,

Version: the version number of the document,

source: stored data user


3.3.2 get multiple Id sets

 var resp = client.GetMany<User>(new List<long>() { 19L, 20L, 21L }, indexName);

3.3.3 query by criteria

var resp = client.Search<User>(x => x.Index(indexName)
                                     .Query(q => q.Range(r => r.Field(f => f.Age).GreaterThan(18)) &&
                                                 q.Range(r => r.Field(f => f.Age).LessThan(30)) &&
                                                 q.Term(t => t.Gender, true)));

The returned type is isearchresponse < user >, including the following properties.

TimedOut: whether the query timed out

Shards: how many slices did you complete this search query

Took: time consumed by this query

MaxScore: the maximum score of the most qualified document in this query

Total: the total number of matching query criteria

Hits: a collection of stored data

The example is just a simple query. ES is designed for query, so its query is very flexible, which will be discussed separately later.


  3.4 update

var user = new User()
    Age = 18,
    Gender = false,
    Name = "test"
var resp4 = client.Update<User>(20, u => u.Index(indexName).Doc(user));

  The returned IsValid indicates whether the update was successful.


  Packaged into an operational warehouse, link



Posted by Sportfishing on Tue, 30 Nov 2021 15:12:47 -0800