• Elasticsearch 入门(三)使用Elasticsearch.Net & NEST作为elasticsearch客户端


    Elasticsearch 入门(三)使用Elasticsearch.Net & NEST作为elasticsearch客户端

    GitHub仓库

    var node = new Uri("http://myserver:9200");
    var config = new ConnectionConfiguration(node);
    var client = new ElasticLowLevelClient(config);
    
    var myJson = @"{ ""hello"" : ""world"" }";
    client.Index<StringResponse>("myindex", "1", myJson);
    var myJson = new { hello = "world" };
    client.Index<BytesResponse>("myindex", "1", PostData.Serializable(myJson));
    

    Elasticsearch.Net 封装

    • 新建 ElasticSearchClient
        public class ElasticSearchClient
        {
            public ElasticLowLevelClient Client { get; }
    
            private readonly IConfiguration _configuration;
    
            public ElasticSearchClient(IConfiguration configuration)
            {
                _configuration = configuration;
                Client = InitClient();
            }
    
            #region Methods
            public async Task<string> Index(string index, string id, PostData body)
            {
                var response = await Client.IndexAsync<StringResponse>(index, id, body);
    
                ResponseValidate(response);
                return response.Body;
            }
    
            public async Task<List<string>> SearchWithHighLight(string index, string query)
            {
                var response = await Client.SearchAsync<StringResponse>(
                    index,
                    PostData.Serializable(new
                    {
                        from = 0,
                        size = 100,
                        query = new
                        {
                            match = new
                            {
                                content = query
                            }
                        },
                        highlight = new
                        {
                            pre_tags = new[] { "<tag1>", "<tag2>" },
                            post_tags = new[] { "/<tag1>", "/<tag2>" },
                            fields = new
                            {
                                content = new { }
                            }
                        }
                    }));
    
                ResponseValidate(response);
                var responseJson = (JObject)JsonConvert.DeserializeObject(response.Body);
    
                var hits = responseJson["hits"]["hits"] as JArray;
    
                var result = new List<string>();
    
                foreach (var hit in hits)
                {
                    var id = hit["_id"].ToObject<string>();
    
                    result.Add(id);
                }
    
                return result;
            }
    
            public async Task Delete(string index, string id)
            {
                var response = await Client.DeleteAsync<StringResponse>(index, id);
    
                ResponseValidate(response);
            }
            #endregion
    
            #region privates
            private ElasticLowLevelClient InitClient()
            {
                var node = new Uri(_configuration.GetConnectionString("ElasticSearch"));
                var settings = new ConnectionConfiguration(node);
                var client = new ElasticLowLevelClient(settings);
    
                return client;
            }
    
            private void ResponseValidate(StringResponse response)
            {
                if (response.Success == false)
                {
                    throw new ResultException(response.Body);
                }
            }
    
            #endregion
    
        }
    
    • 依赖注入 Startup.csservices.AddScoped<ElasticSearchClient>();
    • 使用
    private readonly ElasticSearchClient _elasticSearchClient;
    await _elasticSearchClient.Index(Article.EsIndex, article.ArticleUID,PostData.Serializable(article));
    
  • 相关阅读:
    07-JSP
    06-ServletContext和ServletConfig
    05-请求转发和重定向Login
    04-session学习
    03-cookie学习
    02-servlet生命周期和doget()_dopost()方法
    01-request和respone学习
    05-jQuery案例
    04-jQuery的事件机制和动画效果
    03-jQuery操作元素
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13024156.html
Copyright © 2020-2023  润新知