public static FacetQuery Create(DocumentsOperationContext context, IndexQueryServerSide query) { long?facetsEtag = null; DocumentsTransaction tx = null; try { var facets = new Dictionary <string, FacetSetup>(StringComparer.OrdinalIgnoreCase); foreach (var selectField in query.Metadata.SelectFields) { if (selectField.IsFacet == false) { continue; } var facetField = (FacetField)selectField; if (facetField.FacetSetupDocumentId == null || facets.ContainsKey(facetField.FacetSetupDocumentId)) { continue; } if (tx == null) { tx = context.OpenReadTransaction(); } var documentJson = context.DocumentDatabase.DocumentsStorage.Get(context, facetField.FacetSetupDocumentId); if (documentJson == null) { throw new DocumentDoesNotExistException(facetField.FacetSetupDocumentId); } if (facetsEtag.HasValue == false) { facetsEtag = documentJson.Etag; } else { facetsEtag = facetsEtag.Value ^ documentJson.Etag; } var document = FacetSetup.Create(facetField.FacetSetupDocumentId, documentJson.Data); facets[facetField.FacetSetupDocumentId] = document; } return(new FacetQuery(query, facets, facetsEtag ?? 0) { Legacy = string.IsNullOrEmpty(query.ClientVersion) == false && query.ClientVersion[0] == '4' }); } finally { tx?.Dispose(); } }
public void CanDeserializeLegacyFacetSetup() { var facets = new DynamicJsonArray { new DynamicJsonValue { [nameof(Facet.Aggregations)] = new DynamicJsonValue { [nameof(FacetAggregation.Min)] = "Test" } } }; var rangeFacets = new DynamicJsonArray { new DynamicJsonValue { [nameof(Facet.Aggregations)] = new DynamicJsonValue { [nameof(FacetAggregation.Max)] = "Test2" } } }; var djv = new DynamicJsonValue { [nameof(FacetSetup.Facets)] = facets, [nameof(FacetSetup.RangeFacets)] = rangeFacets }; using (var context = JsonOperationContext.ShortTermSingleUse()) { var json = context.ReadObject(djv, "facet/setup"); var setup = FacetSetup.Create("myId", json); Assert.Equal("myId", setup.Id); Assert.Equal(1, setup.Facets.Count); Assert.Equal(1, setup.Facets[0].Aggregations.Count); Assert.Equal("Test", setup.Facets[0].Aggregations[FacetAggregation.Min].Single().Name); Assert.Null(setup.Facets[0].Aggregations[FacetAggregation.Min].Single().DisplayName); Assert.Equal(1, setup.RangeFacets.Count); Assert.Equal(1, setup.RangeFacets[0].Aggregations.Count); Assert.Equal("Test2", setup.RangeFacets[0].Aggregations[FacetAggregation.Max].Single().Name); Assert.Null(setup.RangeFacets[0].Aggregations[FacetAggregation.Max].Single().DisplayName); } }