ElasticSearch操作实例大全---文档结构操作(1)

前提条件--开发环境已安装 (自行百度)

客户端用的是nest

学习elasticSearch主要是要掌握像sqlserver要会操作数据结构的增删改和数据的增删改查,这里主要写elasticSearch的文档结构操作和文档数据操作

一、新建一个索引

 /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="alias">索引别名</param>
        /// <param name="shards">索引分片数</param>
        /// <param name="replicas">索引备份数</param>
        /// <returns>返回是否创建成功</returns>
        public bool Create(string indexName, string alias, int shards = 5, int replicas = 0)
        {
            if (string.IsNullOrWhiteSpace(indexName))
            {
                return false;
            }
            indexName = indexName.ToLower();
            //如果索引名称已经存在,则不插入
            if (IsExists(indexName))
            {
                OriginalException = new Exception("索引已存在");
                return false;
            }
            CreateIndexDescriptor createIndex = new CreateIndexDescriptor(ConnectionSettings);
            createIndex.Index(indexName);
            if (!string.IsNullOrWhiteSpace(alias))
            {
                createIndex.AddAlias(alias);
            }
            createIndex.NumberOfReplicas(replicas).NumberOfShards(shards);
            var response = Client.CreateIndex(createIndex);
            if (response.ConnectionStatus.Success)
            {
                return true;
            }
            else
            {
                OriginalException = response.ConnectionStatus.OriginalException;
                return false;
            }
        }

当然先要new一个客户端实例client ,不然没法操作

  private ElasticClient _client = null;

        /// <summary>
        /// es客户端实例
        /// </summary>
        public ElasticClient Client
        {
            get
            {
                if (_client == null)
                {
                     List<Uri> uris = new List<Uri>();
                    foreach (var item in Nodes)
                    {
                        uris.Add(new Uri(item));
                    }
                    var connectionPool = new SniffingConnectionPool(uris);
                  ConnectionSettings  _connectionSettings = new ConnectionSettings(connectionPool, "");
                    if (!string.IsNullOrWhiteSpace(UserName))
                    {
                        _connectionSettings = _connectionSettings.SetBasicAuthentication(UserName, Password);
                    }
                    _client = new ElasticClient(_connectionSettings );
                }
                return _client;
            }
        }

可能也需要检查一下这个索引是不是已经存在

 /// <summary>
        /// 判断索引是否存在
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns></returns>
        public bool IsExists(string indexName)
        {
            if (string.IsNullOrWhiteSpace(indexName))
            {
                OriginalException = new ArgumentNullException("indexName", "indexName不允许为空");
                return false;
            }
            return Client.IndexExists(i => i
                 .Index(indexName)).Exists;
        }

 如果不需要了是不是要删除这个索引呢

  /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="indexName">索引名或别名</param>
        /// <returns>执行结果,正确返回true,否则返回false</returns>
        public bool Delete(string indexName)
        {
            if (!IsExists(indexName))
            {
                OriginalException = new Exception("索引不存在!");
                return false;
            }
            var response = Client.DeleteIndex(m => m.Index(indexName));
            return Helper.ConvertIResponse(response);
        }

 

 如果基础不够的话可以看看这个http://es.xiaoleilu.com/

今天就写到这个,文笔不是太好,就按那个思路写了一些,方便初学者

觉得好的话,鼓励一下,点击一下推荐哦

 

posted @ 2016-08-19 16:03  nik2011  阅读(1860)  评论(0编辑  收藏  举报