Exemple #1
0
        public IActionResult MagnetSearch(string key, string index)
        {
            key = key.Trim();
            if (string.IsNullOrEmpty(key))
            {
                return(RedirectToAction("index"));
            }
            int.TryParse(index, out int currentIndex);
            currentIndex = currentIndex < 1 ? 1 : currentIndex > 100 ? 100 : currentIndex; //最多100页
            var pageSize = 10;
            var result   = new MagnetLinkSearchResultViewModel();

            //只缓存前20页数据  1分钟
            if (currentIndex > 20)
            {
                result = _magnetSearchService.MagnetLinkSearch(key, currentIndex, pageSize);
            }
            else
            {
                var cacheKey = $"s_m_{key}_{currentIndex}";
                if (!_memoryCache.TryGetValue(cacheKey, out result))
                {
                    result = _magnetSearchService.MagnetLinkSearch(key, currentIndex, pageSize);
                    _memoryCache.Set(cacheKey, result, new DateTimeOffset(DateTime.Now.AddMinutes(5)));
                }
            }
            return(View(result));
        }
        public MagnetLinkSearchResultViewModel MagnetLinkSearch(string key, int pageIndex, int pageSize = 10)
        {
            var result = new MagnetLinkSearchResultViewModel()
            {
                SearchId  = Guid.NewGuid().ToString(),
                SearchKey = key,
                PageIndex = pageIndex,
                PageSize  = pageSize
            };

            var response = _client.Search <MagnetLink>(s => s
                                                       .Index(IndexName)
                                                       .Type(TypeName)
                                                       .From((pageIndex - 1) * pageSize)
                                                       .Size(pageSize)
                                                       //.Query(q => q.
                                                       //    MultiMatch(mm => mm.Fields(fs => fs.Fields(f => f.Name, f => f.InfoHash)).Query(key)
                                                       //    ))
                                                       //.Query(q => q.
                                                       //    Match(m => m.Field(f => f.Name).Query(key)
                                                       //))
                                                       //.Query(q => q
                                                       //    .MatchAll()
                                                       //)
                                                       .Query(q => q
                                                              .Bool(b => b
                                                                    .Should(sd => sd
                                                                            .Term(t => t.Field(f => f.InfoHash).Value(key)),
                                                                            sd => sd
                                                                            .Match(m => m
                                                                                   .Field(f => f.Name)
                                                                                   .Query(key)
                                                                                   )
                                                                            )))
                                                       .Highlight(h => h
                                                                  //.PreTags("<em>")
                                                                  //.PostTags("</em>")
                                                                  .Fields(
                                                                      fs => fs.Field(f => f.Name)
                                                                      )

                                                                  )
                                                       //.Sort(st => st.Descending(d => d.CreateTime))
                                                       .Source(sc => sc.IncludeAll())
                                                       );

            result.Totals   = (int)response.Total;
            result.FindTime = response.Took.ToString();//毫秒
            foreach (var hit in response.Hits)
            {
                var item = hit.Source;
                //高亮
                var highlightValue = new HighlightHit();
                if (hit.Highlights.TryGetValue("name", out highlightValue))
                {
                    item.Name = highlightValue.Highlights.FirstOrDefault() ?? item.Name;
                }

                result.SearchResult.Add(item);
            }
            return(result);
        }