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("缓存更新失败"); } })); }
/// <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); }
/// <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())); }
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); } }
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); }
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("缓存更新失败"); } })); }
/// <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)); }