用 【NEST】 在C#中操作ElasticSearch
2021-03-28 11:24
标签:host array bsp create cluster arc har nbsp pac 准备工作: VSCode开发环境,在终端控制台(Ctrl+~)输入命令 dotnet add package Nest 安装NEST包,安装好后打开项目的.csproj文件如下图。 Resume类的定义: 用 【NEST】 在C#中操作ElasticSearch 标签:host array bsp create cluster arc har nbsp pac 原文地址:https://www.cnblogs.com/gangle/p/9337472.html一、索引数据:
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("http://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
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 }
二、搜索数据
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("http://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
1 public class Extension
2 {
3 public static Dictionarystring, Expression
*****************************
*** Keep learning and growing. ***
*****************************
文章标题:用 【NEST】 在C#中操作ElasticSearch
文章链接:http://soscw.com/index.php/essay/69017.html