.net core elk(三) .net core 下 elasticsearch 使用

更新整个文档 | Elasticsearch: 权威指南 | Elastic

 

首先 nuget 下载 NEST

学习地址

入门 |Elasticsearch .NET 客户端 [7.17] |弹性的

单机连接

连接编辑
连接到本地运行的 Elasticsearch 就像实例化客户端的新实例一样简单。http://localhost:9200
var client = new ElasticClient();
通常,您可能需要将其他配置选项传递给客户端,例如 Elasticsearch 的地址(如果它正在运行)。 远程计算机。这就是进来的地方;可以实例化实例以向客户端提供不同的 配置。ConnectionSettings
var settings = new ConnectionSettings(new Uri("http://example.com:9200"))
    .DefaultIndex("people");
var client = new ElasticClient(settings);

集群连接

ConnectionSettings不限于为 Elasticsearch 传递单个地址。有几种不同的 NEST 中可用的连接池类型,每种类型具有不同的特征,可用于 配置客户端。以下示例使用带有地址的嗅探连接池 集群中的三个 Elasticsearch 节点,客户端将使用这种类型的池来维护 可以循环方式向其发送请求的群集。
var uris = new[]
{
    new Uri("http://localhost:9200"),
    new Uri("http://localhost:9201"),
    new Uri("http://localhost:9202"),
};
var connectionPool = new SniffingConnectionPool(uris);
var settings = new ConnectionSettings(connectionPool)
    .DefaultIndex("people");
var client = new ElasticClient(settings);

添加索引 

可以同步或异步为单个文档编制索引, 分别使用 and 方法。这些方法是为单个文档编制索引的简单方法 不需要任何其他请求参数IndexDocumentIndexDocumentAsync
var person = new Person
{
    Id = 1,
    FirstName = "Martijn",
    LastName = "Laarman"
};
var indexResponse = client.IndexDocument(person); 
if (!indexResponse.IsValid)
{
    // If the request isn't valid, we can take action here
}
var indexResponseAsync = await client.IndexDocumentAsync(person); 

带参数索引

如果需要在编制索引时设置其他参数,可以将该方法与 fluent 或对象初始值设定项语法一起使用。 该方法公开了一种设置其他参数的方法,例如要在其中编制索引的索引的名称、要分配给 文档、路由参数等,允许对索引进行更多控制。IndexIndex
var person = new Person
{
    Id = 1,
    FirstName = "Martijn",
    LastName = "Laarman"
};
var indexResponse1 = client.Index(person, i => i.Index("people")); 
var indexResponse2 = client.Index(new IndexRequest<Person>(person, "people")); 

其中还有 多个 批量 新建索引 可以参考下面

为文档编制索引 |Elasticsearch .NET 客户端 [7.17] |弹性的

重新更新索引

有时需要将文档从一个索引重新索引到另一个索引。客户端提供了两种不同的方法 重新索引文档和 。ReindexOnServerReindex

重新索引编辑
Elasticsearch 的重新索引 API 作为方法(及其异步对应项)公开 客户端。简单用法是定义源索引和目标索引,然后等待操作完成ReindexOnServerReindexOnServerAsync

在开始重新索引过程之前,目标索引必须存在

var reindexResponse = client.ReindexOnServer(r => r
    .Source(s => s
        .Index("source_index")
    )
    .Destination(d => d
        .Index("destination_index")
    )
    .WaitForCompletion() 
);

查看以下文档

重新索引文档 |Elasticsearch .NET 客户端 [7.17] |弹性的

上 封装代码

  public class ElasticSearchOptions
    {
        /// <summary>
        /// 连接地址
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 索引名称
        /// </summary>
        public string IndexName { get; set; }
    }

 

 public interface IElasticSearchService
    {

        public ElasticClient GetElasticClient();
        public void Send<T>(List<T> model) where T : class;
        /// <summary>
        /// 新增或者更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        public void InsertOrUpdata<T>(T model) where T : class;
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete<T>(string id) where T : class;
        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public bool DropIndex(string indexName);
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="indexName"></param>
        public void CreateIndex(string indexName);
    }

 

  public class ElasticSearchService : IElasticSearchService
    {
        private readonly ElasticSearchOptions _elasticSearchOptions;
        public ElasticSearchService(ElasticSearchOptions elasticSearchOptions)
        {
            _elasticSearchOptions = elasticSearchOptions;
            var settings = new ConnectionSettings(new Uri(_elasticSearchOptions.Url)).DefaultIndex(this._elasticSearchOptions.IndexName);
            Client = new ElasticClient(settings);
        }
        private ElasticClient Client;
        public ElasticClient GetElasticClient()
        {
            return Client;
        }

        public void CreateIndex(string indexName)
        {
            var settings = new ConnectionSettings(new Uri(_elasticSearchOptions.Url))
.DefaultIndex(indexName);
            this.Client = new ElasticClient(settings);
        }

        public bool Delete<T>(string id) where T : class
        {
            var response = Client.Delete<T>(id);
            return response.IsValid;
        }

        public bool DropIndex(string indexName)
        {
            return Client.Indices.Delete(Indices.Parse(indexName)).IsValid;
        }


        public void InsertOrUpdata<T>(T model) where T : class
        {
            Client.IndexDocument(model);
        }

        public void Send<T>(List<T> model) where T : class
        {
            Client.IndexMany<T>(model);
        }
    }

 

posted on 2023-06-21 15:57  是水饺不是水饺  阅读(241)  评论(0)    收藏  举报

导航