public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy)
		{
			var result = new FacetResults();

			foreach (var response in responses.Select(response => (RavenJObject)response.Result))
			{
				var facet = response.JsonDeserialization<FacetResults>();
				foreach (var facetResult in facet.Results)
				{
					if (!result.Results.ContainsKey(facetResult.Key))
						result.Results[facetResult.Key] = new FacetResult();

					var newFacetResult = result.Results[facetResult.Key];
					foreach (var facetValue in facetResult.Value.Values)
					{
						var existingFacetValueRange = newFacetResult.Values.Find((x) => x.Range == facetValue.Range);
						if (existingFacetValueRange != null)
							existingFacetValueRange.Hits += facetValue.Hits;
						else
							newFacetResult.Values.Add(new FacetValue() { Hits = facetValue.Hits, Range = facetValue.Range });
					}

					foreach (var facetTerm in facetResult.Value.RemainingTerms)
					{
						if (!newFacetResult.RemainingTerms.Contains(facetTerm))
							newFacetResult.RemainingTerms.Add(facetTerm);
					}
				}
			}

			Result = result;
		}
        public FacetResults GetFacets(string index, IndexQuery indexQuery, List<Facet> facets, int start = 0, int? pageSize = null)
        {
            var sp = Stopwatch.StartNew();
            var results = new FacetResults();
            var defaultFacets = new Dictionary<string, Facet>();
            var rangeFacets = new Dictionary<string, List<ParsedRange>>();

            var viewGenerator = database.IndexDefinitionStorage.GetViewGenerator(index);
            Index.AssertQueryDoesNotContainFieldsThatAreNotIndexed(indexQuery, viewGenerator);

            foreach (var facet in facets)
            {
                var key = string.IsNullOrWhiteSpace(facet.DisplayName) ? facet.Name : facet.DisplayName;

                defaultFacets[key] = facet;
                if (facet.Aggregation != FacetAggregation.Count && facet.Aggregation != FacetAggregation.None)
                {
                    if (string.IsNullOrEmpty(facet.AggregationField))
                        throw new InvalidOperationException("Facet " + facet.Name + " cannot have aggregation set to " +
                                                            facet.Aggregation + " without having a value in AggregationField");

                    if (string.IsNullOrEmpty(facet.AggregationType))
                        throw new InvalidOperationException("Facet " + facet.Name + " cannot have aggregation set to " +
                                                            facet.Aggregation + " without having a value in AggregationType");

                    if (facet.AggregationField.EndsWith("_Range") == false)
                    {
                        if( QueryForFacets.IsAggregationTypeNumerical(facet.AggregationType))
                             facet.AggregationField = facet.AggregationField + "_Range";
                    }
                       
                }


                switch (facet.Mode)
                {
                    case FacetMode.Default:
                        results.Results[key] = new FacetResult();
                        break;
                    case FacetMode.Ranges:
                        rangeFacets[key] = facet.Ranges.Select(range => ParseRange(facet.Name, range)).ToList();
                        results.Results[key] = new FacetResult
                        {
                            Values = facet.Ranges.Select(range => new FacetValue
                            {
                                Range = range,
                            }).ToList()
                        };

                        break;
                    default:
                        throw new ArgumentException(string.Format("Could not understand '{0}'", facet.Mode));
                }
            }

            var queryForFacets = new QueryForFacets(database, index, defaultFacets, rangeFacets, indexQuery, results, start, pageSize);
            queryForFacets.Execute();
            results.Duration = sp.Elapsed;
            return results;
        }
Beispiel #3
0
		public FacetResults GetFacets(string index, IndexQuery indexQuery, string facetSetupDoc, int start = 0, int? pageSize = null)
		{
			var facetSetup = database.Get(facetSetupDoc, null);
			if (facetSetup == null)
				throw new InvalidOperationException("Could not find facets document: " + facetSetupDoc);

			var facets = facetSetup.DataAsJson.JsonDeserialization<FacetSetup>().Facets;

			var results = new FacetResults();
			var defaultFacets = new Dictionary<string, Facet>();
			var rangeFacets = new Dictionary<string, List<ParsedRange>>();

			foreach (var facet in facets)
			{
				switch (facet.Mode)
				{
					case FacetMode.Default:
						//Remember the facet, so we can run them all under one query
						defaultFacets[facet.Name] = facet;
						results.Results[facet.Name] = new FacetResult();
						break;
					case FacetMode.Ranges:
						rangeFacets[facet.Name] = facet.Ranges.Select(range => ParseRange(facet.Name, range)).ToList();
						results.Results[facet.Name] = new FacetResult
						{
							Values = facet.Ranges.Select(range => new FacetValue
							{
								Range = range,
							}).ToList()
						};

						break;
					default:
						throw new ArgumentException(string.Format("Could not understand '{0}'", facet.Mode));
				}
			}

			var queryForFacets = new QueryForFacets(database, index, defaultFacets, rangeFacets, indexQuery, results, start, pageSize);
			queryForFacets.Execute();

			return results;
		}
			public QueryForFacets(
				DocumentDatabase database,
				string index,
				 Dictionary<string, Facet> facets,
				 Dictionary<string, List<ParsedRange>> ranges,
				 IndexQuery indexQuery,
				 FacetResults results,
				 int start,
				 int? pageSize)
			{
				Database = database;
				Index = index;
				Facets = facets;
				Ranges = ranges;
				IndexQuery = indexQuery;
				Results = results;
				Start = start;
				PageSize = pageSize;
				indexDefinition = Database.IndexDefinitionStorage.GetIndexDefinition(Index);
			}
Beispiel #5
0
		private void CheckFacetResultsMatchInMemoryData(
					FacetResults facetResults,
					List<Camera> filteredData)
		{
			//Make sure we get all range values
			Assert.Equal(filteredData.GroupBy(x => x.Manufacturer).Count(),
						facetResults.Results["Manufacturer"].Values.Count());

			foreach (var facet in facetResults.Results["Manufacturer"].Values)
			{
				var inMemoryCount = filteredData.Count(x => x.Manufacturer.ToLower() == facet.Range);
				Assert.Equal(inMemoryCount, facet.Hits);
			}

			//Go through the expected (in-memory) results and check that there is a corresponding facet result
			//Not the prettiest of code, but it works!!!
			var costFacets = facetResults.Results["Cost_Range"].Values;
			CheckFacetCount(filteredData.Count(x => x.Cost <= 200.0m),
							costFacets.FirstOrDefault(x => x.Range == "[NULL TO Dx200]"));
			CheckFacetCount(filteredData.Count(x => x.Cost >= 200.0m && x.Cost <= 400),
							costFacets.FirstOrDefault(x => x.Range == "[Dx200 TO Dx400]"));
			CheckFacetCount(filteredData.Count(x => x.Cost >= 400.0m && x.Cost <= 600.0m),
							costFacets.FirstOrDefault(x => x.Range == "[Dx400 TO Dx600]"));
			CheckFacetCount(filteredData.Count(x => x.Cost >= 600.0m && x.Cost <= 800.0m),
							costFacets.FirstOrDefault(x => x.Range == "[Dx600 TO Dx800]"));
			CheckFacetCount(filteredData.Count(x => x.Cost >= 800.0m),
							costFacets.FirstOrDefault(x => x.Range == "[Dx800 TO NULL]"));

			//Test the Megapixels_Range facets using the same method
			var megapixelsFacets = facetResults.Results["Megapixels_Range"].Values;
			CheckFacetCount(filteredData.Where(x => x.Megapixels <= 3.0m).Count(),
							megapixelsFacets.FirstOrDefault(x => x.Range == "[NULL TO Dx3]"));
			CheckFacetCount(filteredData.Where(x => x.Megapixels >= 3.0m && x.Megapixels <= 7.0m).Count(),
							megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx3 TO Dx7]"));
			CheckFacetCount(filteredData.Where(x => x.Megapixels >= 7.0m && x.Megapixels <= 10.0m).Count(),
							megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx7 TO Dx10]"));
			CheckFacetCount(filteredData.Where(x => x.Megapixels >= 10.0m).Count(),
							megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx10 TO NULL]"));
		}
Beispiel #6
0
		private void PrintFacetResults(FacetResults facetResults)
		{
			foreach (var kvp in facetResults.Results)
			{
				if (kvp.Value.Values.Count() > 0)
				{
					Console.WriteLine(kvp.Key + ":");
					foreach (var facet in kvp.Value.Values)
					{
						Console.WriteLine("    {0}: {1}", facet.Range, facet.Hits);
					}
					Console.WriteLine();
				}
			}
		}
Beispiel #7
0
        private bool AreFacetsEquiv(FacetResults left, FacetResults right)
        {
            //check if same number of ranges.
            if(left.Results.Count != right.Results.Count 
               // || left.Results.Select(r=> r.Key).Intersect(right.Results.Select(r=> r.Key)).Count() != left.Results.Count
                ){
                return false;
            }
            //deeper check onthe ranges.
            if(left.Results.Sum(r=> r.Value.Values.Sum(var=> var.Hits)) != right.Results.Sum(r=> r.Value.Values.Sum(var=> var.Hits))){
                return false;
            }

            //all the way down...
            foreach (var lfr in left.Results)
            {
                var leftFacetResult = lfr.Value;
                var rightFacetResult = right.Results.First(r => r.Key.Contains(lfr.Key) || lfr.Key.Contains(r.Key)).Value;
                for (int i = 0; i < leftFacetResult.Values.Count; i++)
                {
                    if (leftFacetResult.Values[i].Hits != rightFacetResult.Values[i].Hits)
                    {
                        return false;
                    }
                }

            }
            return true;
        }
Beispiel #8
0
        private void CheckFacetResultsMatchInMemoryData(FacetResults facetResults, List<Camera> filteredData, IList<DateTime> dates)
        {
            //Test the Megapixels_Range facets using the same method
            var dateOfListingFacets = facetResults.Results["DateOfListing_Range"].Values;
            CheckFacetCount(filteredData.Count(x => x.DateOfListing <= dates.First()), dateOfListingFacets.FirstOrDefault(x => x.Range.Contains("NULL TO")));
            CheckFacetCount(filteredData.Count(x => x.DateOfListing >= dates.Last()), dateOfListingFacets.FirstOrDefault(x => x.Range.Contains("TO NULL")));

        }
Beispiel #9
0
			public QueryForFacets(
				DocumentDatabase database,
				string index,
				 Dictionary<string, Facet> facets,
				 Dictionary<string, List<ParsedRange>> ranges,
				 IndexQuery indexQuery,
				 FacetResults results)
			{
				Database = database;
				Index = index;
				Facets = facets;
				Ranges = ranges;
				IndexQuery = indexQuery;
				Results = results;
			}