zoukankan      html  css  js  c++  java
  • 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;
            }
    
    
        }
    }
    ElasticsearchUtils

    二、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();
    Bulk与BulkAll
     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)
                                                                            );
    Update
  • 相关阅读:
    口语详解|为什么“how to say”是错的?
    9 tips to improve spoken english
    splash 安装
    ubuntu 安装NVIDIA驱动过程
    【Python数据分析】时间模块datetime
    【Python数据分析】Pandas模块下的Series与DataFrame
    【Python】文件
    博客园Markdown样式美化
    【Python】异常处理
    【Python】eval函数
  • 原文地址:https://www.cnblogs.com/zd1994/p/13490271.html
Copyright © 2011-2022 走看看