private bool AreFacetsEquiv(FacetedQueryResult left, FacetedQueryResult 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); }
private void CheckFacetResultsMatchInMemoryData(FacetedQueryResult 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.Count(x => x.Megapixels <= 3.0m), megapixelsFacets.FirstOrDefault(x => x.Range == "[NULL TO Dx3]")); CheckFacetCount(filteredData.Count(x => x.Megapixels >= 3.0m && x.Megapixels <= 7.0m), megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx3 TO Dx7]")); CheckFacetCount(filteredData.Count(x => x.Megapixels >= 7.0m && x.Megapixels <= 10.0m), megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx7 TO Dx10]")); CheckFacetCount(filteredData.Count(x => x.Megapixels >= 10.0m), megapixelsFacets.FirstOrDefault(x => x.Range == "[Dx10 TO NULL]")); }
private static void CheckResults(FacetedQueryResult result) { Assert.Contains("Brand", result.Results.Select(x => x.Key)); FacetResult facetResult = result.Results["Brand"]; Assert.Equal(1, facetResult.Values.Count); facetResult.Values[0] = new FacetValue { Range = "mybrand1", Hits = 1 }; }
public override void SetResponseRaw(HttpResponseMessage response, Stream stream, JsonOperationContext context) { _command.SetResponseRaw(response, stream, context); Result = new FacetedQueryResult[_command.Result.Count]; for (var i = 0; i < _command.Result.Count; i++) { var result = _command.Result[i]; Result[i] = JsonDeserializationClient.FacetedQueryResult((BlittableJsonReaderObject)result.Result); } }
private static FacetedQueryResult ExecuteTest(IDocumentStore store) { FacetedQueryResult result = store.DatabaseCommands.GetFacets(new FacetQuery { IndexName = "Product/AvailableForSale2", Query = "MyName1", DefaultField = "Any", FacetSetupDoc = "facets/ProductFacets" }); return(result); }
private FacetedQueryResult HandlRenames(FacetedQueryResult facetedQueryResult) { foreach (var rename in renames) { FacetResult value; if (facetedQueryResult.Results.TryGetValue(rename.Value, out value) && facetedQueryResult.Results.ContainsKey(rename.Key) == false) { facetedQueryResult.Results[rename.Key] = value; facetedQueryResult.Results.Remove(rename.Value); } } return(facetedQueryResult); }
private void PrintFacetResults(FacetedQueryResult 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(); } } }
public void ShouldWorkWithRavenServer() { //arrange using (var store = GetDocumentStore()) { SetupTestData(store); WaitForIndexing(store); //Act FacetedQueryResult result = ExecuteTest(store); //Assert CheckResults(result); } }
public void ShouldWorkWithEmbeddedRaven() { //arrange using (var store = GetDocumentStore()) { store.RegisterListener(new NoStaleQueriesListener()); SetupTestData(store); WaitForIndexing(store); //Act FacetedQueryResult result = ExecuteTest(store); //Assert CheckResults(result); } }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new FacetedQueryResult(); foreach (var response in responses.Select(response => (RavenJObject)response.Result)) { var facet = response.JsonDeserialization <FacetedQueryResult>(); 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 static void WriteFacetedQueryResult(this BlittableJsonTextWriter writer, JsonOperationContext context, FacetedQueryResult result) { writer.WriteStartObject(); writer.WritePropertyName(nameof(result.IndexName)); writer.WriteString(result.IndexName); writer.WriteComma(); writer.WritePropertyName(nameof(result.Results)); writer.WriteStartObject(); var isFirstInternal = true; foreach (var kvp in result.Results) { if (isFirstInternal == false) { writer.WriteComma(); } isFirstInternal = false; writer.WritePropertyName(kvp.Key); writer.WriteFacetResult(context, kvp.Value); } writer.WriteEndObject(); writer.WriteComma(); writer.WritePropertyName(nameof(result.IndexTimestamp)); writer.WriteString(result.IndexTimestamp.ToString(Default.DateTimeFormatsToWrite)); writer.WriteComma(); writer.WritePropertyName(nameof(result.LastQueryTime)); writer.WriteString(result.LastQueryTime.ToString(Default.DateTimeFormatsToWrite)); writer.WriteComma(); writer.WritePropertyName(nameof(result.IsStale)); writer.WriteBool(result.IsStale); writer.WriteComma(); writer.WritePropertyName(nameof(result.ResultEtag)); writer.WriteInteger(result.ResultEtag); writer.WriteEndObject(); }
public static void WriteFacetedQueryResult(this BlittableJsonTextWriter writer, JsonOperationContext context, FacetedQueryResult result, out int numberOfResults) { writer.WriteStartObject(); writer.WritePropertyName(nameof(result.TotalResults)); writer.WriteInteger(result.TotalResults); writer.WriteComma(); writer.WritePropertyName(nameof(result.DurationInMs)); writer.WriteInteger(result.DurationInMs); writer.WriteComma(); writer.WriteQueryResult(context, result, metadataOnly: false, numberOfResults: out numberOfResults, partial: true); writer.WriteEndObject(); }
private FacetedQueryResult MergeFacets(FacetedQueryResult[] results) { if (results == null) { return(null); } if (results.Length == 0) { return(null); } if (results.Length == 1) { return(results[0]); } var finalResult = new FacetedQueryResult(); var avgs = new Dictionary <FacetValue, List <double> >(); foreach (var result in results.SelectMany(x => x.Results)) { FacetResult value; if (finalResult.Results.TryGetValue(result.Key, out value) == false) { finalResult.Results[result.Key] = value = new FacetResult(); } value.RemainingHits += result.Value.RemainingHits; if (result.Value.RemainingTerms != null && result.Value.RemainingTerms.Count > 0) { value.RemainingTerms = value.RemainingTerms.Union(result.Value.RemainingTerms).ToList(); } value.RemainingHits += result.Value.RemainingTermsCount; foreach (var facetValue in result.Value.Values) { var match = value.Values.FirstOrDefault(x => x.Range == facetValue.Range); if (match == null) { match = new FacetValue { Range = facetValue.Range }; value.Values.Add(facetValue); } if (facetValue.Sum != null) { match.Sum += facetValue.Sum; } if (match.Min != null || facetValue.Min != null) { match.Min = Math.Min(match.Min ?? double.MaxValue, facetValue.Min ?? double.MaxValue); } if (match.Max != null || facetValue.Max != null) { match.Max = Math.Min(match.Max ?? double.MinValue, facetValue.Max ?? double.MinValue); } match.Hits += facetValue.Hits; if (facetValue.Count != null) { match.Count += facetValue.Count; } if (facetValue.Average != null) { List <double> list; if (avgs.TryGetValue(match, out list) == false) { avgs[match] = list = new List <double>(); } list.Add(facetValue.Average.Value); } } } foreach (var avg in avgs) { avg.Key.Average = avg.Value.Average(); } return(finalResult); }