Ejemplo n.º 1
0
        public static void HowToUseAggregationsInES(IElasticClient client,
                                                    SearchDescriptor <ProductListEsIndexModelExample> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            //select x,count(1) from tb group by x
            sd = sd.Aggregations(a => a.Terms("terms", x => x.Field(m => m.IsGroup))).Size(1000);
            //select count(f) from tb where f is not null
            sd = sd.Aggregations(a => a.ValueCount("count", x => x.Field(m => m.IsGroup))).Size(1000);
            //最大值
            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup))).Size(1000);
            //最大值,最小值,平均值等统计数据
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));
            //数值区域
            sd = sd.Aggregations(a => a.Range("range", x => x.Field("price").Ranges(r => r.From(10).To(20), r => r.From(30).To(40))));
            //日期区域
            sd = sd.Aggregations(a => a.DateRange("date_range", x => x.Field("date").Ranges(r => r.From(DateTime.Now.AddDays(-1)).To(DateTime.Now))));
            //ip区域
            sd = sd.Aggregations(a => a.IpRange("ip_range", x => x.Field("ip").Ranges(r => r.From("192.168.0.1").To("192.168.0.10"))));

            var response = client.Search <ProductListEsIndexModelExample>(x => sd);

            var stats = response.Aggregations.Stats("stats");
            //etc
        }
Ejemplo n.º 2
0
        public static void HowToUseAggregationsInES(this SearchDescriptor <EsExample.ProductListV2> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", Field.Create("")) && new AverageAggregation("", Field.Create(""));

            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup)));
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));

            var response = ElasticsearchClientManager.Instance.DefaultClient.CreateClient().Search <EsExample.ProductListV2>(x => sd);

            var stats = response.Aggs.Stats("stats");
            //etc
        }
Ejemplo n.º 3
0
        public ISearchResponse <T> Query(QueryDes qd)
        {
            var s = new SearchDescriptor <T>().Index(_config.ES_INDEX).Type(_config.ES_TYPE).From(qd.From).Take(qd.Take)
                    .Source(sr => GetSource(sr, qd))
                    .Query(q => GetQuery(q, qd));

            if (qd.SortField != null)
            {
                if (qd.IsAsc)
                {
                    s.Sort(st => st.Ascending(qd.SortField));
                }
                else
                {
                    s.Sort(st => st.Descending(qd.SortField));
                }
            }

            if (qd.TerminateAfter > 0)                  // 设置单个节点上的数据查询截断阈值,提高响应速度(但会导致数据搜索不全面)
            {
                s.TerminateAfter(qd.TerminateAfter);
            }

            if (qd.Aggs != null)
            {
                s.Aggregations(agg => Get_Agg(agg, qd));
            }

            if (qd.HLPreTag != null)
            {
                s.Highlight(hl => Get_HL(hl, qd));
            }

            return(_client.Search <T>(s));
        }
Ejemplo n.º 4
0
        public async Task <ElasticResponse <T> > SearchLogsAggregate(SearchAgreggateCriteria criteria)
        {
            SearchDescriptor <T> queryCommand = new SearchDescriptor <T>()
                                                .Query(q => q
                                                       .Bool(bl => bl
                                                             .Must(
                                                                 ft => new DateRangeQuery
            {
                Field = "dateTimeLogged",
                GreaterThanOrEqualTo = criteria.StartDateTimeLogged,
                LessThanOrEqualTo    = criteria.EndDateTimeLogged
            })
                                                             )
                                                       )
                                                .Size(0);

            queryCommand.Aggregations(AggregateCommand().Result);

            var result = await BasicQuery(queryCommand);

            var response = await _logElasticMappers.MapElasticResults(result);

            if (!result.IsValid)
            {
                throw new InvalidOperationException(result.DebugInformation);
            }

            return(response);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 构建查询描述器
        /// </summary>
        /// <param name="query"></param>
        /// <param name="sort"></param>
        /// <param name="fields"></param>
        /// <param name="aggs"></param>
        /// <param name="from"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public SearchDescriptor <T> BuildSearchDescriptor(QueryContainer query, SortDescriptor <T> sort, ISourceFilter fields,
                                                          IAggregationContainer aggs, int from = 0, int size = 10)
        {
            var searchdesc = new SearchDescriptor <T>()
                             .Index(EsIndex)
                             .Type(EsType)
                             .From(from)
                             .Size(size);

            if (query != null)
            {
                searchdesc = searchdesc.Query(q => query);
            }
            if (sort != null)
            {
                searchdesc = searchdesc.Sort(s => sort);
            }
            if (fields != null)
            {
                searchdesc = searchdesc.Source(s => fields);
            }
            if (aggs != null)
            {
                searchdesc = searchdesc.Aggregations(a => aggs);
            }

            return(searchdesc);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Get the Aggregated token count
        /// </summary>
        /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param>
        /// <param name="ngramCount"></param>
        /// <param name="tagId">if it's null then the Aggregate will be run on all the DocumentElastic</param>
        /// <returns></returns>
        public int GetAllWordsOccurences(List <string> interpretedFields, int ngramCount, string tagField = null)
        {
            var desc    = new SearchDescriptor <DocumentElastic>();
            var aggDesc = new AggregationContainerDescriptor <DocumentElastic>();

            foreach (var interpretedField in interpretedFields)
            {
                aggDesc.Sum(
                    interpretedField,
                    ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}"));
            }

            desc.Size(0);
            desc.Aggregations(a => aggDesc);

            desc.Query(q => q.MatchAll());

            var response = Client.Search <DocumentElastic>(desc);

            ResponseValidator(response);

            // calculate the count for the current n-gram from the unigrams count
            var allCount = response.Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value)) - (ngramCount - 1);

            return(allCount);
        }
Ejemplo n.º 7
0
        internal static ISearchRequest ToSearchRequest <TDocument>(
            this SearchParameters <TDocument> parameters)
            where TDocument : class
        {
            var searchDescriptor = new SearchDescriptor <TDocument>()
                                   .From(parameters.Start)
                                   .Size(parameters.Take)
                                   .Query(queryDescriptor => parameters.QueryBuilder.Build());

            if (parameters.Sorts?.Any() == true)
            {
                searchDescriptor = searchDescriptor.Sort(sortDescriptor => parameters.Sorts.ToSortDescriptor());
            }

            if (parameters.Routings?.Any() == true)
            {
                searchDescriptor = searchDescriptor.Routing(parameters.Routings.ToArray());
            }

            if (parameters.AggregationsBuilder != null)
            {
                searchDescriptor = searchDescriptor.Aggregations(
                    aggregationContainerDescriptor => parameters.AggregationsBuilder.Build());
            }

            return(searchDescriptor);
        }
Ejemplo n.º 8
0
        public static SearchDescriptor <T> Aggregations <T>(this SearchDescriptor <T> descriptor, AggregationContainer aggregations) where T : class
        {
            descriptor.Aggregations(f => {
                ((IAggregationContainer)f).Aggregations = aggregations.Aggregations;
                return(f);
            });

            return(descriptor);
        }
Ejemplo n.º 9
0
        static void HowToUseInnerAgg()
        {
            var sd = new SearchDescriptor <EsIndexExample>();

            sd = sd.Aggregations(agg => agg
                                 .Terms("NAMEOF_ShowCatalogIdList", av => av.Field("NAMEOF_ShowCatalogIdList").Size(1000))
                                 .Terms("NAMEOF_BrandId", av => av.Field("NAMEOF_BrandId").Order(x => x.CountDescending()).Size(1000))
                                 .Terms("NAMEOF_ProductAttributes",
                                        //sub aggregation
                                        av => av.Field("NAMEOF_ProductAttributes")
                                        .Aggregations(m => m.Average("", d => d.Field(""))).Order(xx => xx.Descending("")).Size(1000)));

            //nested agg
            var path = string.Empty;

            sd = sd.Aggregations(x => x
                                 .Nested("attr_nested", _ => _.Path(path).Aggregations(a => a.Terms("attr", f => f.Field($"{path}.UID").Size(1000)))));
        }
Ejemplo n.º 10
0
        public static void HowToUseAggregationsInES(this SearchDescriptor <ProductListEsIndexModel> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup))).Size(1000);
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));

            var response = ElasticsearchClientManager.Instance.DefaultClient.CreateClient().Search <ProductListEsIndexModel>(x => sd);

            var stats = response.Aggregations.Stats("stats");
            //etc
        }
Ejemplo n.º 11
0
        public static void HowToUseInnerAgg()
        {
            var sd = new SearchDescriptor <ProductListEsIndexModelExample>();

            sd = sd.Aggregations(agg => agg
                                 .Terms("NAMEOF_ShowCatalogIdList", av => av.Field("NAMEOF_ShowCatalogIdList").Size(1000))
                                 .Terms("NAMEOF_BrandId", av => av.Field("NAMEOF_BrandId").Order(x => x.CountDescending()).Size(1000))
                                 .Terms("NAMEOF_ProductAttributes",
                                        //妈的 这什么鬼
                                        av => av.Field("NAMEOF_ProductAttributes")
                                        .Aggregations(m => m.Average("", d => d.Field(""))).Order(xx => xx.Descending("")).Size(1000)));
        }
Ejemplo n.º 12
0
        public async Task AggregateAsyncByDescriptorShouldSucceed()
        {
            var descriptor1 = new SearchDescriptor <MockDocument>();
            var descriptor2 = new SearchDescriptor <MockDocument>();

            descriptor1
            .Aggregations(a => a
                          .Terms("is_active",
                                 t => t
                                 .Field(f => f.IsActive)
                                 )
                          );
            descriptor2
            .Aggregations(a => a
                          .Terms("is_active",
                                 t => t
                                 .Field(f => f.IsActive)
                                 )
                          )
            .Query(q => q
                   .Term(t => t
                         .Field(f => f.Name)
                         .Value("xyz")
                         )
                   )
            ;

            var aggs1 = await _mockRepo.AggregateAsync(descriptor1);

            var aggs2 = await _mockRepo.AggregateAsync(descriptor2);

            var res1 = new List <dynamic>();

            foreach (var bucket in (List <IBucket>)((BucketAggregate)aggs1.Aggregations["is_active"]).Items)
            {
                res1.Add(new {
                    ((KeyedBucket <object>)bucket).Key,
                    Count = ((KeyedBucket <object>)bucket).DocCount
                });
            }

            Assert.NotNull(aggs1);
            Assert.True(aggs1.Aggregations.ContainsKey("is_active"));

            Assert.NotEmpty(res1);
            Assert.Equal(2, res1.Count);

            Assert.NotNull(aggs2);
            Assert.False(aggs2.Aggregations.ContainsKey("is_active"));
        }
Ejemplo n.º 13
0
        static void HowToUseAggregationsInES(IElasticClient client,
                                             SearchDescriptor <EsIndexExample> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            //select x,count(1) from tb group by x
            sd = sd.Aggregations(a => a.Terms("terms", x => x.Field(m => m.IsGroup).Order(s => s.CountDescending()).Size(1000)));
            //select count(f) from tb where f is not null
            sd = sd.Aggregations(a => a.ValueCount("count", x => x.Field(m => m.IsGroup)));
            //最大值
            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup)));
            //最大值,最小值,平均值等统计数据
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));
            //数值区域
            sd = sd.Aggregations(a => a.Range("range", x => x.Field("price").Ranges(r => r.From(10).To(20), r => r.From(30).To(40))));
            //日期区域
            sd = sd.Aggregations(a => a.DateRange("date_range", x => x.Field("date").Ranges(r => r.From(DateTime.Now.AddDays(-1)).To(DateTime.Now))));
            //ip区域
            sd = sd.Aggregations(a => a.IpRange("ip_range", x => x.Field("ip").Ranges(r => r.From("192.168.0.1").To("192.168.0.10"))));


            //sd = sd.SearchType(SearchType.QueryThenFetch);
            var response = client.Search <EsIndexExample>(x => sd);

            response.ThrowIfException();

            var terms         = response.Aggregations.Terms("group").Buckets;
            var count         = response.Aggregations.ValueCount("count").Value;
            var stats         = response.Aggregations.Stats("stats");
            var max           = response.Aggregations.Max("max").Value;
            var histogram     = response.Aggregations.Histogram("hist").Buckets.ToDictionary(x => x.Key, x => x.DocCount);
            var datehistogram = response.Aggregations.DateHistogram("date_hist").Buckets.ToDictionary(x => x.Date, x => x.DocCount);
            var range         = response.Aggregations.Range("range").Buckets.Select(x => new { x.From, x.To, x.DocCount });
            var date_range    = response.Aggregations.DateRange("date_range").Buckets.Select(x => new { x.From, x.To, x.DocCount });
            //etc
        }
Ejemplo n.º 14
0
        private async Task <ISearchResponse <T> > FindLog(LogSearchCriteria criteria)
        {
            SearchDescriptor <T> queryCommand = new SearchDescriptor <T>()
                                                .Query(q => q
                                                       .Bool(bl => bl
                                                             .Must(
                                                                 fq =>
            {
                QueryContainer query = null;

                query &= fq.Term(t => t
                                 .Field("httpUrl")
                                 .Value(criteria.Term)
                                 );

                return(query);
            }
                                                                 )
                                                             .Filter(
                                                                 ft => new DateRangeQuery
            {
                Field = "dateTimeLogged",
                GreaterThanOrEqualTo = criteria.StartDateTimeLogged,
                LessThanOrEqualTo    = criteria.EndDateTimeLogged
            }
                                                                 )
                                                             )
                                                       )
                                                .Sort(SortCommand().Result)
                                                .From(0)
                                                .Size(10)
                                                .Highlight(z => z
                                                           .Fields(y => y
                                                                   .Field(criteria.ColumnField)
                                                                   .PreTags("<u>")
                                                                   .PostTags("</u>")
                                                                   )
                                                           .NumberOfFragments(10)
                                                           .FragmentSize(1)
                                                           .Order(HighlighterOrder.Score)
                                                           );


            queryCommand.Aggregations(AggregateCommand().Result);

            return(await BasicQuery(queryCommand));
        }
Ejemplo n.º 15
0
 protected virtual void ApplyAggregations <T>(SearchDescriptor <T> searchDescriptor, SearchTextQuery query) where T : class
 {
     searchDescriptor.Aggregations(agg => agg
                                   .Global(SearchConstants.SearchFacetNames.Types, g => g
                                           .Aggregations(a => a
                                                         .Filter(SearchConstants.SearchFacetNames.GlobalFilter, ss => ss
                                                                 .Filter(fi => fi
                                                                         .Bool(b => b
                                                                               .Should(GetQueryContainers(query.Text))
                                                                               .Must(GetSearchableTypeQueryContainers(query.SearchableTypeIds))
                                                                               .Must(GetOnlyPinnedQueryContainer(query.OnlyPinned))
                                                                               ))
                                                                 .Aggregations(
                                                                     ia =>
                                                                     ia.Terms(SearchConstants.SearchFacetNames.Types,
                                                                              f => f.Field("type").Size(ElasticHelpers.MaxAggregationSize)))))));
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Get the Aggregated token count per tag
        /// </summary>
        /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param>
        /// <param name="ngramCount"></param>
        /// <param name="tagIds">the tagIds to run on</param>
        /// <param name="tagField"></param>
        /// <returns></returns>
        public Dictionary <string, int> CountForWord(List <string> interpretedFields, int ngramCount, List <string> tagIds, string tagField)
        {
            if (!tagIds.Any())
            {
                return(new Dictionary <string, int>());
            }
            var resultDic = new Dictionary <string, int>();

            foreach (var tagIdsBatch in tagIds.Batch(1000))
            {
                var mRequest = new MultiSearchRequest {
                    Operations = new Dictionary <string, ISearchRequest>()
                };
                foreach (var tagId in tagIdsBatch)
                {
                    var desc    = new SearchDescriptor <DocumentElastic>();
                    var aggDesc = new AggregationContainerDescriptor <DocumentElastic>();

                    foreach (var interpretedField in interpretedFields)
                    {
                        aggDesc.Sum(
                            interpretedField,
                            ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}"));
                    }

                    desc.Size(0);
                    desc.Aggregations(a => aggDesc);

                    if (!string.IsNullOrEmpty(tagId))
                    {
                        desc.Query(q => q.Term($"{tagField}", tagId));
                    }
                    else
                    {
                        desc.Query(q => q.MatchAll());
                    }

                    mRequest.Operations.Add(tagId, desc);
                }
                var result = Client.MultiSearch(mRequest);
                ResponseValidator(result);
                tagIdsBatch.ToList().ForEach(tid => resultDic.Add(tid, Convert.ToInt32(result.GetResponse <DocumentElastic>(tid).Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value))) - (ngramCount - 1)));
            }
            return(resultDic);
        }
Ejemplo n.º 17
0
        public override void BuildSearch <T>(object query, object options, ref SearchDescriptor <T> descriptor)
        {
            var facetQuery = query as IFacetQuery;

            if (facetQuery?.FacetFields == null || facetQuery.FacetFields.Count <= 0)
            {
                return;
            }

            var opt = options as IQueryOptions;

            if (opt?.AllowedFacetFields?.Length > 0 && !facetQuery.FacetFields.All(f => opt.AllowedFacetFields.Contains(f.Field)))
            {
                throw new InvalidOperationException("All facet fields must be allowed.");
            }

            descriptor.Aggregations(agg => GetAggregationDescriptor <T>(facetQuery));
        }
Ejemplo n.º 18
0
        public ISearchResponse <ProductListV2> SearchEsProducts(SearchParamModel model)
        {
            ISearchResponse <ProductListV2> response = null;
            var temp = new ProductListV2();

            PrepareES(client =>
            {
                var sd = new SearchDescriptor <ProductListV2>();

                sd = sd.Index(ES_PRODUCTLIST_INDEX);

                sd = sd.Query(q => BuildQuery(model));

                var NAMEOF_ShowCatalogIdList = nameof(temp.ShowCatalogIdList);
                var NAMEOF_BrandId           = nameof(temp.BrandId);
                var NAMEOF_ProductAttributes = nameof(temp.ProductAttributes);

                sd = sd.Aggregations(agg => agg
                                     .Terms(NAMEOF_ShowCatalogIdList, av => av.Field(NAMEOF_ShowCatalogIdList).Size(1000))
                                     .Terms(NAMEOF_BrandId, av => av.Field(NAMEOF_BrandId).Size(1000))
                                     .Terms(NAMEOF_ProductAttributes, av => av.Field(NAMEOF_ProductAttributes).Size(1000)));

                sd = sd.Sort(x => BuildSort(model));

                //var range = PagerHelper.GetQueryRange(model.page, model.pagesize);
                //sd = sd.Skip(range[0]).Take(range[1]);
                sd = sd.QueryPage_(model.page, model.pagesize);

                response = client.Search <ProductListV2>(x => sd);

                var mx = response.Aggs.Max("");

                return(true);
            });
            if (response == null || !response.IsValid || response.OriginalException != null)
            {
                throw new Exception("ES 挂了");
            }
            return(response);
        }
Ejemplo n.º 19
0
        public async Task <ElasticResponse <T> > SearchLogsAggregateByCountryId(GraphCriteria searchCriteria)
        {
            SearchDescriptor <T> queryCommand = new SearchDescriptor <T>()
                                                .Query(q => q
                                                       .Bool(bl => bl
                                                             .Must(
                                                                 fq =>
            {
                QueryContainer query = null;

                query &= fq.Term(t => t
                                 .Field("countryId").Value(searchCriteria.CountryId)
                                 );

                return(query);
            }
                                                                 )
                                                             .Filter(
                                                                 ft => new DateRangeQuery
            {
                Field = "dateTimeLogged",
                GreaterThanOrEqualTo = searchCriteria.StartDateTimeLogged,
                LessThanOrEqualTo    = searchCriteria.EndDateTimeLogged
            })
                                                             )
                                                       );

            queryCommand.Aggregations(AggregateCommand().Result);

            var result = await BasicQuery(queryCommand);

            var response = await _logElasticMappers.MapElasticResults(result);

            if (!result.IsValid)
            {
                throw new InvalidOperationException(result.DebugInformation);
            }

            return(response);
        }
Ejemplo n.º 20
0
        public async Task <object> Aggregation(double lat, double lng)
        {
            var client           = GetClient();
            var searchDescriptor = new SearchDescriptor <Place>();
            var dumper           = new NestDescriptorDumper(client.RequestResponseSerializer);

            var maxRating   = "maxRating";
            var minDistance = "minDistance";

            searchDescriptor
            .Aggregations(aggs =>
                          aggs
                          .Max(maxRating, max => max.Field(f => f.Rating))
                          .Min(minDistance, min => min.Script($"doc['geometry.location'].arcDistance({lat},{lng})"))
                          );

            var results = await client.SearchAsync <Place>(searchDescriptor);

            var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor);

            return(results.Aggregations);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 搜索日志
        /// </summary>
        /// <param name="highlight"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="keyword"></param>
        /// <param name="logger_name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public static async Task <PagerData <ESLogLine, QueryExtData> > Search(
            bool highlight = true,
            DateTime?start = null, DateTime?end       = null,
            string keyword = null, string logger_name = null,
            int page       = 1, int pagesize = 10)
        {
            var sd = new SearchDescriptor <ESLogLine>();

            sd = sd.Index(IndexName);

            var query = new QueryContainer();

            if (start != null)
            {
                query &= new DateRangeQuery()
                {
                    Field = nameof(temp.UpdateTime), GreaterThanOrEqualTo = start.Value
                };
            }
            if (end != null)
            {
                query &= new DateRangeQuery()
                {
                    Field = nameof(temp.UpdateTime), LessThan = end.Value
                };
            }
            if (ValidateHelper.IsPlumpString(keyword))
            {
                query &= new MatchQuery()
                {
                    Field              = nameof(temp.Message),
                    Query              = keyword,
                    Operator           = Operator.Or,
                    MinimumShouldMatch = "100%"
                };
            }
            if (ValidateHelper.IsPlumpString(logger_name))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.LoggerName), Value = logger_name
                };
            }
            //查询条件
            sd = sd.Query(_ => query);
            //聚合
            sd = sd.Aggregations(x =>
                                 x.Terms(nameof(temp.LoggerName), av => av.Field(nameof(temp.LoggerName)).Size(1000))
                                 .Terms(nameof(temp.Level), av => av.Field(nameof(temp.Level)).Size(1000))
                                 .Terms(nameof(temp.Domain), av => av.Field(nameof(temp.Domain)).Size(1000)));
            //高亮
            if (highlight)
            {
                sd = sd.AddHighlightWrapper("<em class='kwd'>", "</em>", x => x.Field(nameof(temp.Message)));
            }
            //排序
            var sort = new SortDescriptor <ESLogLine>();

            sort = sort.Descending(x => x.UpdateTime);
            sort = sort.Descending(Field.Create("_score", boost: null));
            sd   = sd.Sort(_ => sort);

            //分页
            sd = sd.QueryPage_(page, pagesize);

            //请求服务器
            var client = new ElasticClient(ElasticsearchClientManager.Instance.DefaultClient);
            var re     = await client.SearchAsync <ESLogLine>(_ => sd);

            re.ThrowIfException();

            var data = new PagerData <ESLogLine, QueryExtData>();

            data.ItemCount = (int)re.Total;
            data.DataList  = re.Hits.Select(x => x.Source).ToList();
            //聚合数据
            data.ExtData           = new QueryExtData();
            data.ExtData.Ass       = re.GetAggs();
            data.ExtData.Highlight = re.GetHighlights();

            return(data);
        }
Ejemplo n.º 22
0
        public IActionResult Index(SearchQueryModel model)
        {
            //Search data
            //http://localhost:9200/person_full_details/_search
            //Search exact term (case-insensitive)

            /*var searchResponse = _elasticClient.Search<PersonFullDetails>(s => s
             *  .From(0)
             *  .Size(10)
             *  .Query(q => q
             *      .Match(m => m
             *          .Field(f => f.Firstname)
             *          .Query("gil*")
             *      )
             *  )
             * );*/
            //Search start with
            var searchResponse = _elasticClient.Search <PersonFullDetails>(s => s
                                                                           .From(0)
                                                                           .Size(10)
                                                                           .Query(q => q
                                                                                  .Prefix(m => m
                                                                                          .Field(f => f.Firstname)
                                                                                          .Value("gil")
                                                                                          )
                                                                                  )
                                                                           );
            var people = searchResponse.Documents;

            //Low-level search
            var searchResponseLowLevel = _elasticClient.LowLevel.Search <SearchResponse <PersonFullDetails> >(PostData.Serializable(new
            {
                from  = 0,
                size  = 10,
                query = new
                {
                    match = new
                    {
                        field = "firstname",
                        query = "gilles"
                    }
                }
            }));
            var responseJsonLowLevel = searchResponseLowLevel;

            //Aggregates
            //TODO: Order bu last/first
            var searchResponseAggOLD = _elasticClient.Search <PersonFullDetails>(s => s
                                                                                 .From((model.Page - 1) * model.PageSize)
                                                                                 .Size(model.PageSize)
                                                                                 //.Size(0)
                                                                                 .Query(q => q
                                                                                        .MatchAll()

                                                                                        /*.Match(m => m
                                                                                         *  .Field(f => f.Firstname)
                                                                                         *  .Query("gilles")
                                                                                         * )*/
                                                                                        )
                                                                                 .Aggregations(a => a
                                                                                               .Terms("last_names", ta => ta
                                                                                                      .Field(f => f.Lastname.Suffix("keyword"))
                                                                                                      )
                                                                                               .Terms("roles", ta => ta
                                                                                                      .Field(f => f.Roles.Suffix("keyword"))
                                                                                                      )
                                                                                               .Terms("company_names", ta => ta
                                                                                                      .Field(f => f.Company.Name.Suffix("keyword"))
                                                                                                      )
                                                                                               )
                                                                                 );

            var searchQuery = new SearchDescriptor <PersonFullDetails>()
                              .From((model.Page - 1) * model.PageSize)
                              .Size(model.PageSize);
            //Term

            /*searchQuery = searchQuery
             *  .Query(qu => qu
             *      .Bool(b => b
             *          //Term
             *          .Must(must => must
             *              .Match(m => m
             *                  .Field(f => f.Firstname)
             *                  .Query(model.Term)
             *                  )
             *              )
             *          //Filter
             *          .Filter(f =>
             *              f.Match(term => term//Term?
             *                  .Field(field => field.Lastname)
             *                  .Query("Dupont")//Value?
             *              )
             *          )
             *      )
             *  );*/

            //TODO: Clean next lines
            var lastnames = model.LastnameFilterValues ?? new string[] { };
            var companies = model.CompanyFilterValues ?? new string[] { };
            var roles     = model.RoleFilterValues ?? new string[] { };
            var filters   = new List <Func <QueryContainerDescriptor <PersonFullDetails>, QueryContainer> >();

            //TODO: Include 1+ filters
            if (lastnames.Any(i => i != null))
            {
                foreach (var lastname in lastnames)
                {
                    filters.Add(fq => fq.Match(t => t.Field(f => f.Lastname).Query(lastname)));
                }
            }
            if (companies.Any(i => i != null))
            {
                foreach (var company in companies)
                {
                    filters.Add(fq => fq.Match(t => t.Field(f => f.Company.Name).Query(company)));
                }
            }
            if (roles.Any(i => i != null))
            {
                foreach (var role in roles)
                {
                    filters.Add(fq => fq.Match(t => t.Field(f => f.Roles).Query(role)));
                }
            }

            Fields firstnameField = Infer.Field <PersonFullDetails>(p => p.Firstname);
            var    lastnameField  = Infer.Field <PersonFullDetails>(p => p.Lastname, 2);//Boost 2, more important
            var    bioField       = Infer.Field <PersonFullDetails>(p => p.Bio);

            searchQuery = searchQuery
                          .Query(qu => qu
                                 .Bool(b => b
                                       //Term
                                       .Must(must => must
                                       //.Match(m => m
                                             .MultiMatch(m => m
                                       //.Field(f => f.Firstname)
                                                         .Fields(firstnameField.And(lastnameField).And(bioField))
                                                         .Query(model.Term)
                                                         )
                                             )
                                       //Filter
                                       .Filter(filters)

                                       /*.Filter(f =>
                                        *  f.Match(term => term//Term?
                                        *      .Field(field => field.Lastname)
                                        *      .Query(model.NameFilterValue)//Value?
                                        *  )
                                        * )*/
                                       )
                                 ).Highlight(h => h
                                             .PreTags("<b>")
                                             .PostTags("</b>")
                                             .Fields(f => f
                                                     .Field(e => e.Bio)
                                                     .PreTags("<span class=\"highlighted\">")
                                                     .PostTags("</span>")
                                                     )
                                             );

            //Aggregates
            searchQuery = searchQuery
                          .Aggregations(a => a
                                        .Terms("last_names", ta => ta
                                               .Field(f => f.Lastname.Suffix("keyword"))
                                               )
                                        .Terms("roles", ta => ta
                                               .Field(f => f.Roles.Suffix("keyword"))
                                               )
                                        .Terms("company_names", ta => ta
                                               .Field(f => f.Company.Name.Suffix("keyword"))
                                               )
                                        );
            var searchResponseAgg = _elasticClient.Search <PersonFullDetails>(searchQuery);

            /*var search2 = new SearchDescriptor<PersonFullDetails>()
             * .Query(q => q
             *     .QueryString(queryString => queryString
             *         .Query("gilles")))
             * .Aggregations(a => a
             *     .Terms("last_names", term => term
             *         .Field(f => f.Lastname.Suffix("keyword"))));*/

            //var search21 = _elasticClient.Search<PersonFullDetails>(search2);

            var results = new List <SearchPersonModel>();
            var i       = 0;

            foreach (var hit in searchResponseAgg.Hits)
            {
                var document = hit.Source;
                foreach (var highlight in hit.Highlight)
                {
                    if (highlight.Key == "bio")
                    {
                        document.Bio = highlight.Value.FirstOrDefault();
                    }
                }
                results.Add(new SearchPersonModel
                {
                    Id        = document.Id,
                    Firstname = document.Firstname,
                    Lastname  = document.Lastname,
                    Age       = document.Age,
                    Bio       = document.Bio,
                    Roles     = document.Roles,
                    Company   = new SearchCompanyModel
                    {
                        Id   = document.Company.Id,
                        Name = document.Company.Name
                    }
                });
                i++;
            }

            var filterGroups = new List <SearchFilterGroup>();
            //Lastname
            var lastnameFilters = new List <SearchFilter>();

            foreach (var bucket in searchResponseAgg.Aggregations.Terms("last_names").Buckets)
            {
                lastnameFilters.Add(new SearchFilter
                {
                    Label = bucket.Key,
                    Count = bucket.DocCount ?? 0
                });
            }
            var lastnameFilterGroup = new SearchFilterGroup
            {
                Label   = "Lastname",
                Filters = lastnameFilters
            };

            filterGroups.Add(lastnameFilterGroup);
            //Role
            var roleFilters = new List <SearchFilter>();

            foreach (var bucket in searchResponseAgg.Aggregations.Terms("roles").Buckets)
            {
                roleFilters.Add(new SearchFilter
                {
                    Label = bucket.Key,
                    Count = bucket.DocCount ?? 0
                });
            }
            var roleFilterGroup = new SearchFilterGroup
            {
                Label   = "Role",
                Filters = roleFilters
            };

            filterGroups.Add(roleFilterGroup);
            //Company
            var companyFilters = new List <SearchFilter>();

            foreach (var bucket in searchResponseAgg.Aggregations.Terms("company_names").Buckets)
            {
                companyFilters.Add(new SearchFilter
                {
                    Label = bucket.Key,
                    Count = bucket.DocCount ?? 0
                });
            }
            var companyFilterGroup = new SearchFilterGroup
            {
                Label   = "Company",
                Filters = companyFilters
            };

            filterGroups.Add(companyFilterGroup);

            var totalPages = searchResponseAgg.Total / (double)model.PageSize;
            var data       = new SearchResultModel
            {
                SearchTerm           = model.Term,
                NbTotalResults       = searchResponseAgg.Total,
                Results              = results,
                FilterGroups         = filterGroups,
                PageSize             = model.PageSize,
                CurrentPage          = model.Page,
                TotalPages           = (int)Math.Ceiling(totalPages),
                LastnameFilterValues = model.LastnameFilterValues,
                CompanyFilterValues  = model.CompanyFilterValues,
                RoleFilterValues     = model.RoleFilterValues
            };

            return(View(data));
        }
Ejemplo n.º 23
0
        public async Task <SearchResponse <Specimen> > SearchAsync(FindParams <Data.Shared.Models.Specimen> findParams, Data.Shared.Models.User user)
        {
            var specimenFindParams = findParams as SpecimenFindParams;
            var searchTerm         = findParams.SearchText;
            var musts   = GetFilters(findParams);
            var shoulds = new List <QueryContainer>();
            var query   = new QueryContainerDescriptor <Specimen>();

            if (!string.IsNullOrEmpty(searchTerm))
            {
                var fields = new FieldsDescriptor <Specimen>();

                fields = fields.Field(f => f.Name)
                         .Field(f => f.Lifeform.CommonName)
                         .Field(f => f.Lifeform.ScientificName);

                if (findParams.UseNGrams)
                {
                    fields = fields.Field("name.nameSearch");
                }

                shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields)
                                             .Query(searchTerm)
                                             .Fuzziness(Fuzziness.AutoLength(1, 5))));
            }

            musts.Add(FilterByVisibility(query, user));

            var searchDescriptor = new SearchDescriptor <Specimen>()
                                   .Query(q => q
                                          .Bool(b => b
                                                .Should(shoulds.ToArray())
                                                .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var countDescriptor = new CountDescriptor <Specimen>()
                                  .Query(q => q
                                         .Bool(b => b
                                               .Should(shoulds.ToArray())
                                               .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            // Sort
            var specimenSorts = GetSpecimenSorts();

            if (!string.IsNullOrEmpty(findParams.SortBy))
            {
                if (findParams.SortDirection == SortDirection.Ascending)
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Ascending()));
                }
                else
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Descending()));
                }
            }
            else if (string.IsNullOrEmpty(findParams.SearchText))
            {
                searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts["DateModified"]).Descending()).Field(f => f.Field(specimenSorts["DateCreated"]).Descending()));
            }

            var aggregations  = new AggregationContainerDescriptor <Specimen>();
            var searchFilters = new List <SearchFilter <Specimen> >
            {
                new SearchValueFilter <Specimen, string>("Stage", "specimenStage", specimenFindParams.Filters.StageFilter.Value)
            };

            foreach (var filter in searchFilters)
            {
                if (filter is NestedSearchValueFilter <Specimen, string> nestedFilter)
                {
                    aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchRangeFilter <Specimen, double> searchRangeFilter)
                {
                    aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValueFilter <Specimen, string> searchValueFilter)
                {
                    aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations);
                }
            }

            searchDescriptor.Aggregations(a => aggregations);

            var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor);

            response.AggregationResult = ProcessAggregations(response, specimenFindParams);

            return(response);
        }
Ejemplo n.º 24
0
        public async Task <SearchResponse <PlantInfo> > SearchAsync(FindParams <Data.Shared.Models.PlantInfo> findParams, Data.Shared.Models.User user)
        {
            var plantInfoFindParams = findParams as PlantInfoFindParams;
            var searchTerm          = findParams.SearchText;
            var shoulds             = new List <QueryContainer>();
            var query = new QueryContainerDescriptor <PlantInfo>();

            if (!string.IsNullOrEmpty(searchTerm))
            {
                var fields = new FieldsDescriptor <PlantInfo>();

                fields = fields.Field(m => m.CommonName)
                         .Field(m => m.ScientificName)
                         .Field(m => m.Lifeform.CommonName)
                         .Field(m => m.Lifeform.ScientificName)
                         .Field("commonName.nameSearch")
                         .Field("scientificName.nameSearch")
                         .Field("lifeform.commonName.nameSearch")
                         .Field("lifeform.scientificName.nameSearch");

                shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields)
                                             .Query(searchTerm)
                                             .Fuzziness(Fuzziness.AutoLength(3, 5))));
                shoulds.Add(query.Nested(n => n
                                         .Path(p => p.Synonyms)
                                         .Query(q => q
                                                .Match(sq => sq
                                                       .Field("synonyms.name")
                                                       .Query(searchTerm)
                                                       .Fuzziness(Fuzziness.AutoLength(3, 5))))));
            }

            var filters       = plantInfoFindParams.Filters;
            var searchFilters = new List <SearchFilter <PlantInfo> >
            {
                new NestedSearchValueFilter <PlantInfo, string>(filters.RegionFilter.Name, "location.region.keyword", "plantLocations", filters.RegionFilter.Value),
                new SearchValuesFilter <PlantInfo, string>(filters.WaterFilter.Name, "waterTypes", filters.WaterFilter.MinimumValue, filters.WaterFilter.MaximumValue),
                new SearchValuesFilter <PlantInfo, string>(filters.LightFilter.Name, "lightTypes", filters.LightFilter.MinimumValue, filters.LightFilter.MaximumValue),
                new SearchValuesFilter <PlantInfo, string>(filters.BloomFilter.Name, "bloomTimes", filters.BloomFilter.MinimumValue?.ToString(), filters.BloomFilter.MaximumValue?.ToString()),
                new NestedSearchValueFilter <PlantInfo, string>(filters.ZoneFilter.Name, "id", "zones", filters.ZoneFilter.Value?.ToString()),
                new SearchRangeFilter <PlantInfo, double>(filters.HeightFilter.Name, "minHeight", "maxHeight", filters.HeightFilter.Values, filters.HeightFilter.Value, filters.HeightFilter.MaximumValue),
                new SearchRangeFilter <PlantInfo, double>(filters.SpreadFilter.Name, "minSpread", "maxSpread", filters.SpreadFilter.Values, filters.SpreadFilter.Value, filters.SpreadFilter.MaximumValue),
                new NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus>(filters.NativeFilter.Name, "location.stateOrProvince.keyword", "plantLocations", "status",
                                                                                     filters.NativeFilter.Value, filters.NativeFilter.Status)
            };

            var musts = GetFilters(plantInfoFindParams, searchFilters);

            musts.Add(FilterByVisibility(query, user));

            if (plantInfoFindParams.Lifeform != null)
            {
                musts.Add(query.Bool(b => b.Must(m => m.Term(t => t.Lifeform.Id, plantInfoFindParams.Lifeform.LifeformId))));
            }

            var searchDescriptor = new SearchDescriptor <PlantInfo>()
                                   .Query(q => q
                                          .Bool(b => b
                                                .Should(shoulds.ToArray())
                                                .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var countDescriptor = new CountDescriptor <PlantInfo>()
                                  .Query(q => q
                                         .Bool(b => b
                                               .Should(shoulds.ToArray())
                                               .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1)));

            var aggregations = new AggregationContainerDescriptor <PlantInfo>();

            foreach (var filter in searchFilters)
            {
                if (filter is NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus> nestedMultiFilter)
                {
                    aggregations = nestedMultiFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is NestedSearchValueFilter <PlantInfo, string> nestedFilter)
                {
                    aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchRangeFilter <PlantInfo, double> searchRangeFilter)
                {
                    aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValuesFilter <PlantInfo, string> searchValuesFilter)
                {
                    aggregations = searchValuesFilter.ToAggregationContainerDescriptor(aggregations);
                }
                else if (filter is SearchValueFilter <PlantInfo, string> searchValueFilter)
                {
                    aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations);
                }
            }

            searchDescriptor.Aggregations(a => aggregations);

            // Sort
            var plantInfoSorts = GetPlantInfoSorts();

            if (!string.IsNullOrEmpty(findParams.SortBy))
            {
                if (findParams.SortDirection == SortDirection.Ascending)
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Ascending()));
                }
                else
                {
                    searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Descending()));
                }
            }
            else if (string.IsNullOrEmpty(findParams.SearchText))
            {
                searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts["DateModified"]).Descending()).Field(f => f.Field(plantInfoSorts["DateCreated"]).Descending()));
            }

            var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor);

            response.AggregationResult = ProcessAggregations(response, plantInfoFindParams);

            return(response);
        }
Ejemplo n.º 25
0
        public static FamilyNamePack FamilyNameSearch(FamilyNameSearchParam fi)
        {
            if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 6)
            {
                fi.ESIndex = "familyname_v1";
            }
            else
            {
                fi.ESIndex = "familyname_v0";
            }

            var res = new FamilyNamePack();
            var s   = new SearchDescriptor <familyname>().Index(fi.ESIndex).Type(fi.ESType).Size(0);

            if (!string.IsNullOrWhiteSpace(fi.areacode))
            {
                s.Query(q => q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode)))));
            }
            var fns = new List <string>();
            ISearchResponse <familyname> r;
            bool fn_flag = false;

            if (string.IsNullOrWhiteSpace(fi.Familyname))
            {
                s.Aggregations(agg => GetAgg_fn_FamilyName(agg, fi));
                r = ESClientInst.Client.Search <familyname>(s);
                var count = r.Aggs.Terms("fn").Buckets.Count;
                res.totalpage = (count % fi.pg_size == 0) ? count / fi.pg_size : count / fi.pg_size + 1;

                if (r.Aggregations.ContainsKey("carea"))
                {
                    var areas = r.Aggs.Terms("carea");
                    res.Area2Num = areas.Buckets.ToDictionary(b => b.Key, b => (int)b.DocCount);
                }
                foreach (var aa in r.Aggs.Terms("fn").Buckets.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size))
                {
                    fns.Add(aa.Key);
                    var fnl = new FamilyName_List()
                    {
                        Familyname = aa.Key, total = (int)(aa.DocCount)
                    };
                    fnl.list = new List <FN_Name_List>();
                    res.list.Add(fnl);
                }
                //fns.AddRange(r.Aggs.Terms("fn").Buckets.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size).Select(b => b.Key));
                s = new SearchDescriptor <familyname>().Index(fi.ESIndex).Type(fi.ESType).Size(0);
                //if (!string.IsNullOrWhiteSpace(fi.areacode))
                //{
                //    s.Query(q => q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode)))));
                //}
            }
            else
            {
                fn_flag = true;
                fns.Add(fi.Familyname);
                res.list.Add(new FamilyName_List()
                {
                    Familyname = fi.Familyname, list = new List <FN_Name_List>()
                });
            }
            s.Query(q =>
            {
                QueryContainer qc = new QueryContainer();
                if (!string.IsNullOrWhiteSpace(fi.areacode))
                {
                    qc &= q.Bool(b => b.Filter(f => f.Term(t => t.Field(fi.areacode.Length == 2 ? "cmarea" : "carea").Value(fi.areacode))));
                }
                if (fns.Count > 0)
                {
                    qc &= q.Terms(t => t.Field("fn_" + fi.ptype.ToString()).Terms(fns));
                }
                return(qc);
            });
            s.Aggregations(agg => GetAgg_name_FamilyName(agg, fi, fns));
            r = ESClientInst.Client.Search <familyname>(s);
            if (r.Aggregations.ContainsKey("carea"))
            {
                var areas = r.Aggregations.Terms("carea");
                res.Area2Num = areas.Buckets.ToDictionary(b => b.Key, b => (int)b.DocCount);
            }
            var names          = new List <string>(30);
            var fnr            = r.Aggregations.Filters("fn");
            var rank_head_flag = fi.pg_index == 0 && !fn_flag;

            for (var i = 0; i < res.list.Count; ++i)
            {
                var l = res.list[i];
                if (fnr.ContainsKey(l.Familyname))
                {
                    var agg   = (Nest.SingleBucketAggregate)fnr.NamedBucket(l.Familyname);
                    var buck  = (BucketAggregate)agg.Aggregations["name"];
                    var items = fn_flag ? buck.Items.Skip(fi.pg_index * fi.pg_size).Take(fi.pg_size) : buck.Items;
                    if (i < fi.rank_head_num && rank_head_flag || fn_flag)
                    {
                        foreach (KeyedBucket <object> item in items)
                        {
                            var nm      = (string)item.Key;
                            var fn_name = new FN_Name_List()
                            {
                                name = nm, total = (int)(item.DocCount)
                            };
                            l.list.Add(fn_name);
                            names.Add(nm);
                        }
                    }
                    else
                    {
                        var item = items.FirstOrDefault() as KeyedBucket <object>;
                        if (item != null)
                        {
                            var nm      = (string)item.Key;
                            var fn_name = new FN_Name_List()
                            {
                                name = nm, total = (int)(item.DocCount)
                            };
                            l.list.Add(fn_name);
                            names.Add(nm);
                        }
                    }

                    if (fn_flag)
                    {
                        l.total = buck.Items.Sum(it => (int)((KeyedBucket <object>)it).DocCount);
                    }
                }
            }

            var mr = ESClientInst.Client.MultiSearch(ms => GetMultiSearch_FamilyName(ms, names, fi));

            foreach (var p in res.list)
            {
                foreach (var k in p.list)
                {
                    var rr = mr.GetResponse <familyname>(k.name);
                    k.list = rr.Documents.Select(d => new ComMini()
                    {
                        code = d.ccode, name = d.cname, area = string.IsNullOrWhiteSpace(d.carea) ? d.cmarea : d.carea
                    }).ToList();
                }
            }
            return(res);
        }
Ejemplo n.º 26
0
        public PagerData <CommentEs> QueryCommentFromEs(
            string user_product_id = null, string user_uid = null, string q = null, int page = 1, int pagesize = 10)
        {
            var data     = new PagerData <CommentEs>();
            var client   = ElasticsearchClientManager.Instance.DefaultClient.CreateClient();
            var temp     = new CommentEs();
            var tag_temp = new TagEs();

            var sd = new SearchDescriptor <CommentEs>();

            sd = sd.Index(INDEX_NAME);

            #region where
            var query = new QueryContainer();
            if (ValidateHelper.IsPlumpString(user_product_id))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.UserProductUID), Value = user_product_id
                };
            }
            if (ValidateHelper.IsPlumpString(user_uid))
            {
                query &= new TermQuery()
                {
                    Field = nameof(temp.UserUID), Value = user_uid
                };
            }
            if (ValidateHelper.IsPlumpString(q))
            {
                query &= new MatchQuery()
                {
                    Field = nameof(temp.Comment), Query = q, Operator = Operator.Or, MinimumShouldMatch = "100%"
                };
            }
            sd = sd.Query(_ => query);
            #endregion

            #region order
            var sort = new SortDescriptor <CommentEs>();
            sort = sort.Descending(x => x.CreateTime);
            sd   = sd.Sort(_ => sort);
            #endregion

            #region aggs
            sd = sd.Aggregations(x => x
                                 .Terms("tags", av => av.Field($"{nameof(temp.Tags)}.{nameof(tag_temp.TagName)}").Size(10))
                                 .Terms("shops", av => av.Field(f => f.TraderUID).Size(10))
                                 .Average("score", av => av.Field(f => f.Score)));
            #endregion

            #region pager
            sd = sd.QueryPage_(page, pagesize);
            #endregion

            var response = client.Search <CommentEs>(_ => sd);
            response.ThrowIfException();

            data.ItemCount = (int)response.Total;
            data.DataList  = response.Documents.ToList();

            var tags_agg  = response.Aggs.Terms("tags");
            var shops_agg = response.Aggs.Terms("shops");
            var score_agg = response.Aggs.Average("score");

            return(data);
        }
Ejemplo n.º 27
0
        public async Task <SearchResult> FindByFilter(SearchFilter filterOpt)
        {
            var result = new SearchResult();

            int page  = filterOpt.paging == null ? 0 : filterOpt.paging.page;
            int limit = Math.Min(100, filterOpt.paging == null ? 10 : filterOpt.paging.limit);

            //Paging
            var searchDes = new SearchDescriptor <SearchGoods>()
                            .From(page)
                            .Size(limit);

            //Sort
            var sortDescriptor = new SortDescriptor <SearchGoods> ();

            if (!String.IsNullOrWhiteSpace(filterOpt.sort?.price))
            {
                if (filterOpt.sort.price == "desc")
                {
                    sortDescriptor.Field(f => f.price, Nest.SortOrder.Descending);
                }
                else if (filterOpt.sort.price == "asc")
                {
                    sortDescriptor.Field(f => f.price, Nest.SortOrder.Ascending);
                }
            }

            if (!String.IsNullOrWhiteSpace(filterOpt.sort?.saleCnt))
            {
                if (filterOpt.sort.saleCnt == "desc")
                {
                    sortDescriptor.Field(f => f.saleCnt, Nest.SortOrder.Descending);
                }
                else if (filterOpt.sort.saleCnt == "asc")
                {
                    sortDescriptor.Field(f => f.saleCnt, Nest.SortOrder.Ascending);
                }
            }

            if (!String.IsNullOrWhiteSpace(filterOpt.sort?.viewCnt))
            {
                if (filterOpt.sort.viewCnt == "desc")
                {
                    sortDescriptor.Field(f => f.viewCnt, Nest.SortOrder.Descending);
                }
                else if (filterOpt.sort.viewCnt == "asc")
                {
                    sortDescriptor.Field(f => f.viewCnt, Nest.SortOrder.Ascending);
                }
            }

            searchDes = searchDes.Sort(s => sortDescriptor);

            //Filter
            var filters = new List <Func <QueryContainerDescriptor <SearchGoods>, QueryContainer> >();

            //Category Filter
            if (!String.IsNullOrWhiteSpace(filterOpt.filters?.category1))
            {
                filters.Add(fq => fq.Match(t => t.Field(f => f.category1).Query(filterOpt.filters.category1)));
            }

            if (!String.IsNullOrWhiteSpace(filterOpt.filters?.category2))
            {
                filters.Add(fq => fq.Match(t => t.Field(f => f.category2).Query(filterOpt.filters.category2)));
            }

            if (!String.IsNullOrWhiteSpace(filterOpt.filters?.category3))
            {
                filters.Add(fq => fq.Match(t => t.Field(f => f.category3).Query(filterOpt.filters.category3)));
            }

            //Tag Filter
            if (!String.IsNullOrWhiteSpace(filterOpt.filters?.tag))
            {
                filters.Add(fq => fq.Match(t => t.Field(f => f.tags).Query(filterOpt.filters.tag)));
            }

            //Query
            if (!String.IsNullOrWhiteSpace(filterOpt.keyword))
            {
                var keywords = filterOpt.keyword.Split(' ');
                filters.Add(fq => fq.Terms(t => t.Field(f => f.terms).Terms(keywords)));
            }

            //Price
            if (filterOpt.filters?.minPrice > 0 && filterOpt.filters?.maxPrice > 0)
            {
                filters.Add(fq => fq.Range(c => c
                                           .Name("named_query")
                                           .Boost(1.1)
                                           .Field(p => p.price)
                                           .GreaterThanOrEquals(filterOpt.filters.minPrice)
                                           .LessThanOrEquals(filterOpt.filters.maxPrice)
                                           .Relation(RangeRelation.Within)
                                           ));
            }

            searchDes = searchDes.Query(q => q
                                        .Bool(bq => bq.Filter(filters)));

            //Aggregations(검색 결과 집계 처리)
            searchDes.Aggregations(aggs => aggs
                                   .Average("average_per_price", avg => avg.Field(p => p.price))
                                   .Max("max_per_price", avg => avg.Field(p => p.price))
                                   .Min("min_per_price", avg => avg.Field(p => p.price))
                                   .Terms("category1_cnt", st => st
                                          .Field(p => p.category1.Suffix("keyword"))
                                          .MinimumDocumentCount(1)
                                          .Size(100000)
                                          .ShardSize(100)
                                          .ExecutionHint(TermsAggregationExecutionHint.Map)
                                          .Missing("na")
                                          .Order(o => o
                                                 .KeyAscending()
                                                 .CountDescending()
                                                 )
                                          .Meta(m => m
                                                .Add("foo", "bar")
                                                )
                                          )
                                   .Terms("category2_cnt", st => st
                                          .Field(p => p.category2.Suffix("keyword"))
                                          .MinimumDocumentCount(1)
                                          .Size(100000)
                                          .ShardSize(100)
                                          .ExecutionHint(TermsAggregationExecutionHint.Map)
                                          .Missing("na")
                                          .Order(o => o
                                                 .KeyAscending()
                                                 .CountDescending()
                                                 )
                                          .Meta(m => m
                                                .Add("foo", "bar")
                                                )
                                          )
                                   .Terms("tag_cnt", st => st
                                          .Field(p => p.tags.Suffix("keyword"))
                                          .MinimumDocumentCount(1)
                                          .Size(100000)
                                          .ShardSize(100)
                                          .ExecutionHint(TermsAggregationExecutionHint.Map)
                                          .Missing("na")
                                          .Order(o => o
                                                 .KeyAscending()
                                                 .CountDescending()
                                                 )
                                          .Meta(m => m
                                                .Add("foo", "bar")
                                                )
                                          )
                                   );

            var engine_result = await _elasticClient.SearchAsync <SearchGoods>(searchDes);

            result.list  = engine_result.Documents.ToList();
            result.total = (int)engine_result.Total;
            result.size  = result.list.Count;

            var agg            = engine_result.Aggregations;
            var category1_aggs = agg.Terms("category1_cnt");
            var category2_aggs = agg.Terms("category2_cnt");
            var tag_aggs       = agg.Terms("tag_cnt");

            // 검색내 재 검색을 위한 summary
            result.summary = new Summary
            {
                filterValues = new List <FilterValue>()
                {
                    new FilterValue()
                    {
                        fieldName = "average_per_price", name = "", value = agg.ValueCount("average_per_price").Value
                    },
                    new FilterValue()
                    {
                        fieldName = "max_per_price", name = "", value = agg.ValueCount("max_per_price").Value
                    },
                    new FilterValue()
                    {
                        fieldName = "min_per_price", name = "", value = agg.ValueCount("min_per_price").Value
                    }
                }
            };

            foreach (var category1_item in category1_aggs.Buckets)
            {
                result.summary.filterValues.Add(new FilterValue()
                {
                    fieldName = "category1", name = category1_item.Key, value = category1_item.DocCount
                });
            }

            foreach (var category2_item in category2_aggs.Buckets)
            {
                result.summary.filterValues.Add(new FilterValue()
                {
                    fieldName = "category2", name = category2_item.Key, value = category2_item.DocCount
                });
            }

            result.summary.tags = new List <string>();

            foreach (var tag_item in tag_aggs.Buckets)
            {
                result.summary.tags.Add(tag_item.Key);
            }

            return(result);
        }