public static List <ArticleES> GetESArticleBykeyWord(string keyWord) { List <ArticleES> result = new List <ArticleES>(); try { var response = ElasticsearchHelper.CreateClient() .Search <ArticleES>(s => s .Index(articleIndexName) .Type(articleTypeName) .Query(q => q .MultiMatch(t => t .Fields(f => f .Field(ae => ae.title)) .Query(keyWord).MinimumShouldMatch("100%")))); if (response != null && response.IsValid) { result = response.Hits.Select(t => t.Source).ToList(); } } catch (Exception ex) { logger.Error(ex); } return(result); }
public static List <ArticleES> GetESArticleById(int id) { List <ArticleES> result = new List <ArticleES>(); try { var response = ElasticsearchHelper.CreateClient() .Search <ArticleES>(s => s .Index(articleIndexName) .Type(articleTypeName) .Query(m => m .Term(mt => mt .Field(ae => ae.id) .Value(id)))); if (response != null && response.IsValid) { result = response.Hits.Select(t => t.Source).ToList(); } } catch (Exception ex) { logger.Error(ex); } return(result); }
private bool DeleteEsData(IEnumerable <UserMessageBoxInfoRelation> relations) { if (relations != null && relations.Any()) { var operations = new List <IBulkOperation>(); var exlogs = ConvertMessageLogToEsLog(relations); foreach (var log in exlogs) { operations.Add(new BulkDeleteOperation <UserMessageBoxInfoRelationC>(log.Id) { Routing = log.UserId }); } var bulkRequest = new BulkRequest(UserMessageBoxRelationIndex) { Operations = operations.ToArray() }; var client = ElasticsearchHelper.CreateClient(); #if DEBUG ElasticsearchHelper.EnableDebug(); #endif var responseOne = client.Bulk(bulkRequest); var errorcount = responseOne.ItemsWithErrors.Count(); DalMessageBox.WriteSyncLogs(relations); return(errorcount <= 0); } return(false); }
protected override void Append(LoggingEvent loggingEvent) { var logline = new ESLogLine(loggingEvent); var client = ElasticsearchHelper.CreateClient(); client.AddToIndex("es_log", new List <ESLogLine>() { logline }); }
private void DeleteESGroupBuyingProducts(List <ESGroupBuyingProduct> products) { var client = ElasticsearchHelper.CreateClient(); var response = client.Bulk(new BulkRequest("pintuanproduct") { Operations = products.Select <ESGroupBuyingProduct, IBulkOperation>(item => new BulkDeleteOperation <ESGroupBuyingProduct>(item) { Routing = item.ProductGroupId }).ToList() }); if (!response.IsValid) { var productGroupId = products.Select(p => p.ProductGroupId).FirstOrDefault(); Logger.Warn($"{productGroupId} ES数据删除失败:{JsonConvert.SerializeObject(response.ItemsWithErrors)}"); } }
private List <ESGroupBuyingProduct> GetESGroupBuyingProducts(string productGroupId) { var array = new List <Func <QueryContainerDescriptor <ESGroupBuyingProduct>, QueryContainer> > { q => q.Term(f => f.Field(fd => fd.ProductGroupId).Value(productGroupId)) }; var client = ElasticsearchHelper.CreateClient(); var response = client.Search <ESGroupBuyingProduct>( s => s.Index("pintuanproduct") .Type("GroupBuyingProduct") .Query(q => q.Bool(qb => qb.Must(array))) .Size(100)); var esProducts = new List <ESGroupBuyingProduct>(); if (response.IsValid && response.Documents?.Any() == true) { esProducts = response.Documents.ToList(); } return(esProducts); }
public void Execute(IJobExecutionContext context) { Logger.Info($"刷新开始执行"); int count = 0; int maxpkid = 0; int exceptioncount = 0; string runtimename = "HandleExpiredMessageBox"; var result = CheckIsOpenWithDescription(runtimename); if (!result.Item1) { Logger.Info("开关已关,return"); return; } int.TryParse(result.Item2, out maxpkid); #if DEBUG maxpkid = 0; #endif Logger.Info($"maxpkid:{maxpkid}"); var starttime = DateTime.Now; while (true && count <= 554948) { count++; try { result = CheckIsOpenWithDescription(runtimename); if (!result.Item1) { Logger.Info("开关已关,return"); return; } Logger.Info($"第{count}批次循环刷新开始执行,maxpkid:{maxpkid}"); var relations = SelectMessageRelation(maxpkid, starttime); if (relations != null && relations.Any()) { maxpkid = relations.Max(x => x.PKID); var operations = new List <IBulkOperation>(); var exlogs = ConvertMessageLogToEsLog(relations); foreach (var log in exlogs) { operations.Add(new BulkDeleteOperation <UserMessageBoxInfoRelationC>(log.Id) { Routing = log.UserId }); } var bulkRequest = new BulkRequest(DeleteExpiredMessageBoxJob.UserMessageBoxRelationIndex) { Operations = operations.ToArray() }; var client = ElasticsearchHelper.CreateClient(); #if DEBUG ElasticsearchHelper.EnableDebug(); #endif var responseOne = client.Bulk(bulkRequest); var errorcount = responseOne.ItemsWithErrors.Count(); WriteSyncLogs(relations); UpdateRunTimeSwitchDescription(runtimename, maxpkid.ToString()); Logger.Info($"第{count}批次结束刷新.errorcount:{errorcount}"); } else { break; } } catch (System.Exception ex) { maxpkid -= 1000; Logger.Warn(ex); exceptioncount++; if (exceptioncount >= 10) { break; } } } Logger.Info($"刷新结束"); }
public static async Task <bool> InsertOrUpdateES(Article article) { var result = false; if (ConfigurationManager.AppSettings["IsOpen"] == "true") { try { if (article.Type == 1 || article.Type == 0 || article.Status == ArticleStatus.Published.ToString() || article.Status == ArticleStatus.Withdrew.ToString()) { var createIndex = await ElasticsearchHelper.CreateClient() .CreateIndexIfNotExistsAsync(articleIndexName, c => c .Settings(cs => cs.NumberOfShards(2).NumberOfReplicas(1)) //设置副本和分片数 .Mappings(cm => cm.MapDefault() .Map <ArticleES>(m => m .AutoMap())) ); if (createIndex) { if (article.IsShow == 1 || article.Status == ArticleStatus.Published.ToString()) { var response = await ElasticsearchHelper.CreateClient() .IndexAsync(new ArticleES { id = article.PKID, title = article.SmallTitle, body = "", created_at = article.CreateDateTime.ToString("yyyy-MM-dd HH:mm:ss"), updated_at = article.LastUpdateDateTime.ToString("yyyy-MM-dd HH:mm:ss"), reply_count = "0", view_count = "0", vote_count = "0" }, i => i.Index(articleIndexName).Type(articleTypeName)); if (response.IsValid) { result = true; } } else { var response = await ElasticsearchHelper.CreateClient() .DeleteAsync <ArticleES>(article.PKID.ToString(), d => d.Index(articleIndexName).Type(articleTypeName)); if (response.IsValid) { result = true; } } } } } catch (Exception ex) { logger.Error(ex); } } return(result); }
public async static Task <PagedModel <HomePageTimeLineRequestModel> > SelectDiscoveryHomeAsync(string UserId, PagerModel page, int version) { //关注的类目ID List <int> CategoryIds = new List <int>(); //关注的文章ID List <int> ArticleIDs = new List <int>(); //关注的用户ID List <string> AttentionUserIds = new List <string>(); //关注的用户ID 不包含技师和途虎员工 List <string> AttentionUserIdsNoTuhu = new List <string>(); CategoryIds.Add(0); ArticleIDs.Add(0); AttentionUserIdsNoTuhu.Add("jiadeshujuyongyuguolu"); var cids = await DalArticle.SelectMyCategoryId(UserId); if (cids != null && cids.Any()) { CategoryIds = cids.Select(x => x.CategoryId).ToList(); } var atcid = await DalArticle.SelectMyArticleID(UserId); if (atcid != null && atcid.Any()) { ArticleIDs = atcid.Select(x => x.PKID).ToList(); } var atuid = await DalArticle.SelectMyAttentionUserId(UserId); if (atuid != null && atuid.Any()) { AttentionUserIds = atuid.Select(x => x.AttentionUserId).ToList(); if (atuid.Any(x => x.UserIdentity == null || x.UserIdentity == 0)) { AttentionUserIdsNoTuhu = atuid.Where(x => x.UserIdentity == null || x.UserIdentity == 0).Select(x => x.AttentionUserId).ToList(); } } AttentionUserIds.AddRange(new string[] { "{c69cf542-7b0c-8494-8831-c1a9a2fe6388}", "{f8479c14-3bb1-4711-9a38-2626359b8c28}", "{935b3462-9310-59ce-d742-0cbc1c276ebf}", "{81c9ed57-008c-b2ba-53c8-1cc35caf69c0}" }); var types = new List <int> { 1, 2, 5 }; if (version > 1) { types.Add(11); } var client = ElasticsearchHelper.CreateClient(); var b = await client.SearchAsync <DiscoveryArticleModel>(x => x .Index("discoveryarticle") .Type("Discovery") .Query(q => q. Bool(qb => qb. Should( qs => qs. Bool(qsb => qsb. Must(qsbm => qsbm. Terms(qt => qt. Field(qf => qf.AttentionUserIds). Terms(AttentionUserIds)), qsbm => qsbm. Terms(qt => qt. Field(qf => qf.TYPE). Terms(types)) ) ), qs => qs. Bool(qsb => qsb. Must(qsbm => qsbm. Terms(qt => qt. Field(qf => qf.AttentionUserIds). Terms(AttentionUserIdsNoTuhu)), qsbm => qsbm. Terms(qt => qt. Field(qf => qf.TYPE). Terms(3, 4)) ) ), qs => qs. Bool(qsb => qsb. Must( qsbm => qsbm. Terms(qt => qt. Field(qf => qf.PKID). Terms(ArticleIDs)), qsbm => qsbm. Terms(qt => qt. Field(qf => qf.TYPE). Terms(6)), qsbm => qsbm.Bool(qsbmb => qsbmb. Should( qsbmbs => qsbmbs. Term(qt => qt. Field(qf => qf.AuditStatus). Value(2)), qsbmbs => qsbmbs. Term(qt => qt. Field(qf => qf.AttentionUserIds). Value(UserId)))) ) ), qs => qs. Bool(qsb => qsb. Must( qsbm => qsbm. Terms(qt => qt. Field(qf => qf.CategoryId). Terms(CategoryIds)), qsbm => qsbm. Terms(qt => qt. Field(qf => qf.TYPE). Terms(7)) ) ), qs => qs. Bool(qsb => qsb. Must( qsbm => qsbm. Terms(qt => qt. Field(qf => qf.CategoryId). Terms(CategoryIds)), qsbm => qsbm. Terms(qt => qt. Field(qf => qf.TYPE). Terms(8)), qsbm => qsbm.Bool(qsbmb => qsbmb. Should( qsbmbs => qsbmbs. Term(qt => qt. Field(qf => qf.AuditStatus). Value(2)), qsbmbs => qsbmbs. Term(qt => qt. Field(qf => qf.AttentionUserIds). Value(UserId))) ) ) ) ) ) ) .Sort(s => s. Descending(ss => ss.OperateTime)) .Skip((page.CurrentPage - 1) * page.PageSize) .Take(page.PageSize)); page.Total = (int)b.Total; var a = b.Hits.Select(x => x.Source).Select(x => new HomePageTimeLineRequestModel() { PKID = x.PKID, Type = x.TYPE, DistinctId = x.DISTINCTID, AnswerId = x.AnswerId, AnswerContent = x.AnswerContent, AttentionCount = x.AttentionCount, BigTitle = x.BigTitle, Brief = x.Brief, VoteNum = x.VoteNUm, CategoryId = x.CategoryId, AttentionUserIds = x.AttentionUserIds, CategoryImage = x.CategoryImage, CategoryName = x.CategoryName, CategoryTags = x.CategoryTags, ClickCount = x.ClickCount, CommentImage = x.CommentImage, CommentTimes = x.CommentTimes, Content = x.Content, ContentUrl = x.ContentUrl, CreatorInfo = x.CreatorInfo, OperateTime = x.OperateTime, Praise = x.Praise, RelatedArticleId = x.RelatedArticleId, ShowImages = x.ShowImages, ShowType = x.ShowType, SmallImage = x.SmallImage, SmallTitle = x.SmallTitle, ImagesCount = x.ImagesCount }).ToList(); var groupTypeTimeLine = a.GroupBy(t => new { t.PKID, t.DistinctId, t.Type }).ToList(); var tempTimeLineList = new List <HomePageTimeLineRequestModel>(); //分组拼接所有关注的人 foreach (var timeLine in groupTypeTimeLine) { var tempTimeLine = timeLine.OrderByDescending(t => t.OperateTime).FirstOrDefault(); tempTimeLine.AttentionCount = timeLine.Select(t => t.AttentionUserIds).Distinct().Count(); tempTimeLine.AttentionUserIds = string.Join("、", timeLine.Select(t => t.AttentionUserIds).Distinct().ToArray <string>()); tempTimeLineList.Add(tempTimeLine); } var groupFilterTimeLine = tempTimeLineList.GroupBy(t => new { t.PKID, t.AnswerId }).ToList(); tempTimeLineList.Clear(); //分组去掉重复的数据 foreach (var timeLine in groupFilterTimeLine) { var tempTimeLine = timeLine.OrderByDescending(t => t.OperateTime).FirstOrDefault(); tempTimeLineList.Add(tempTimeLine); } tempTimeLineList = tempTimeLineList.Select(t => t).OrderByDescending(t => t.OperateTime).ToList(); return(new PagedModel <HomePageTimeLineRequestModel>(page, tempTimeLineList));; }