当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Web API + Elasticsearch 6.x 快速做个全文搜索

ASP.NET Web API + Elasticsearch 6.x 快速做个全文搜索

2018年12月24日  | 移动技术网IT编程  | 我要评论

国历农历转换,麦道夫黑洞,ics频道节目表

最近想做个全文搜索,设想用 asp.net web api + elasticsearch 6.x 来实现。

网上搜了下 elasticsearch 的资料,大部分是讲 linux 平台下如何用 java 来开发,有少量讲在 windows 平台下用 c# 开发的,且版本是 elasticsearch 5.x 。无奈上官网撸串,这里梳理下官网的教程,希望对大家有所帮助。

 

一、elasticsearch 的安装 

 

下载  msi(https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.1.msi)安装文件,完成后双击安装,点下一步,全部默认设置。

 

二、运行 elasticsearch

 

打开 cmd,输入

cd %programfiles%\elastic\elasticsearch\bin

回车,输入 

.\elasticsearch.exe

回车

 

三、开发环境搭建

 

1、新建一个 webapi 工程

2、安装 nest,用来连接 elasticsearch

 

打开 nuget 包管理器控制台,输入以下命令

install-package nest -version 6.0.1

注意安装时带上版本号,nest 与 elasticsearch 版本对应,这是个坑。

 

3、连接 elasticsearch

 

新建一个连接类 clienthelper.cs

 1 public class clienthelper
 2 {
 3     private static clienthelper clienthelper = null;
 4     // 默认索引
 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 }

 

新建映射类 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、增删查改操作

 

新建一个 api 控制器 escontroller.cs

 1 public class escontroller : apicontroller
 2 {
 3   // get: api/es/1
 4   // 按 id 查询单条记录
 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   // 批量导入数据库数据
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    // 按 id 更新单条数据
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     // 按 id 删除单条数据
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 }

 

另新建一个api 控制器 searchcontroller.cs 用来提供搜索服务

 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、试一试

(1) 导入数据到 elasticsearch

post http://localhost:8389/api/es

 

(2) 查询 id 为 1 的记录

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

 

(3) 更新 id 为 1 的记录

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

 

(4) 删除 id 为 1 的记录

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

 

(5) 查询名字中带有 中 的记录

get http://localhost:8389/api/search/中

 

一个简单的全文索引服务就完成了!

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网