nest客户端
一、客户端封装
using Elasticsearch.Net; using Nest; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace esdemo { public class ElasticsearchUtils { private static ElasticsearchUtils _elasticsearchUtils; /// <summary> /// Linq查询的官方Client /// </summary> private static IElasticClient _elasticLinqClient { get; set; } /// <summary> /// Js查询的官方Client /// </summary> private static IElasticLowLevelClient _elasticJsonClient { get; set; } private ElasticsearchUtils() { } private static readonly object eslock = new object(); public static ElasticsearchUtils GetInstance() { if (_elasticsearchUtils == null) { lock (eslock) { if (_elasticsearchUtils == null) { var urls = "http://192.168.0.150:9200"; var username = "elastic"; var passowrd = "123456"; var uris = urls.Split(",").ToList().ConvertAll(x => new Uri(x));//配置节点地址,以,分开 var connectionPool = new StaticConnectionPool(uris);//配置请求池 var settings = new ConnectionSettings(connectionPool) .BasicAuthentication(username, passowrd) .RequestTimeout(TimeSpan.FromSeconds(30));//请求配置参数 _elasticLinqClient = new ElasticClient(settings); _elasticJsonClient = new ElasticLowLevelClient(settings); _elasticsearchUtils = new ElasticsearchUtils(); } } } return _elasticsearchUtils; } public IElasticClient LinqClient() { return _elasticLinqClient; } public IElasticLowLevelClient JsonClient() { return _elasticJsonClient; } } }
二、CURD
DataService dataService = new DataService(); for (int i = 0; i < 10000; i++) { var logs = dataService.GetSource(i * 10, 10); var logsv2 = new List<request_audit_logs_v2>(); foreach (var item in logs) { //var aa = JsonConvert.DeserializeObject<dynamic>(item.request_content); //var aaa = aa.parm.Token; request_audit_logs_v2 log = new request_audit_logs_v2(); log.api = log.api; log.key = item.key; log.post_type = item.post_type; log.url = item.url; log.request_content = item.request_content; log.hander = item.hander; log.status = item.status; log.response_content = item.response_content; log.request_time = item.request_time.ToString("yyyy-MM-dd HH:mm:ss"); log.response_time = item.response_time.ToString("yyyy-MM-dd HH:mm:ss"); log.exception = item.exception; log.run_time = item.run_time; log.thread_num = item.thread_num; log.create_time = item.create_time.ToString("yyyy-MM-dd HH:mm:ss"); log.modify_time =item.modify_time.ToString("yyyy-MM-dd HH:mm:ss"); log.user_name = item.user_name; logsv2.Add(log); //var atters1 = ElasticsearchUtils.GetInstance().LinqClient().Bulk(p => p.Create<object>(c => c.Index("request_audit_logs").Document(log))); } var bulkAllObserver = new BulkAllObserver(); var aa= ElasticsearchUtils.GetInstance().LinqClient().BulkAll(logsv2, f => f.Index("request_audit_logs")); aa.Subscribe(bulkAllObserver); } Console.WriteLine("完成"); Console.ReadKey();
var aa = server.ElasticJsonClient.Update<StringResponse>("employees", "DOBxx3MBH3d_nFQ4zJ-J", PostData.Serializable(emp)); var updateResponse = server.ElasticLinqClient.Update<object>("DOBxx3MBH3d_nFQ4zJ-J", u => u .Index("employees") .Doc(new { name = "new_name" }) .DetectNoop(false) );
var searchResponse = client.Search<Account>(s => s .AllIndices() .Query(q => q .Bool(b => b .Must(m => m.Term(p => p.User, "kimchy")) .Filter(f => f.Term(p => p.Tags, "tech")) .MustNot(m => m .Range(r => r .Field(p => p.Age) .GreaterThanOrEquals(10) .LessThanOrEquals(20) ) ) .Should( sh => sh.Term(p => p.Tags, "wow"), sh => sh.Term(p => p.Tags, "elasticsearch") ) .MinimumShouldMatch(1) .Boost(1.0) ) ) );
public async Task<Tuple<long, List<notify_announce_range_description>>> GetListByPages(NotifyAnnounceRangeDescriptionGetListByPages data) { //var predicate = PredicateBuilder.New<notify_announce_range_description>(); //predicate = predicate.And(t => t.system_tag == data.system_tag); var conditions =new List<Func<QueryContainerDescriptor<notify_announce_range_description>, QueryContainer>>(); conditions.Add(f => f.Term(p => p.is_deleted, 1)); if (data.system_tag > 0) { conditions.Add(f=>f.Term(p => p.system_tag, data.system_tag)); } if (data.id != null) { conditions.Add(f => f.Term(p => p.id, data.id)); } var res = _elasticsearchUtils.LinqClient().Search<notify_announce_range_description>(a => { a.Index(typeof(notify_announce_range_description).Name); a.Query(q => { q.Bool(b => { b.Filter(conditions); return b; }); return q; }); a.PageBy(data); return a; }); return Tuple.Create(res.Total, res.Documents.ToList()); }
三、立即刷盘&&设置刷盘时间
public async Task BaseUpdate<T>(T data, string storage_indexs = null) where T : BaseEntity { _elasticsearchUtils.LinqClient().Bulk(b => { b.Update<T>(c => { c.Index(GetIndex<T>(storage_indexs)); c.Id(data.id); c.Doc(data); return c; }); b.Refresh(Refresh.True); return b; }); }
PUT student/_settings { "index" : { "refresh_interval" : "1m" } }
- ms: 毫秒
- s: 秒
- m: 分钟
学习永不止境,技术成就梦想。