程序员

NET SDK连接阿里云ElasticSearch示例

作者:admin 2021-04-12 我要评论

Step By Step 1、ES开通公网地址(本身连接需要使用公网地址) 2、配置IP白名单 3、开启允许自动创建索引 4、通过Nuget安装: NEST 5、Test 5.1 Code Sample using ...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
Step By Step

1、ES开通公网地址(本身连接需要使用公网地址)
图片.png

2、配置IP白名单
图片.png

3、开启允许自动创建索引
图片.png

图片.png

4、通过Nuget安装: NEST
图片.png

5、Test

5.1 Code Sample
using Nest;
using System;
namespace ElasticSearchDemo
 class Program
 static void Main(string[] args)
 // 1、初始化连接认证信息
 var settings = new ConnectionSettings(new Uri("http://es-cn-******.public.elasticsearch.aliyuncs.com:9200"));
 settings.BasicAuthentication("elastic", "******");
 var client = new ElasticClient(settings);
 // 2、定义index
 var tweet = new Tweet
 Id = 1,
 User = "kimchy",
 PostDate = new DateTime(2009, 11, 15),
 Message = "Trying out NEST, so far so good?"
 // 3、创建索引
 var response = client.Index(tweet, idx = idx.Index("new_create_index")); //or specify index via settings.DefaultIndex("mytweetindex");
 // 4、打印输出结果
 Console.WriteLine(response);
 Console.ReadKey();
}
5.2 Tweet.cs
using System;
namespace ElasticSearchDemo
 internal class Tweet
 public int Id { get; set; }
 public string User { get; set; }
 public DateTime PostDate { get; set; }
 public string Message { get; set; }
}
5.3 Result
Valid NEST response built from a successful (201) low level call on PUT: /new_create_index/_doc/1

6、Kibina索引查询
图片.png

更多参考

elastic/elasticsearch-net
PostMan 工具连接阿里云 ElasticSearch 示例


本文转自网络,原文链接:https://developer.aliyun.com/article/783450

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 四两拨千斤——你不知道的VScode编码Ty

    四两拨千斤——你不知道的VScode编码Ty

  • 我是如何在 Vue 项目中做代码分割的

    我是如何在 Vue 项目中做代码分割的

  • position:sticky 粘性定位的几种巧妙应

    position:sticky 粘性定位的几种巧妙应

  • 从零到一搭建React组件库

    从零到一搭建React组件库

腾讯云代理商
海外云服务器