public SearchResult <TEntity> FullTextSearch(SearchQueryItem queryItem) { if (queryItem == null) { throw new ArgumentNullException(nameof(queryItem)); } int from = ((queryItem.Pagination.Page - 1) * queryItem.Pagination.PageSize); ElasticsearchQueryContainer <TEntity> query = ElasticsearchQueryBuilder <TEntity> .Create().Build(queryItem); SortDescriptor <TEntity> sort = new SortDescriptor <TEntity>(); ISearchResponse <TEntity> response = this.client.Search <TEntity>(i => i.Index(this.aliasName ?? this.currentIndex) .Type(currentType) .Source(d => queryItem.Select != null? d.Includes(cc => cc.Fields(queryItem.Select)) : d) .Size(queryItem.Pagination.PageSize) .Query(q => query.Query) .Sort(s => sort) .Aggregations(agg => query.Aggregation) .Highlight(h => h.Fields(ff => ff.Field("*") .Type(HighlighterType.Plain)) .PreTags("<span class='highlight'>") .PostTags("</span>") .RequireFieldMatch(false))); SearchResult <TEntity> result = this.GetElasticSearchResult(response); return(result); }
public async Task <IActionResult> GetArticles(int?ID, int page = 1) { if (ID == null) { return(NotFound()); } var topic = await _context.Topics.SingleOrDefaultAsync(s => s.ID == ID); if (topic == null) { throw new ApplicationException($"Unable to load topic with ID '{ID}'."); } //var request = topic.ESRequest; int size = 20; int from = (page - 1) * size; var request = new SearchRequest { Query = ElasticsearchQueryBuilder.QueryBuilder(JObject.Parse(topic.Rules)), Sort = new List <ISort> { new SortField { Field = "modified", Order = SortOrder.Descending }, new SortField { Field = "_score", Order = SortOrder.Descending } }, Size = size, From = from }; var result = await _client.SearchAsync <Articles>(request); //bool loadmore = (page * size <= (int)result.Total); //var esreq = _client.RequestResponseSerializer.SerializeToString(request); var model = new TopicViewModel { ID = (int)ID, TopicName = topic.TopicName, NoOfPosts = (int)result.Total, page = page, size = size, Articles = result.Documents }; ViewData["Page"] = page; //ViewData["More"] = loadmore; return(PartialView("_ArticleList", model)); }
public async Task <IActionResult> Create([Bind("TopicName,Description,StartDate,EndDate, isFeatured, Rules")] TopicViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var rules = model.Rules; var request = new SearchRequest { Query = ElasticsearchQueryBuilder.QueryBuilder(JObject.Parse(rules)), Size = 50 }; var topic = new Topics { TopicName = model.TopicName, Description = model.Description, DateCreated = DateTime.Now, StartDate = model.StartDate, EndDate = model.EndDate, Rules = model.Rules, ESRequest = _client.RequestResponseSerializer.SerializeToString(request), isFeatured = model.isFeatured }; try { _context.Add(topic); var result = await _context.SaveChangesAsync(); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } } StatusMessage = "Thêm mới chủ đề thành công!"; return(RedirectToAction(nameof(Index))); //return View("Index", model); }
public async Task <IActionResult> Edit(int?ID, [Bind("ID,TopicName,Description,Rules, isFeatured")] TopicViewModel model) { var topic = await _context.Topics.SingleOrDefaultAsync(s => s.ID == ID); if (topic == null) { throw new ApplicationException($"Unable to load topic with ID '{ID}'."); } if (ModelState.IsValid) { var request = new SearchRequest { Query = ElasticsearchQueryBuilder.QueryBuilder(JObject.Parse(model.Rules)), Size = 50 }; try { topic.TopicName = model.TopicName; topic.Description = model.Description; topic.isFeatured = model.isFeatured; topic.Rules = model.Rules; topic.ESRequest = _client.RequestResponseSerializer.SerializeToString(request); _context.Update(topic); await _context.SaveChangesAsync(); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); } } TempData["SuccessMessage"] = "Cập nhật chủ đề thành công!"; return(RedirectToAction(nameof(Index))); }
//[MiddlewareFilter(typeof(JsReportPipeline))] public async Task <IActionResult> ReportByTopics(int ID, string ReportTitle, string StartDate, string EndDate) { var model = new ReportViewModel(); var topic = await _context.Topics.SingleOrDefaultAsync(s => s.ID == ID); if (topic == null) { throw new ApplicationException($"Unable to load topic with ID '{ID}'."); } var request = new SearchRequest { Query = ElasticsearchQueryBuilder.QueryBuilder(JObject.Parse(topic.Rules)), PostFilter = new QueryContainer( new DateRangeQuery { Field = "modified", Format = "dd-MM-YYYY", GreaterThanOrEqualTo = StartDate, LessThanOrEqualTo = EndDate }), Sort = new List <ISort> { new SortField { Field = "modified", Order = SortOrder.Descending }, new SortField { Field = "_score", Order = SortOrder.Descending } }, }; var response = await _client.SearchAsync <Articles>(request); if (ModelState.IsValid) { var data = new { reporttitle = ReportTitle, startdate = StartDate, enddate = EndDate, topicname = topic.TopicName, took = response.Took, total = response.Total, articles = response.Documents }; var report = await _rs.RenderAsync(new RenderRequest() { Template = new Template() { Name = "report-by-topic" }, Data = JsonConvert.SerializeObject(data) }); Debug.WriteLine(JsonConvert.SerializeObject(data)); //HttpContext. return(new FileStreamResult(report.Content, "application/pdf")); //return report.Content } //HttpContext.JsReportFeature().Recipe(Recipe.PhantomPdf); return(RedirectToAction("Index")); }