NET SDK连接阿里云ElasticSearch示例

Step By Step

1、ES开通公网地址(本身连接需要使用公网地址)
NET SDK连接阿里云ElasticSearch示例

2、配置IP白名单
NET SDK连接阿里云ElasticSearch示例

3、开启允许自动创建索引
NET SDK连接阿里云ElasticSearch示例

NET SDK连接阿里云ElasticSearch示例

4、通过Nuget安装: NEST
NET SDK连接阿里云ElasticSearch示例

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索引查询
NET SDK连接阿里云ElasticSearch示例

更多参考

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

上一篇:SpringBoot集成阿里云Kafka 示例


下一篇:阿里云消息队列Kafka Ram账户授权问题