ES4: Elastic Search uses C # to add and update documents

Keywords: C# ElasticSearch Windows Database JSON

This is the fourth in the Elastic Search 2.4 series:

Chapter one: ES1: Installing Elastic Search under Windows

The second article: ES2: Elastic Search Cluster Configuration

The third article: ES3: Elastic Search Index

The fourth article: ES4: Elastic Search uses C # to add and update documents

 

Full-text search in Elastic Search engine is a dazzling thing, and index creation is the most important thing. It must be carefully designed. It is recommended to use head plug-in to create index Mapping. For daily updates of index document data, C# client program can be used to synchronize and update data automatically according to plan.

For a database development, C# code has not been written for a long time, dot. net novice, this article simply shares the use of Elastic Search. net client driver to add documents to the index code fragments, details, please refer to the official manual: Elasticsearch.Net and NEST: the .NET clients [5.x] » Introduction

1. Elastic Search's. net Client Driver

ElasticSearch official website provides two. net client drivers, among which Elasticsearch.Net is a very low-level and flexible client driver. Users need to create requests and responses manually; NEST is a high-level client, which still uses Elasticsearch.Net driver internally, and NEST has query DSL (domain specific). Language), which can map all requests and Response objects, is more convenient to use. The interface provided by different versions of NEST driver varies greatly. After being familiar with Nest, you can use Elasticsearch.Net driver to write your own code to avoid updating.

First, download ElastiSearch's. net client driver, open the Tools menu of VS, and install NEST by entering commands through the NuGet package manager console:

PM> Install-Package NEST

After installation, the system refers to three DLL files. The driver versions of the main installation are as follows:

  • Elasticsearch.Net.dll (version 5.0.0.0)
  • Nest.dll (version 5.0.0.0)
  • Newtonsoft.Json.dll (version 9.0.0.0)

2. Simple Use of NEST Driver

1, connect to Elastic Search engine server

Note that the name of the default index must be lowercase. It is recommended that the index name, document type name, and field name be lowercase.

using Nest;    
public static class Setting
{
    public static string strConnectionString=@"http://localhost:9200";
    public static Uri Node
    {
        get
        {
            return new Uri(strConnectionString);
        }
    }
    public static ConnectionSettings ConnectionSettings
    {
        get
        {
            return new ConnectionSettings(Node).DefaultIndex("default");
        }
    }
}

2. Create a data model

Note that the field name of the model is consistent with the field in the index map created. It is recommended to use lowercase letters. The Nest driver provides model attributes that readers can try on their own.

public class MeetupEvents
{
    public long eventid { get; set; }
    public string orignalid { get; set; }
    public string eventname { get; set; }
    public string description { get; set; }
}

3. Update Documents

NEST provides two ways to update documents, one by one and the other in batches. The function PopulateIndex is used to update the index one by one, and the function Bulk PopulateIndex is used to update the index in batches.

Note that when updating the index, the highlighted code specifies that the metafield _id of the index is the primary key eventid of the meetupevent entity;

using Nest;
public class ESProvider
{
    public static ElasticClient client = new ElasticClient(Setting.ConnectionSettings);
    public static string strIndexName = @"meetup".ToLower();
    public static string strDocType = "events".ToLower();

    public bool PopulateIndex(MeetupEvents meetupevent)
    {
        var index = client.Index(meetupevent,i=>i.Index(strIndexName).Type(strDocType).Id(meetupevent.eventid));
        return index.Created;
    }

    public bool BulkPopulateIndex(List<MeetupEvents> posts)
    {
        var bulkRequest = new BulkRequest(strIndexName,strDocType) { Operations = new List<IBulkOperation>() };   
        var idxops = posts.Select(o => new BulkIndexOperation<MeetupEvents>(o) { Id=o.eventid}).Cast<IBulkOperation>().ToList();
        bulkRequest.Operations = idxops;
        var response = client.Bulk(bulkRequest);
        return response.IsValid;
    }
}

4. Perform batch update operations

If the amount of updated data is very large, it is recommended that the index of Elastic Search be updated by paging the data source and batch.

ESProvider es = new ESProvider();
List<MeetupEvents> pbs = new List<MeetupEvents>();

foreach (DataRow dr in MeetupEventsTable.Rows)
{
    MeetupEvents pb = new MeetupEvents();
    pb.eventid = long.Parse(dr["EventID"].ToString());
    pb.orignalid = dr["OriginalID"].ToString();
    pb.eventname = dr["EventName"].ToString();
    pb.description = dr["Description"].ToString();

    pbs.Add(pb);
}          
es.BulkPopulateIndex(pbs);

5. Summarize the use of NEST driver

Connect the NEST client using the following three sections of code:

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

Update/add a single document using the Index method of the client:

Client.Index(student);

Update/add multiple documents using the IndexMany function of the client:

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

Using the Bulk method of client to update documents in batches, we need to construct a BulkRequest parameter according to the List of entities:

client.Bulk(bulkRequest);

 

 

Reference documents:

NEST Usage Guide

Chinese version of the introduction guide to elastic search.net search

Elastic Search NEST Notes

.net ElasticSearch

Posted by touchingvirus on Sat, 20 Apr 2019 15:48:34 -0700