Example #1
0
        public bool StopIndex(string indexNumber)
        {
            var entity = this.IndexManagerRepository.Queryable()
                         .Where(x => x.IsDeleted == false && x.IndexNumber == indexNumber.Trim())
                         .FirstOrDefault();

            if (entity == null)
            {
                return(false);
            }

            entity.Status       = IndexStatus.Stoped;
            entity.ModifiedBy   = "admin";
            entity.ModifiedTime = DateTime.Now;

            return(this.IndexManagerRepository.ModifyWithTransection(entity, () =>
            {
                var key = IndexNameHelper.GetIndexMappingName(entity.ServiceSign, entity.IndexName);
                var isSuccess = this.CacheClient.SetCache(key, entity.Status, 173000);
                if (!isSuccess)
                {
                    throw new Exception("缓存更新失败");
                }
            }));
        }
Example #2
0
        /// <summary>
        /// 批量添加数据到索引
        /// </summary>
        /// <typeparam name="T">实体模型类型</typeparam>
        /// <param name="index">索引名称</param>
        /// <param name="entites">添加的实体模型</param>
        public bool BulkIndex <T>(string indexName, params T[] entites) where T : SearchEntityBase
        {
            var indexMappingName = IndexNameHelper.GetIndexMappingName(SetupConfig.SetupConfig.ServiceSign, indexName);
            var status           = this.CacheClient.GetCache <IndexOperateStatus>(indexMappingName);

            this.IndexOperate.CkeckIndexStatus <T>(status, indexMappingName);

            var sizeKey   = IndexNameHelper.GetIndexSizeName(SetupConfig.SetupConfig.ServiceSign, indexName);
            var indexSize = this.CacheClient.GetCache <int>(sizeKey);

            if (indexSize == 0)
            {
                return(false);
            }

            var catResponse = this.Client.CatIndices(index => index.Index(indexMappingName).Bytes(Bytes.Mb));
            var record      = catResponse.Records.FirstOrDefault();

            if (record != null)
            {
                long storeSize;
                if (long.TryParse(record.StoreSize, out storeSize))
                {
                    if (storeSize >= indexSize)
                    {
                        throw new ElasticsearchException("该索引存储空间已满");
                    }

                    var response = this.Client.IndexMany(entites, indexMappingName);
                    return(response.IsValid);
                }
            }
            return(false);
        }
Example #3
0
        public bool Expension(string indexNumber, ref string errorMsg)
        {
            var entity = this.IndexManagerRepository.Queryable()
                         .Where(x => x.IsDeleted == false && x.IndexNumber == indexNumber.Trim())
                         .FirstOrDefault();

            if (entity == null)
            {
                return(false);
            }

            if (entity.MaxExpensionNum == entity.ExpensionNum)
            {
                errorMsg = $"{entity.IndexName}已进行{entity.ExpensionNum}次扩容,已达到该索引的最大扩容次数";
                return(false);
            }

            entity.ExpensionNum += 1;
            entity.ModifiedBy    = "admin";
            entity.ModifiedTime  = DateTime.Now;

            return(this.IndexManagerRepository.ModifyWithTransection(entity, () =>
            {
                var key = IndexNameHelper.GetIndexSizeName(entity.ServiceSign, entity.IndexName);
                if (!this.CacheClient.SetCache <int>(key, entity.Size * (entity.ExpensionNum + 1), 173000))
                {
                    throw new Exception("缓存设置失败");
                }
            }));
        }
Example #4
0
        /// <summary>
        /// 结构化查询
        /// </summary>
        /// <typeparam name="T">查询模型类型</typeparam>
        /// <typeparam name="TR">查询结果模型类型</typeparam>
        /// <param name="searchCondition">查询条件</param>
        /// <returns></returns>
        public IEnumerable <TR> ExactValueSearch <T, TR>(IExactValueSearchCondition <T, TR> searchCondition)
            where T : SearchEntityBase
            where TR : class
        {
            var indexMappingName = IndexNameHelper.GetIndexMappingName(SetupConfig.SetupConfig.ServiceSign, searchCondition.Index);
            var status           = this.CacheClient.GetCache <IndexOperateStatus>(indexMappingName);

            this.IndexOperate.CkeckIndexStatus <T>(status, indexMappingName);

            if (searchCondition.Selector == null)
            {
                throw new ElasticsearchException("index查询异常:未设置查询结果类型转换表达式");
            }

            var queryContainer = searchCondition.QueryPredicate == null ? new MatchAllQuery()
                 : searchCondition.QueryPredicate.GetQuery();

            var properties  = LoadFieldHelper.GetProperty(searchCondition.Selector);
            var sourefilter = new SourceFilter()
            {
                Includes = properties
            };

            var searchRequest = new SearchRequest <T>(indexMappingName);

            searchRequest.Query = new ConstantScoreQuery()
            {
                Filter = queryContainer
            };
            searchRequest.Source = new Union <bool, ISourceFilter>(sourefilter);

            if (searchCondition.Sort != null && searchCondition.Sort.Count() > 0)
            {
                searchRequest.Sort = searchCondition.Sort.Select(x => (ISort) new SortField
                {
                    Field = x.Field,
                    Order = x.isAsc ? SortOrder.Ascending : SortOrder.Descending
                }).ToList();
            }

            searchRequest.Size = searchCondition.SizeNumber;
            searchRequest.From = searchCondition.SkipNumber;

            var response = this.Client.Search <T>(searchRequest);

            if (!response.IsValid)
            {
                throw new ElasticsearchException("index查询异常:" + response.OriginalException.Message);
            }

            return(response.Hits.Select(x => x.Source).Select(searchCondition.Selector.Compile()));
        }
        public void List_should_return_expected_result(
            [Values("{ singleIndex : 1 }", "{ compoundIndex1 : 1, compoundIndex2 : 1 }")] string key,
            [Values(false, true)] bool unique,
            [Values(false, true)] bool async)
        {
            var indexKeyDocument = BsonDocument.Parse(key);
            var collection       = GetEmptyCollection();
            var subject          = collection.Indexes;

            subject.CreateOne(new CreateIndexModel <BsonDocument>(indexKeyDocument, new CreateIndexOptions()
            {
                Unique = unique
            }));

            var indexesCursor =
                async
                    ? subject.ListAsync().GetAwaiter().GetResult()
                    : subject.List();
            var indexes = indexesCursor.ToList();

            indexes.Count.Should().Be(2);
            AssertIndex(collection.CollectionNamespace, indexes[0], "_id_");
            var indexName = IndexNameHelper.GetIndexName(indexKeyDocument);

            AssertIndex(collection.CollectionNamespace, indexes[1], indexName, expectedUnique: unique);

            void AssertIndex(CollectionNamespace collectionNamespace, BsonDocument index, string expectedName, bool expectedUnique = false)
            {
                index["name"].AsString.Should().Be(expectedName);

                if (expectedUnique)
                {
                    index["unique"].AsBoolean.Should().BeTrue();
                }
                else
                {
                    index.Contains("unique").Should().BeFalse();
                }

                if (CoreTestConfiguration.ServerVersion < new SemanticVersion(4, 3, 0))
                {
                    index["ns"].AsString.Should().Be(collectionNamespace.ToString());
                }
                else
                {
                    // the server doesn't return ns anymore
                    index.Contains("ns").Should().BeFalse();
                }
            }
        }
Example #6
0
        private void TrySyncData()
        {
            var query = this.IndexManagerRepository.Queryable().Where(x => x.IsDeleted == false).Select(x => new
            {
                ServiceSign  = x.ServiceSign,
                IndexName    = x.IndexName,
                Status       = x.Status,
                Size         = x.Size,
                ExpensionNum = x.ExpensionNum
            }).ToList();

            foreach (var indexData in query)
            {
                var indexMappingName = IndexNameHelper.GetIndexMappingName(indexData.ServiceSign, indexData.IndexName);
                this.CacheClient.SetCache(indexMappingName, indexData.Status, 173000);
                var indexMappingNameForSize = IndexNameHelper.GetIndexSizeName(indexData.ServiceSign, indexData.IndexName);
                this.CacheClient.SetCache(indexMappingNameForSize, indexData.Size * (indexData.ExpensionNum + 1), 173000);
            }
        }
Example #7
0
        public bool Delete(string indexNumber)
        {
            var entity = this.IndexManagerRepository.Queryable()
                         .Where(x => x.IsDeleted == false && x.IndexNumber == indexNumber.Trim())
                         .FirstOrDefault();

            if (entity == null)
            {
                return(false);
            }

            entity.Status       = IndexStatus.Deleted;
            entity.IsDeleted    = true;
            entity.ModifiedBy   = "admin";
            entity.ModifiedTime = DateTime.Now;

            var indexMappingName = IndexNameHelper.GetIndexMappingName(entity.ServiceSign, entity.IndexName);
            var isSuccess        = this.IndexManagerRepository.ModifyWithTransection(entity, () =>
            {
                if (!this.CacheClient.SetCache(indexMappingName, entity.Status, 173000))
                {
                    throw new Exception("缓存更新失败");
                }
            });

            if (isSuccess)
            {
                new Task(() =>
                {
                    try
                    {
                        IndexOperateHelper.RemoveIndex(indexMappingName);
                    }
                    catch (Exception)
                    {
                        //TODO:
                    }
                }).Start();
            }

            return(isSuccess);
        }
Example #8
0
        public bool AddIndex(IndexAddParam param, ref string errorMsg)
        {
            if (this.IsIndexExist(param.ServiceNumber, param.IndexName))
            {
                errorMsg = $"该服务已存在名称为{param.IndexName}的索引";
                return(false);
            }

            var entity = new IndexManager
            {
                ServiceNumber   = param.ServiceNumber,
                ServiceSign     = param.ServiceSign,
                ShardNumber     = param.ShardNum,
                Size            = param.Size,
                Status          = IndexStatus.NotStarted,
                ExpensionNum    = 0,
                MaxExpensionNum = param.MaxExpensionNum,
                ReplicasNumber  = param.ReplicasNum,
                IndexName       = param.IndexName,
                IndexNumber     = Guid.NewGuid().ToString("N"),
                IsDeleted       = false,
                CreatedTime     = DateTime.Now,
                CreatedBy       = "admin",
                ModifiedTime    = DateTime.Now,
                ModifiedBy      = "admin"
            };

            return(this.IndexManagerRepository.AddWithTransection(entity, () =>
            {
                var key = IndexNameHelper.GetIndexMappingName(entity.ServiceSign, entity.IndexName);
                var isSucess = this.CacheClient.SetCache(key, entity.Status, 173000);
                if (!isSucess)
                {
                    throw new Exception("缓存更新失败");
                }
            }));
        }
Example #9
0
        public void List_should_return_expected_result(
            [Values("{ singleIndex : 1 }", "{ compoundIndex1 : 1, compoundIndex2 : 1 }")] string key,
            [Values(false, true)] bool unique,
            [Values(null, false, true)] bool?hidden,
            [Values(false, true)] bool async)
        {
            var indexKeyDocument = BsonDocument.Parse(key);
            var collectionName   = DriverTestConfiguration.CollectionNamespace.CollectionName;
            var client           = DriverTestConfiguration.Client;
            var database         = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);

            database.DropCollection(collectionName);
            var collection = database.GetCollection <BsonDocument>(collectionName);

            var subject = collection.Indexes;
            var isHiddenIndexSupported = Feature.HiddenIndex.IsSupported(CoreTestConfiguration.ServerVersion);

            try
            {
                var indexOptions = new CreateIndexOptions()
                {
                    Unique = unique
                };
                if (isHiddenIndexSupported)
                {
                    indexOptions.Hidden = hidden;
                }

                subject.CreateOne(
                    new CreateIndexModel <BsonDocument>(
                        indexKeyDocument,
                        indexOptions));

                var indexesCursor =
                    async
                        ? subject.ListAsync().GetAwaiter().GetResult()
                        : subject.List();
                var indexes = indexesCursor.ToList();

                indexes.Count.Should().Be(2);
                AssertIndex(collection.CollectionNamespace, indexes[0], "_id_");
                var indexName = IndexNameHelper.GetIndexName(indexKeyDocument);
                AssertIndex(collection.CollectionNamespace, indexes[1], indexName, expectedUnique: unique, expectedHidden: hidden);
            }
            finally
            {
                // make sure that index has been removed
                database.DropCollection(collectionName);
            }

            void AssertIndex(CollectionNamespace collectionNamespace, BsonDocument index, string expectedName, bool expectedUnique = false, bool?expectedHidden = false)
            {
                index["name"].AsString.Should().Be(expectedName);

                if (expectedUnique)
                {
                    index["unique"].AsBoolean.Should().BeTrue();
                }
                else
                {
                    index.Contains("unique").Should().BeFalse();
                }

                if (expectedHidden.GetValueOrDefault() && isHiddenIndexSupported)
                {
                    index["hidden"].AsBoolean.Should().BeTrue();
                }
                else
                {
                    index.Contains("hidden").Should().BeFalse();
                }

                if (CoreTestConfiguration.ServerVersion < new SemanticVersion(4, 3, 0))
                {
                    index["ns"].AsString.Should().Be(collectionNamespace.ToString());
                }
                else
                {
                    // the server doesn't return ns anymore
                    index.Contains("ns").Should().BeFalse();
                }
            }
        }
        /// <summary>
        /// 全文检索
        /// </summary>
        /// <typeparam name="T">查询模型类型</typeparam>
        /// <param name="condition">查询条件</param>
        /// <returns></returns>
        public IEnumerable <T> FullTextSearch <T>(IFullTextSearchCondition <T> condition) where T : SearchEntityBase
        {
            var indexMappingName = IndexNameHelper.GetIndexMappingName(SetupConfig.SetupConfig.ServiceSign, condition.Index);
            var status           = this.CacheClient.GetCache <IndexOperateStatus>(indexMappingName);

            this.IndexOperate.CkeckIndexStatus <T>(status, indexMappingName);

            if (condition.SearchFields == null || condition.SearchFields.Length == 0)
            {
                throw new ElasticsearchException("全文检索失败:查询字段不能为空");
            }

            var searchRequest = new SearchRequest <T>(indexMappingName);

            searchRequest.Size = condition.SizeNumber;
            searchRequest.From = condition.SkipNumber;

            var boolQuery = new BoolQuery();
            List <QueryContainer> queryContainers = new List <QueryContainer>();

            if (condition.SearchFields.Length > 1)
            {
                queryContainers.Add(new MultiMatchQuery {
                    Fields = condition.SearchFields, Query = condition.SearchValue
                });
            }
            else
            {
                queryContainers.Add(new MatchQuery {
                    Field = condition.SearchFields[0], Query = condition.SearchValue
                });
            }
            boolQuery.Must = queryContainers;
            if (condition.FilterPredicate != null)
            {
                boolQuery.Filter = new List <QueryContainer> {
                    condition.FilterPredicate.GetQuery()
                };
            }

            searchRequest.Query = boolQuery;

            if (condition.Sort != null && condition.Sort.Count() > 0)
            {
                searchRequest.Sort = condition.Sort.Select(x => (ISort) new SortField
                {
                    Field = x.Field,
                    Order = x.isAsc ? SortOrder.Ascending : SortOrder.Descending
                }).ToList();
            }

            var highlight = new Highlight();
            var dic       = new Dictionary <Field, IHighlightField>();

            foreach (var field in condition.SearchFields)
            {
                dic.Add(field, new HighlightField());
            }
            highlight.Fields = dic;

            searchRequest.Highlight = highlight;

            var response = Client.Search <T>(searchRequest);

            if (!response.IsValid)
            {
                throw new ElasticsearchException("全文检索失败:" + response.OriginalException.Message);
            }
            if (condition.IsHighLight)
            {
                return(response.Hits.Select(x => this.SetHightValue(x.Source, x.Highlights)));
            }
            return(response.Hits.Select(x => x.Source));
        }