Overview
This page details how to compute the median value of a field over all documents matching a specified query using a facet.
View incoming links.
Example (Java)
// Create a client factory AieClientFactory clientFactory = new DefaultAieClientFactory(); // create an attivio search client SearchClient client = clientFactory.createSearchClient(host, port); // Must set a workflow: default workflow is "search" client.setClientWorkflow("search"); // Create the query request QueryRequest request = new QueryRequest("*:*"); FacetRequest facetRequest = new FacetRequest("field_i"); facetRequest.setCalculateStatistics(true); // Need the getCount() from FacetResponseStatistics facetRequest.setFacetFinder(false); // Disable facet finder (otherwise FacetRequest may be altered) facetRequest.setMaxBuckets(-1); // Need all buckets to get accurate result facetRequest.setDistributedMaxBuckets(-1); // unlimited (prevent cutoff on sub dispatchers for 100% accurate result) facetRequest.setSortOrder(FacetRequest.SortBy.VALUE, Sort.SortOrder.ASCENDING); // sort buckets by value ascending request.addFacet(facetRequest); // Add to our query request // Get the query response QueryResponse response = client.search(request); FacetResponse facet = response.getFacet("field_i"); FacetResponseStatistics stats = facet.getStatistics(); long medianIndex = stats.getCount() / 2L; // Index for median value // Step through buckets until we find the one halfway through long index = 0L; Integer median = null; for (FacetBucket bucket : facet) { index += bucket.getCount(); if (index > medianIndex) { median = (Integer)((NumericBucket)bucket).getSortValue(); break; } } // Display the median System.err.println("Facet Statistics:"); System.err.printf (" Median: %d\n", median);