C#编程之用 【NEST】 在C#中操作ElasticSearch
小标 2018-11-27 来源 : 阅读 4369 评论 0

摘要:本文主要向大家介绍了C#编程之用 【NEST】 在C#中操作ElasticSearch,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之用 【NEST】 在C#中操作ElasticSearch,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

准备工作: VSCode开发环境,在终端控制台(Ctrl+~)输入命令 dotnet add package Nest 安装NEST包,安装好后打开项目的.csproj文件如下图。

一、索引数据:

 1 using Nest;
 2 using System;
 3 
 4 namespace NetCoreFirst
 5 {
 6     public class ImportES
 7     {
 8         public static string ElasticsearchMethod()
 9         {
10             //1.通过es服务器 localhost:9200 来定义es client
11             var node = new Uri("//localhost:9200");
12             var indexName = "esbot";
13             var settings = new ConnectionSettings(node).DefaultIndex(indexName);
14             var elastic = new ElasticClient(settings);
15             
16             //es服务器健康检查
17             var res = elastic.ClusterHealth();
18             Console.WriteLine(res.Status);
19 
20             //2.创建索引esbot
21             if (!elastic.IndexExists(indexName).Exists)
22             {
23                 var createIndexResponse = elastic.CreateIndex(indexName);
24                 var mappingBlogPost = elastic.Map(s => s.AutoMap());
25             }
26 
27             //3.构造数据
28             string[] nameArray = {"Cody", "Blake", "Dennis", "Evan ", "Harris", "Jason ", "Lambert ", "Louis ", "Milton ", "Cody" };
29             string[] skillArray = {"c#", "c++", "java", "python", "php", "Linux", "ruby", "matlab", "perl", "powershell" };
30             long[] ageRange = { 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 };
31             for(int i=0; i< 10;i++)
32             {
33                 var resume = new Resume
34                 {
35                     Id= Guid.NewGuid(),
36                     Name=nameArray[i],
37                     Age=ageRange[i],
38                     Skills="My skill is Azure and " + skillArray[i]
39                 };
40                 //4.导入数据到索引
41                 IIndexResponse bulkIndexResponse = elastic.Index(resume, p => p.Type(typeof(Resume)).Id(i).Refresh(null));
42             }
43 
44             //5. 简单搜索
45             var searchResult = elastic.Search(sr => sr.Query(q => q.MatchAll()));
46             // System.Console.WriteLine(searchResult.Hits.Count);
47             // System.Console.ReadLine();
48             var resumesCount = searchResult.Hits.Count.ToString();
49             return resumesCount;
50         }
51     }
52 }

Resume类的定义:


 1 using Nest;
 2 using System;
 3 
 4 namespace NetCoreFirst
 5 {
 6     [ElasticsearchType(Name="candidate", IdProperty="Id")]
 7     public class Resume
 8     {
 9         [Text(Name="id", Index=false)]
10         public Guid? Id { get; set; }
11 
12         [Text(Name="name", Index=true)]
13         public string Name { get; set; }
14 
15         [Text(Name="age", Index=false)]
16         public long Age { get; set; }
17 
18         [Text(Name="skills", Index=true)]
19         public string Skills { get; set; }
20     }
21 }

View Code
 
二、搜索数据

 1 using Nest;
 2 using System;
 3 
 4 namespace NetCoreFirst
 5 {
 6     public class SearchES
 7     {
 8         public static string indexName="bank";
 9         public static Uri node = new Uri("//localhost:9200");
10         public static ConnectionSettings settings = new ConnectionSettings(node).DefaultIndex(indexName);
11         public static ElasticClient elastic = new ElasticClient(settings);
12 
13         public static ISearchResponse SearchNumber()
14         {
15             string dictionaryKey = "balance";
16             var dictionary = Extension.BankDictionary();
17             var rangeField = dictionary[dictionaryKey];
18             var gt = 40000;
19             var lt = 40100;
20             var searchResponse = elastic.Search(es => es.Query(q => 
21                     q.Range(r => r.Name("").Field(rangeField).GreaterThan(gt).LessThan(lt).Boost(2.0))));
22             return searchResponse;
23         }
24 
25         public static ISearchResponse SearchString()
26         {
27             string queryStr = "";
28             var searchResponse = elastic.Search(es => es.Query(q => 
29                     q.QueryString(qs => qs.Query(queryStr))));
30             return searchResponse;
31         }
32 
33         public static ISearchResponse SearchField()
34         {
35             string queryStr = "35";
36             string dictionaryKey = "age";
37             var dictionary = Extension.BankDictionary();
38             var rangeField = dictionary[dictionaryKey];
39             var searchResponse = elastic.Search(es => es.Query(q => 
40                     q.Match(m => m.Field(rangeField).Query(queryStr))));
41             return searchResponse;
42         }
43     }
44 }

Extension类和Bank类的定义


 1     public class Extension
 2     {
 3         public static Dictionary<string, Expression<Func>> ResumeDictionary()
 4         {
 5             return new Dictionary<string, Expression<Func>>
 6             {
 7                 {"age", p=>p.Age},
 8                 {"name", p=>p.Name}
 9             };
10         }
11 
12         public static Dictionary<string, Expression<Func>> BankDictionary()
13         {
14             return new Dictionary<string, Expression<Func>>
15             {
16                 {"account_number", p => p.account_number},
17                 {"age", p => p.age},
18                 {"balance", p => p.balance}
19             };
20         }
21     }
22 
23     [ElasticsearchType(Name="account", IdProperty="Id")]
24     public class Bank
25     {
26         public long account_number {get;set;}
27         public string address { get; set; }
28         public long age { get; set; }
29         public long balance { get; set; }
30         public string city { get; set; }
31         public string email { get; set; }
32         public string employer { get; set; }
33         public string firstname { get; set; }
34         public string gender { get; set; }
35         public string lastname { get; set; }
36         public string state { get; set; }
37     }

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 3 不喜欢 | 2
看完这篇文章有何感觉?已经有5人表态,60%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程