zoukankan      html  css  js  c++  java
  • 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));
    
  • 相关阅读:
    创新县(市、区)的主要条件
    C# imgage图片转base64字符/base64字符串转图片另存成
    base64编码的 文件 图片
    CSS
    Exception has been thrown by the target of an invocation 网站报错
    高效通用分页存储过程 多表查询
    JavaScript 知识记录
    JQuery 全选 取消
    ELK(elasticsearch+logstash+kibana)入门到熟练-从0开始搭建日志分析系统教程
    ELK- elasticsearch 讲解,安装,插件head,bigdesk ,kopf,cerebro(kopf升级版)安装
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13024156.html
Copyright © 2011-2022 走看看