Пример #1
0
        public async Task <IActionResult> GetCodeNewsAsync([FromHeader] String source,
                                                           [FromRoute] SiteIdRoute route,
                                                           [FromQuery] PagerElastic item)
        {
            var response = new Response <List <NewsListResponse> >();

            try {
                var request = new SearchRequest <WebNewsDoc>(_IWebNewsElastic.IndexName)
                {
                    TrackTotalHits = true,
                    Query          = new BoolQuery()
                    {
                        Must = new QueryContainer[] {
                            new TermQuery()
                            {
                                Field = "siteId",
                                Value = route.mark
                            } &&
                            new TermQuery()
                            {
                                Field = "specialCode",
                                Value = route.id
                            }
                        },
                        MustNot = new QueryContainer[] {
                            new TermQuery()
                            {
                                Field = "contentType",
                                Value = 2
                            }
                        }
                    },
                    Source = new Union <bool, ISourceFilter>(new SourceFilter {
                        Excludes = new[] { "contents" }
                    }),
                    Sort = new List <ISort>()
                    {
                        new FieldSort()
                        {
                            Field = "specialSort", Order = SortOrder.Ascending
                        },
                        new FieldSort()
                        {
                            Field = "pushTime", Order = SortOrder.Descending
                        }
                    },
                    Size = item.PageSize
                };
                if (item.PageIndex != null)
                {
                    request.From        = 0;
                    request.SearchAfter = item.PageIndex.Split(",");
                }
                var result = await this._IWebNewsElastic.Client
                             .SearchAsync <NewsListResponse>(request);

                if (result.ApiCall.Success && result.ApiCall.HttpStatusCode == 200)
                {
                    if (result.Documents.Count > 0)
                    {
                        response.Code  = true;
                        response.Data  = result.Documents.ToList();
                        response.Other = string.Join(',', result.Hits.LastOrDefault().Sorts);
                    }
                    else
                    {
                        return(NoContent());
                    }
                }
                else
                {
                    return(NoContent());
                }
            }
            catch (Exception ex) {
                response.SetError(ex, this._ILogger);
            }
            return(response.ToHttpResponse());
        }
Пример #2
0
        public async Task <IActionResult> GetCategoryNewsAsync([FromHeader] String source,
                                                               [FromRoute] SiteIdRoute route,
                                                               [FromQuery] PagerElastic item)
        {
            var response = new Response <List <NewsListResponse> >();

            try {
                //根据当前系统版本号区分是否显示视频
                var entity = source.ToObject <HeaderSource>();

                // 针对安卓版本判断
                var  lastVers        = new Version("1.0.4"); // 最后版本
                var  defalutVersCode = 5;                    //当前安卓版本code
                var  newVers         = new Version();
                var  newVersInt      = 0;
                bool newsBool;
                try {
                    newVers = new Version(entity.SystemVers); // 最新版本
                }
                catch (Exception) {
                    newVersInt = entity.SystemVers.ToInt();
                }
                if (newVersInt == 0)
                {
                    newsBool = entity.Device == "android" && newVers <= lastVers;
                }
                else
                {
                    newsBool = entity.Device == "android" && newVersInt <= defalutVersCode;
                }

                //判断是否展示视频
                if (newsBool)
                {
                    var request = new SearchRequest <WebNewsDoc>(_IWebNewsElastic.IndexName)
                    {
                        TrackTotalHits = true,
                        Query          = new BoolQuery()
                        {
                            Must = new QueryContainer[] {
                                new TermQuery()
                                {
                                    Field = "siteId",
                                    Value = route.mark
                                } &&
                                new TermQuery()
                                {
                                    Field = "categoryId",
                                    Value = route.id
                                }
                            },
                            MustNot = new QueryContainer[] {//过滤掉类型为视频的数据
                                new TermQuery()
                                {
                                    Field = "contentType",
                                    Value = 2
                                }
                            },
                        },
                        Source = new Union <bool, ISourceFilter>(new SourceFilter {
                            Excludes = new[] { "contents" }
                        }),
                        Sort = new List <ISort>()
                        {
                            new FieldSort {
                                Field = "pushTime", Order = SortOrder.Descending
                            },
                            new FieldSort {
                                Field = "categorySort", Order = SortOrder.Ascending
                            }
                        },
                        Size = item.PageSize
                    };
                    if (item.PageIndex != null)
                    {
                        request.From        = 0;
                        request.SearchAfter = item.PageIndex.Split(",");
                    }
                    var result = await this._IWebNewsElastic.Client
                                 .SearchAsync <NewsListResponse>(request);

                    if (result.ApiCall.Success && result.ApiCall.HttpStatusCode == 200)
                    {
                        if (result.Documents.Count > 0)
                        {
                            response.Code    = true;
                            response.Data    = result.Documents.ToList();
                            response.Message = $"返回{result.Documents.Count}条数据";
                            response.Other   = string.Join(',', result.Hits.LastOrDefault().Sorts);
                        }
                    }
                }
                else
                {
                    //根据新闻视频2:1比例设置Size
                    int?newsSize = 0, newsRido = 2;
                    int?videoSize = 0;

                    if (item.PageSize < 10)
                    {
                        item.PageSize = 10;
                    }
                    if (item.PageSize % newsRido == 0)
                    {
                        newsSize = item.PageSize / newsRido + 2;
                    }
                    else
                    {
                        newsSize = (item.PageSize + 1) / newsRido + 2;
                    }
                    videoSize = item.PageSize - newsSize;
                    int?     from         = null;
                    string[] searechAfter = null;

                    if (item.PageIndex != null)
                    {
                        from         = 0;
                        searechAfter = item.PageIndex.Split('|');
                    }
                    //es多重查询
                    var request = new MultiSearchRequest()
                    {
                        TotalHitsAsInteger = true,
                        Operations         = new Dictionary <string, ISearchRequest>
                        {
                            { "news", new SearchRequest <NewsListResponse>(_IWebNewsElastic.IndexName)
                              {
                                  Query = new BoolQuery()
                                  {
                                      Must = new QueryContainer[] {
                                          new TermQuery {
                                              Field = "siteId",
                                              Value = route.mark
                                          } &&
                                          new TermQuery {
                                              Field = "categoryId",
                                              Value = route.id
                                          } &&
                                          new TermQuery {
                                              Field = "contentType",
                                              Value = 1
                                          }
                                      }
                                  },
                                  Sort = new List <ISort>()
                                  {
                                      new FieldSort()
                                      {
                                          Field = "pushTime", Order = SortOrder.Descending
                                      },
                                      new FieldSort()
                                      {
                                          Field = "categorySort", Order = SortOrder.Ascending
                                      }
                                  },
                                  From        = from,
                                  Size        = newsSize,
                                  SearchAfter = item.PageIndex != null?searechAfter[0].Split(','):null
                              } },
                            { "video", new SearchRequest <NewsListResponse>(_IWebNewsElastic.IndexName)
                              {
                                  Query = new BoolQuery()
                                  {
                                      Must = new QueryContainer[] {
                                          new TermQuery {
                                              Field = "siteId",
                                              Value = route.mark
                                          } &&
                                          new TermQuery {
                                              Field = "categoryId",
                                              Value = route.id
                                          } &&
                                          new TermQuery {
                                              Field = "contentType",
                                              Value = 2
                                          }
                                      }
                                  },
                                  Sort = new List <ISort>()
                                  {
                                      new FieldSort()
                                      {
                                          Field = "pushTime", Order = SortOrder.Descending
                                      },
                                      new FieldSort()
                                      {
                                          Field = "categorySort", Order = SortOrder.Ascending
                                      }
                                  },
                                  From        = from,
                                  Size        = videoSize,
                                  SearchAfter = item.PageIndex != null?(searechAfter.Count() > 1?searechAfter[1].Split(','):null):null
                              } }
                        }
                    };
                    var result = await this._IWebNewsElastic.Client.MultiSearchAsync(request); //es多重查询

                    var newsResult  = result.GetResponse <NewsListResponse>("news");           //新闻
                    var videoResult = result.GetResponse <NewsListResponse>("video");          //视频

                    if (newsResult != null && newsResult.ApiCall.Success && videoResult != null && videoResult.ApiCall.Success)
                    {
                        if (newsResult.Documents.Count > 0 || videoResult.Documents.Count > 0)
                        {
                            response.Code = true;
                            //根据ContentType返回结果 2视频 1新闻
                            //每隔两个新闻放置一个视频
                            var vieoList  = videoResult.Documents.Where(c => !string.IsNullOrEmpty(c.ImagePath)).ToList(); //视频结果集
                            var newsList  = newsResult.Documents.ToList();                                                 //新闻结果集
                            var newsIndex = 0;
                            var indexof   = 0;
                            for (int i = 0; i < vieoList.Count; i++)
                            {
                                if (i == 0)
                                {
                                    indexof += 2;
                                }
                                else
                                {
                                    indexof += 3;
                                }
                                if (newsList.Count == item.PageSize)
                                {
                                    break;
                                }
                                if (indexof < newsList.Count)
                                {
                                    newsList.Insert(indexof, vieoList[newsIndex]);
                                }
                                else
                                {
                                    newsList.Insert(newsList.Count, vieoList[newsIndex]);
                                }
                                newsIndex++;
                            }
                            response.Data = newsList;
                            if (newsResult.Hits.Count > 0 && videoResult.Hits.Count == 0)
                            {
                                response.Other = string.Join(",", newsResult.Hits.LastOrDefault().Sorts);
                            }
                            if (newsResult.Hits.Count > 0 && videoResult.Hits.Count > 0)
                            {
                                response.Other = string.Join(",", newsResult.Hits.LastOrDefault().Sorts) + "|" + string.Join(",", videoResult.Hits.LastOrDefault().Sorts);
                            }
                            response.Message = $"返回{newsList.Count}条数据";
                        }
                        else
                        {
                            return(NoContent());
                        }
                    }
                    else
                    {
                        return(NoContent());
                    }
                }
            }
            catch (Exception ex) {
                response.SetError(ex, this._ILogger);
            }
            return(response.ToHttpResponse());
        }