zoukankan      html  css  js  c++  java
  • ASP.NET Web API + Elasticsearch 6.x 快速做个全文搜索

    最近想做个全文搜索,设想用 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%ElasticElasticsearchin

    回车,输入 

    .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/中

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

  • 相关阅读:
    POJ 2251 Dungeon Master
    HDU 3085 Nightmare Ⅱ
    CodeForces 1060 B Maximum Sum of Digits
    HDU 1166 敌兵布阵(树状数组)
    HDOJ 2050 折线分割平面
    HDU 5879 Cure
    HDU 1878 欧拉回路
    HDU 6225 Little Boxes
    ZOJ 2971 Give Me the Number
    HDU 2680 Choose the best route
  • 原文地址:https://www.cnblogs.com/lifefriend/p/10159371.html
Copyright © 2011-2022 走看看