Page tree
Skip to end of metadata
Go to start of metadata


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"

// 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 =;
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();

// Display the median
System.err.println("Facet Statistics:");
System.err.printf ("          Median: %d\n", median);
  • No labels