Ejemplo n.º 1
0
        private async Task FacetedQuery(DocumentsOperationContext context, string indexName, OperationCancelToken token)
        {
            var query = FacetQuery.Parse(HttpContext.Request.Query, GetStart(), GetPageSize(Database.Configuration.Core.MaxPageSize));

            var  existingResultEtag = GetLongFromHeaders("If-None-Match");
            long?facetsEtag         = null;

            if (query.FacetSetupDoc == null)
            {
                KeyValuePair <List <Facet>, long> facets;
                if (HttpContext.Request.Method == HttpMethod.Post.Method)
                {
                    var json = await context.ParseArrayToMemoryAsync(RequestBodyStream(), "facets", BlittableJsonDocumentBuilder.UsageMode.None);

                    facets = FacetedQueryParser.ParseFromJson(json);
                }
                else if (HttpContext.Request.Method == HttpMethod.Get.Method)
                {
                    var f = GetStringQueryString("facets");
                    if (string.IsNullOrWhiteSpace(f))
                    {
                        throw new InvalidOperationException("One of the required parameters (facetDoc or facets) was not specified.");
                    }

                    facets = await FacetedQueryParser.ParseFromStringAsync(f, context);
                }
                else
                {
                    throw new NotSupportedException($"Unsupported HTTP method '{HttpContext.Request.Method}' for Faceted Query.");
                }

                facetsEtag   = facets.Value;
                query.Facets = facets.Key;
            }

            var runner = new QueryRunner(Database, context);

            var result = await runner.ExecuteFacetedQuery(indexName, query, facetsEtag, existingResultEtag, token);

            if (result.NotModified)
            {
                HttpContext.Response.StatusCode = 304;
                return;
            }

            HttpContext.Response.Headers[Constants.MetadataEtagField] = result.ResultEtag.ToInvariantString();

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                writer.WriteFacetedQueryResult(context, result);
            }
        }