排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
.NET Core
Elasticsearch使用版本7.12.0
安装Elasticsearch与kibana可以参考:https://www.tnblog.net/aojiancc2/article/details/5875
使用kibana来进行原生crud可以参考:https://www.tnblog.net/hb/article/details/4918
一:安装.NET CORE操作Elasticsearch的库NEST
<ItemGroup> <PackageReference Include="NEST" Version="7.12.0" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> </ItemGroup>
方法2:
Install-Package NEST -version 7.12.0 Install-Package Newtonsoft.Json -version 13.0.1
NEST库要注意版本和你的Elasticsearch版本要匹配,json库可以选装
二:创建Elasticsearch访问类
1:创建IElasticSearchServer接口,创建两个Client,一个用于linq查询,一个用于json查询
public interface IElasticSearchServer { /// <summary> /// Linq查询的官方Client /// </summary> IElasticClient ElasticLinqClient { get; set; } /// <summary> /// Js查询的官方Client /// </summary> IElasticLowLevelClient ElasticJsonClient { get; set; } }
2:创建IElasticSearchServer的实现类
public class ElasticSearchServer : IElasticSearchServer { /// <summary> /// Linq查询的官方Client /// </summary> public IElasticClient ElasticLinqClient { get; set; } /// <summary> /// Json查询的官方Client /// </summary> public IElasticLowLevelClient ElasticJsonClient { get; set; } public IMemoryCache memoryCache { get; set; } public ElasticSearchServer(IConfiguration configuration, IMemoryCache memoryCache_arg) { memoryCache = memoryCache_arg; //写死ElasticSearch地址 List<Uri> uris = new List<Uri>(); uris.Add(new Uri("http://localhost:9200/")); //配置Es请求连接池 var connectionPool = new StaticConnectionPool(uris); //json查询的初始化 this.ElasticJsonClient = new ElasticLowLevelClient(); //linq查询的初始化 this.ElasticLinqClient = new ElasticClient(); } }
如果要根据配置文件读取es地址,以及配置密码等可以这样写:
/// <summary> /// 根据配置文件读取es地址,以及配置密码 /// </summary> /// <param name="configuration"></param> /// <param name="memoryCache_arg"></param> public ElasticSearchServer(IConfiguration configuration, IMemoryCache memoryCache_arg) { memoryCache = memoryCache_arg; //es的地址,支持连接池设置。(建议配置在appsettings.json中进行读取) var uris = configuration["ElasticSearchContext:Url"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList().ConvertAll(x => new Uri(x)); //配置Es请求连接池 var connectionPool = new StaticConnectionPool(uris); var settings = new ConnectionSettings(connectionPool) .BasicAuthentication("aj", "123456") //验证账号密码登录 .RequestTimeout(TimeSpan.FromSeconds(30)); //延迟 30s //json查询的初始化 this.ElasticJsonClient = new ElasticLowLevelClient(settings); //linq查询的初始化 this.ElasticLinqClient = new ElasticClient(settings); }
三:使用创建的类访问Elasticsearch
我们先要在ElasticSearch里边添加一条数据,可以被查询到,例如这样:
把实体类创建一下:
public class Persons { public string user { get; set; } public string message { get; set; } public DateTime post_date { get; set; } public List<string> albums { get; set; } }
然后使用core代码来查询
1:基于Linq的查询
/// <summary> /// 基于Linq的查询 /// </summary> /// <param name="elasticSearchServer"></param> /// <returns></returns> public async Task<List<Persons>> ElasticByLinq([FromServices] IElasticSearchServer elasticSearchServer) { var list = await elasticSearchServer.ElasticLinqClient.SearchAsync<Persons>( p => p.Index("users") .Query(op => op.Match( ss => ss.Field( qq => qq.user == "Jack")))); return list.Documents.ToList(); }
2:基于Json的查询
/// <summary> /// 基于Json的查询 /// </summary> /// <param name="elasticSearchServer"></param> /// <returns></returns> public async Task<string> ElasticByJson([FromServices] IElasticSearchServer elasticSearchServer) { var jsonobject = new { query = new { match = new { user = "Jack" } } }; string json = JsonConvert.SerializeObject(jsonobject); var stringResponse = await elasticSearchServer.ElasticJsonClient.SearchAsync<StringResponse>("users", json); return stringResponse.Body; }
注意ElasticSearch在7.x版本删除了Type类型,所以你的操作给不了Type了
项目下载:https://download.tnblog.net/resource/index/2e86676437094675a3dc743049fe2c74
欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739
评价