From 5c106a8ab407fd2d5788b2ec7fc7991f304f93ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Tue, 9 Nov 2021 16:59:50 +0800 Subject: [PATCH] esinvoker --- .../Yi.Framework.Core/ElasticSearchInvoker.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Yi.Framework/Yi.Framework.Core/ElasticSearchInvoker.cs diff --git a/Yi.Framework/Yi.Framework.Core/ElasticSearchInvoker.cs b/Yi.Framework/Yi.Framework.Core/ElasticSearchInvoker.cs new file mode 100644 index 00000000..4b4556cc --- /dev/null +++ b/Yi.Framework/Yi.Framework.Core/ElasticSearchInvoker.cs @@ -0,0 +1,53 @@ +using Microsoft.Extensions.Options; +using Nest; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Common.IOCOptions; + +namespace Yi.Framework.Core +{ + public class ElasticSearchInvoker + { + private readonly ElasticSearchOptions _elasticSearchOptions; + + public ElasticSearchInvoker(IOptionsMonitor optionsMonitor) + { + _elasticSearchOptions = optionsMonitor.CurrentValue; + 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 Send(List model) where T : class + { + Client.IndexMany(model); + } + + public void InsertOrUpdata(T model) where T : class + { + Client.IndexDocument(model); + } + + public bool Delete(string id) where T : class + { + + var response = Client.Delete(id); + return response.IsValid; + } + public bool DropIndex(string indexName) + { + return Client.Indices.Delete(Indices.Parse(indexName)).IsValid; + } + public void CreateIndex(string indexName) + { + var settings = new ConnectionSettings(new Uri(_elasticSearchOptions.Url)).DefaultIndex(indexName); + this.Client = new ElasticClient(settings); + } + } +}