private static List <DocumentUniqueValue> FlattenCountAndSum(ElasticsearchTermCount termcount, string divider, string parentKey) { var list = new List <DocumentUniqueValue>(); foreach (var bucket in termcount.buckets) { var key = string.Format("{0}{1}", parentKey ?? string.Empty, bucket.key); if (bucket.term_count == null || bucket.term_count.buckets.Count == 0 || bucket.term_count.buckets[0].term_count == null) { //If 'depth' (number of terms) = 1 (i.e. when bucket.term_count == null) then the unique count is set to 1 list.Add(new DocumentUniqueValue { Key = key, Count = bucket.term_count == null ? 1 : bucket.term_count.buckets.Count, DocumentCount = bucket.doc_count }); } else { foreach (var innerItem in FlattenCountAndSum(bucket.term_count, divider, string.Format("{0}{1}{2}", parentKey ?? string.Empty, bucket.key, divider))) { list.Add(innerItem); } } } return(list); }
///// <summary> ///// Get number of items that matches filter. ///// </summary> ///// <param name="type">Type name.</param> ///// <param name="filter">Filter for object of specified type.</param> ///// <returns>Number of items that matches filter.</returns> //protected DocumentCountResponse GetCount(String type, // String filter) //{ // DocumentCountResponse documentCountResponse; // ElasticsearchResponse<DynamicDictionary> response; // String shardInformation; // String[] splitShardInformation; // response = mClient.Count(IndexName, type, filter); // CheckResponse(response); // documentCountResponse = new DocumentCountResponse(); // if (response.Response.IsNotNull()) // { // documentCountResponse.DocumentCount = (Int32)(response.Response.Values.ElementAt(0)); // // Get shard information. // shardInformation = (String)(response.Response.Values.ElementAt(1)); // splitShardInformation = shardInformation.Split(':'); // documentCountResponse.ShardTotalCount = splitShardInformation[1].Substring(0, splitShardInformation[1].IndexOf(',')).WebParseInt32(); // documentCountResponse.ShardSuccessfulCount = splitShardInformation[2].Substring(0, splitShardInformation[2].IndexOf(',')).WebParseInt32(); // documentCountResponse.ShardFailedCount = splitShardInformation[3].Substring(0, splitShardInformation[3].IndexOf('}')).WebParseInt32(); // } // return documentCountResponse; //} /// <summary> /// Returns a dictionary with a 'flattened' key from a termcount tree structure, the value is the last bucket node's doc_count (works in a recursive fashion) /// All keys are concatenated with the divider (like "key1:key2:key3" given that the divider is a colon) /// </summary> /// <param name="termcount"></param> /// <param name="divider"></param> /// <param name="parentKey">This value should be null on first call (parameter used during recursion)</param> /// <returns></returns> private static Dictionary <string, long> Flatten(ElasticsearchTermCount termcount, string divider, string parentKey) { var list = new Dictionary <string, long>(); foreach (var bucket in termcount.buckets) { var key = string.Format("{0}{1}", parentKey ?? string.Empty, bucket.key); if (bucket.term_count == null) { list.Add(key, bucket.doc_count); } else { foreach (var innerItem in Flatten(bucket.term_count, divider, string.Format("{0}{1}{2}", parentKey ?? string.Empty, bucket.key, divider))) { list.Add(innerItem.Key, innerItem.Value); } } } return(list); }