排名
1
文章
860
粉丝
112
评论
163
.net core自定义项目模板,创建自己的模板项目,使用命令行创建模板项目
尘叶心繁 : 可以可以讲真的我都想弄个模板
net core webapi post传递参数
庸人 :
确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 : 已精
.net webapi 返回需要的字段,忽略某些字段,修改字段名等
雨雨雨雨雨辰 :
疯狂反射
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

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
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价