ElasticSearch系列学习

ElasticSearch第一步-环境配置

 

ElasticSearch第三步-中文分词

ElasticSearch第四步-查询详解

ElasticSearch第五步-.net平台下c#操作ElasticSearch详解

  

前面我们讲解了关于ElasticSearch的安装配置,以及CRUD

本章我将讲解怎么使用c#操作ElasticSearch。

首先你需要一定的技术储备,比如:asp.net webapi,mvc,jsonp,knockout。这些知识在这里不再讲解,请自行Google。

项目DEMO介绍

搜索和索引功能我是以服务(webapi项目)方式提供的,在客户端(mvc项目)中的view视图中,直接使用ajax(jsonp格式)方式调用webapi,然后使用knockout绑定到table上的。

项目结构如图:

ElasticSearch-.net平台下c#操作ElasticSearch详解

 

引入驱动

工欲善其事必先利其器,首先我们需要在Supernova.Webapi层中引入操作ElasticSearch的驱动dll PlainElastic.Net。

如图:

ElasticSearch-.net平台下c#操作ElasticSearch详解

 

封装操作ElasticSearch的ElasticSearchHelper

View Code

demo中涉及的实体对象模型

ElasticSearch-.net平台下c#操作ElasticSearch详解
/// <summary>
    /// ik分词结果对象
    /// </summary>
    public class ik
    {
        public List<tokens> tokens { get; set; }
    }
    public class tokens
    {
        public string token { get; set; }
        public int start_offset { get; set; }
        public int end_offset { get; set; }
        public string type { get; set; }
        public int position { get; set; }
    }

    /// <summary>
    /// 测试数据对象
    /// </summary>
    public class personList
    {
        public personList()
        {
            this.list = new List<person>();
        }
        public int hits { get; set; }
        public int took { get; set; }
        public List<person> list { get; set; }
    }
    public class person
    {
        public string id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public bool sex { get; set; }
        public DateTime birthday { get; set; }
        public string intro { get; set; }
    }
ElasticSearch-.net平台下c#操作ElasticSearch详解

相关文章: