Beispiel #1
0
        /// <summary>
        /// 热门词汇列表({“分组值”,“当前文档的数量”})
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="typeName"></param>
        /// <param name="groupBy"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public Dictionary <string, long> Group(string indexName, string typeName, string groupBy, int size = 10)
        {
            var result = _es.Search <T>(s => s.Index(indexName).Type(typeName)
                                        .Aggregations(ag => ag
                                                      .Terms($"group_by_{groupBy}", t => t.Field(groupBy).Size(size))//分组
                                                      )
                                        );
            SearchRequest request = new SearchRequest();
            var           hotDic  = new Dictionary <string, long>();
            IAggregate    aggr;

            result.Aggregations.TryGetValue($"group_by_{groupBy}", out aggr);
            if (aggr != null)
            {
                BucketAggregate bucket = aggr as BucketAggregate;
                if (bucket != null && bucket.Items.Count > 0)
                {
                    foreach (KeyedBucket <object> item in bucket.Items)
                    {
                        hotDic.Add(item.Key.ToString(), item.DocCount ?? 0);
                    }
                }
            }
            return(hotDic);
        }
Beispiel #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var item      = JObject.Load(reader);
            var typeToken = item.SelectToken("Data.@type") ?? item.SelectToken("data.@type");

            IAggregate value = null;

            if (typeToken != null)
            {
                string type = typeToken.Value <string>();
                switch (type)
                {
                case "bucket":
                    value = new BucketAggregate();
                    break;

                case "exstats":
                    value = new ExtendedStatsAggregate();
                    break;

                case "ovalue":
                    value = new ObjectValueAggregate();
                    break;

                case "percentiles":
                    value = new PercentilesAggregate();
                    break;

                case "sbucket":
                    value = new SingleBucketAggregate();
                    break;

                case "stats":
                    value = new StatsAggregate();
                    break;

                case "tophits":
                    // TODO: Have to get all the docs as JToken and
                    //value = new TopHitsAggregate();
                    break;

                case "value":
                    value = new ValueAggregate();
                    break;

                case "dvalue":
                    value = new ValueAggregate <DateTime>();
                    break;
                }
            }

            if (value == null)
            {
                value = new ValueAggregate();
            }

            serializer.Populate(item.CreateReader(), value);

            return(value);
        }
        public void BuildOptions(BucketAggregate bucket)
        {
            Options = new List <StringFilterOption>();
            foreach (KeyedBucket item in bucket.Items.Where(x => ((KeyedBucket)x).DocCount > 0))
            {
                var option = new StringFilterOption {
                    Value       = item.Key,
                    EntityCount = item.DocCount.Value
                };

                Options.Add(option);
            }
        }
Beispiel #4
0
        private static JObject CreateBucket(BucketAggregate bucketAggregate)
        {
            var bucketLevel = new JObject
            {
                { "docCount", bucketAggregate.DocCount },
                { "sumOtherDocCount", bucketAggregate.SumOtherDocCount },
                { "docCountErrorUpperBound", bucketAggregate.DocCountErrorUpperBound }
            };

            var items = CreateBucketItems(bucketAggregate);

            bucketLevel.Add("items", items);

            return(bucketLevel);
        }
Beispiel #5
0
        private static JArray CreateBucketItems(BucketAggregate bucketAggregate)
        {
            var array = new JArray();

            foreach (var bucket1 in bucketAggregate.Items)
            {
                var keyedBucket = (KeyedBucket <object>)bucket1;
                var bucket      = new JObject
                {
                    { "docCount", keyedBucket.DocCount },
                    { "docCountErrorUpperBound", keyedBucket.DocCountErrorUpperBound },
                    { "keyAsString", keyedBucket.KeyAsString },
                    { "key", keyedBucket.Key.ToString() }
                };
                array.Add(bucket);
            }

            return(array);
        }
Beispiel #6
0
        private IEnumerable <ISearchSummaryDocumentDTO> GetSearchTypeStatisticsDocumentDTO(ISearchResponse <dynamic> response)
        {
            IEnumerable <ISearchSummaryDocumentDTO> lstSearchTypeStatistics = null;

            if (response != null && response.Hits != null)
            {
                if (response.Aggregations != null && response.Aggregations.Any())
                {
                    if (response.Aggregations.ContainsKey("type_count"))
                    {
                        BucketAggregate aggsItems = (BucketAggregate)response.Aggregations["type_count"];

                        if (aggsItems != null)
                        {
                            lstSearchTypeStatistics = aggsItems.Items.Select(b => GetSearchTypeStatistics((KeyedBucket)b)).ToList();
                        }
                    }
                }
            }

            return(lstSearchTypeStatistics);
        }
        public void BuildOptions(BucketAggregate bucket)
        {
            Options = new List <CurrencyRangeFilterOption>();
            foreach (KeyedBucket item in bucket.Items.Where(x => ((KeyedBucket)x).DocCount > 0))
            {
                var option = new CurrencyRangeFilterOption {
                    Start       = double.Parse(item.Key),
                    EntityCount = item.DocCount.Value
                };

                Options.Add(option);
            }

            if (Options.Count > 0)
            {
                for (var i = 0; i < Options.Count - 1; i++)
                {
                    Options.Skip(i).First().End = Options.Skip(i + 1).First().Start - 1;
                }
                Options.First().Start = double.MinValue;
                Options.Last().End    = double.MaxValue;
            }
        }
Beispiel #8
0
        public SearchResult Search(SearchParameters searchParameters)
        {
            int            size     = 10000;
            List <Dataset> datasets = new List <Dataset>();
            string         text     = searchParameters.text;

            string[] coverageTypes = searchParameters.coveragetypes;
            string[] areas         = searchParameters.areas;
            string[] projections   = searchParameters.projections;
            string[] formats       = searchParameters.formats;

            var filters = new List <Func <QueryContainerDescriptor <Dataset>, QueryContainer> >();

            if (coverageTypes != null && coverageTypes.Length > 0)
            {
                filters.Add(nq => nq.Terms(m0 => m0.Field("file.coverageType").Terms(coverageTypes)));
            }

            if (areas != null && areas.Length > 0)
            {
                filters.Add(nq => nq.Terms(m1 => m1.Field("file.area").Terms(areas)));
            }

            if (projections != null && projections.Length > 0)
            {
                filters.Add(nq => nq.Terms(m2 => m2.Field("file.projection").Terms(projections)));
            }

            if (formats != null && formats.Length > 0)
            {
                filters.Add(nq => nq.Terms(m3 => m3.Field("file.format").Terms(formats)));
            }

            if (!string.IsNullOrEmpty(text))
            {
                text = "*" + text + "*";
            }

            var searchResponse = _client.Search <Dataset>(s => s
                                                          .Size(size)
                                                          .Query(q => q
                                                                 .Wildcard(m => m
                                                                           .Field(t => t.Title)
                                                                           .Value(text)
                                                                           ) &&
                                                                 q.Nested(n => n
                                                                          .InnerHits(ih => ih.From(0).Size(size))
                                                                          .Path(b => b.Files)
                                                                          .Query(nq => nq.Bool(bq => bq.Filter(filters))

                                                                                 )
                                                                          )
                                                                 )

                                                          .Aggregations(a => a
                                                                        .Nested("facets", n => n
                                                                                .Path(p => p.Files)
                                                                                .Aggregations(a => a
                                                                                              .Terms("coverageType", t => t.Field("file.coverageType").Size(size))
                                                                                              .Terms("area", t => t.Field("file.area").Size(size))
                                                                                              .Terms("projection", t => t.Field("file.projection").Size(size))
                                                                                              .Terms("format", t => t.Field("file.format").Size(size))
                                                                                              )
                                                                                )
                                                                        )
                                                          );

            List <Facet> facetResult = new List <Facet>();

            if (areas == null && coverageTypes == null && projections == null && formats == null)
            {
                SingleBucketAggregate facets = (SingleBucketAggregate)searchResponse.Aggregations["facets"];


                for (int f = 0; f < facets.Keys.Count(); f++)
                {
                    string key = facets.Keys.ElementAt(f);


                    Facet facet = new Facet(key);

                    BucketAggregate bucketAggregate = facets.Values.ElementAt(f) as BucketAggregate;
                    if (bucketAggregate != null)
                    {
                        var items = bucketAggregate.Items;
                        foreach (KeyedBucket <object> bucked in items)
                        {
                            var facetCount = (int)bucked.DocCount;
                            var facetValue = bucked.Key.ToString();

                            facet.FacetResults.Add(new Facet.FacetValue(facetValue, facetCount));
                        }

                        facetResult.Add(facet);
                    }
                }
            }

            SearchResult searchResult = new SearchResult();

            foreach (var hit in searchResponse.Hits)
            {
                Dataset dataset = new Dataset();
                dataset.Files        = new List <File>();
                dataset.MetadataUuid = hit.Source.MetadataUuid;
                dataset.Title        = hit.Source.Title;

                if (hit.InnerHits.Count > 0)
                {
                    if (facetResult.Count == 0)
                    {
                        facetResult.Add(new Facet("area"));
                        facetResult.Add(new Facet("coverageType"));
                        facetResult.Add(new Facet("format"));
                        facetResult.Add(new Facet("projection"));
                    }

                    var innerhits = hit.InnerHits["file"].Documents <File>();

                    foreach (var innerhit in innerhits)
                    {
                        var coverageType  = innerhit.CoverageType;
                        var facetCoverage = facetResult[1].FacetResults.Where(f => f.Name == coverageType).FirstOrDefault();
                        if (facetCoverage == null)
                        {
                            facetResult[1].FacetResults.Add(new Facet.FacetValue {
                                Name = coverageType, Count = 1
                            });
                        }
                        else
                        {
                            facetResult[1].FacetResults.Where(p => p.Name == coverageType).Select(u => { u.Count = u.Count + 1; return(u); }).ToList();
                        }


                        var area      = innerhit.Area;
                        var facetArea = facetResult[0].FacetResults.Where(f => f.Name == area).FirstOrDefault();
                        if (facetArea == null)
                        {
                            facetResult[0].FacetResults.Add(new Facet.FacetValue {
                                Name = area, Count = 1
                            });
                        }
                        else
                        {
                            facetResult[0].FacetResults.Where(p => p.Name == area).Select(u => { u.Count = u.Count + 1; return(u); }).ToList();
                        }

                        var format      = innerhit.Format;
                        var facetFormat = facetResult[2].FacetResults.Where(f => f.Name == format).FirstOrDefault();
                        if (facetFormat == null)
                        {
                            facetResult[2].FacetResults.Add(new Facet.FacetValue {
                                Name = format, Count = 1
                            });
                        }
                        else
                        {
                            facetResult[2].FacetResults.Where(p => p.Name == format).Select(u => { u.Count = u.Count + 1; return(u); }).ToList();
                        }

                        var projection      = innerhit.Projection;
                        var facetProjection = facetResult[3].FacetResults.Where(f => f.Name == projection).FirstOrDefault();
                        if (facetProjection == null)
                        {
                            facetResult[3].FacetResults.Add(new Facet.FacetValue {
                                Name = projection, Count = 1
                            });
                        }
                        else
                        {
                            facetResult[3].FacetResults.Where(p => p.Name == projection).Select(u => { u.Count = u.Count + 1; return(u); }).ToList();
                        }
                    }

                    dataset.Files.AddRange(hit.InnerHits["file"].Documents <File>());
                }
                else
                {
                    dataset.Files.AddRange(hit.Source.Files);
                }

                datasets.Add(dataset);
            }

            searchResult.Datasets = datasets;
            searchResult.Facets   = facetResult;

            return(searchResult);
        }
		private IAggregate GetMultiBucketAggregate(JsonReader reader, JsonSerializer serializer)
		{
			var bucket = new BucketAggregate();
			var propertyName = (string)reader.Value;
			if (propertyName == "doc_count_error_upper_bound")
			{
				reader.Read();
				bucket.DocCountErrorUpperBound = reader.Value as long?;
				reader.Read();
			}
			propertyName = (string)reader.Value;
			if (propertyName == "sum_other_doc_count")
			{
				reader.Read();
				bucket.SumOtherDocCount = reader.Value as long?;
				reader.Read();
			}
			var items = new List<IBucket>();
			reader.Read();

			if (reader.TokenType == JsonToken.StartObject)
			{
				reader.Read();
				var aggs = new Dictionary<string, IAggregate>();
				do
				{
					var name = reader.Value.ToString();
					reader.Read();
					var innerAgg = this.ReadAggregate(reader, serializer);
					aggs.Add(name, innerAgg);
					reader.Read();
				} while (reader.TokenType != JsonToken.EndObject);

				reader.Read();
				return new FiltersAggregate(aggs);
			}

			if (reader.TokenType != JsonToken.StartArray)
				return null;
			reader.Read(); //move from start array to start object
			if (reader.TokenType == JsonToken.EndArray)
			{
				reader.Read();
				bucket.Items = EmptyReadOnly<IBucket>.Collection;
				return bucket;
			}
			do
			{
				var item = this.ReadBucket(reader, serializer);
				items.Add(item);
				reader.Read();
			} while (reader.TokenType != JsonToken.EndArray);
			bucket.Items = items;
			reader.Read();
			return bucket;
		}