ASP.NET Web API + Elastic search 6.x Quickly Make a Full Text Search

Keywords: ASP.NET ElasticSearch Java Linux

Recently, I want to do a full-text search, imagine using ASP.NET Web API + Elastic search 6.x to achieve.

I searched the information of Elastic search on the Internet. Most of them talked about how to use java to develop on linux platform. A few talked about using c # to develop on windows platform, and the version was Elastic search 5.x. I have no choice but to go to the official website. Here I comb out the tutorials of the official website, hoping to help you all.

 

I. Installation of Elastic search

 

Download the MSI(https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.msi) installation file, double-click the installation after completion, click Next, all default settings.

 

Running Elastic search

 

Open cmd and enter

cd %PROGRAMFILES%\Elastic\Elasticsearch\bin

Enter.

.\elasticsearch.exe

Enter

 

3. Construction of Development Environment

 

1. Building a new webApi project

2. Install NEST to connect Elastic search

 

Open the NuGet package manager console and enter the following commands

Install-Package NEST -Version 6.0.1

Pay attention to the version number when installing. NEST corresponds to Elastic search. This is a pit.

 

3. Connect Elastic search

 

Create a new connection class ClientHelper.cs

 1 public class ClientHelper
 2 {
 3     private static ClientHelper clientHelper = null;
 4     // Default index
 5     public static string DEFAULT_INDEX = "resource";
 6     private ElasticClient Client()
 7     {
 8         var nodes = new Uri[]
 9         {
10             new Uri("http://127.0.0.1:9200")
11         };
12         var pool = new StaticConnectionPool(nodes);
13         var settings = new ConnectionSettings(pool)
14             .DefaultIndex(DEFAULT_INDEX)
15             .PrettyJson();
16             //.BasicAuthentication("elastic", "changeme");
17 
18         return new ElasticClient(settings);
19     }
20 
21     public static ElasticClient getInstance()
22     {
23         if(clientHelper==null){
24             clientHelper = new ClientHelper();
25         }
26         return clientHelper.Client();   
27     }
28 }

 

New mapping class Resource.cs

1 [ElasticsearchType(Name = "resource", IdProperty = "ID")]
2 public class Resource
3 {
4     [Keyword(Name = "id")]
5     public string ID { get; set; }
6 
7     [Text(Name = "name")]
8     public string NAME { get; set; }
9 }

 

4. Adding, deleting, checking and modifying operations

 

New api controller ESController.cs

 1 public class ESController : ApiController
 2 {
 3   // GET: api/ES/1
 4   // Press id Query single record
 5   public Resource Get(string id)
 6   {
 7       var client = ClientHelper.getInstance();
 8       var response = client.Get<Resource>(id, idx => idx.Index(ClientHelper.DEFAULT_INDEX));
 9       return response.Source;
10   } 
11      
12   // POST api/ES   
13   // Importing database data in batches
14   public string Post()
15   {
16       using (DataContext db = new DataContext())
17        {
18           var client = ClientHelper.getInstance();
19           List<Demo>  items= db.demo.ToList();
20           for (int i = 0; i < 100;i++ )
21           {
22              var item = items[i];
23              Resource mod = new Resource();
24              mod.ID = item.ID;
25              mod.NAME = item.NAME;
26              client.Index<Resource>(mod, idx => idx.Index(ClientHelper.DEFAULT_INDEX));
27           }
28       }
29       return "OK";
30    }
31 
32    // PUT api/ES/5
33    // Press id Update single data
34    public Result Put(string id)
35    {
36        var client = ClientHelper.getInstance();
37        var response = client.Update<Resource>(id, idx => idx.Index(ClientHelper.DEFAULT_INDEX));
38        return response.Result; 
39    }
40 
41     // DELETE api/ES/5
42     // Press id Delete single data
43     public Result Delete(string id)
44     {
45       var client = ClientHelper.getInstance();
46       var response = client.Delete<Resource>(id, idx => idx.Index(ClientHelper.DEFAULT_INDEX));
47       return response.Result; 
48     }
49 }

 

New api controller SearchController.cs to provide search services

 1 public class SearchController : ApiController
 2 {
 3     // GET: api/Search/
 4     public List<IHit<Resource>> Get(string id)
 5     {
 6         var client = ClientHelper.getInstance();
 7         var modList = client.Search<Resource>(s => s
 8             .From(0)
 9             .Size(10)
10             .Query(q => q.Term(t => t.NAME, id))
11         );
12         return modList.Hits.ToList();
13     }
14 }

 

5. Give it a try

(1) Import data to Elastic search

POST http://localhost:8389/api/es

 

(2) Query the record with id 1

GET http://localhost:8389/api/es/1

 

(3) Update the record with id 1

PUT http://localhost:8389/api/es/1

 

(4) Delete the record with id 1

DELETE http://localhost:8389/api/es/1

 

(5) Records in the query name

GET http://localhost:8389/api/Search/in

 

A simple full-text indexing service is complete!

Posted by lc on Mon, 21 Jan 2019 11:24:13 -0800