1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Search_Elastic_FacetBuilder 9{ 10 private $count; 11 private $mainKey; 12 13 function __construct($count = 10, $useAggregations = false) 14 { 15 $this->count = $count; 16 $this->mainKey = $useAggregations ? 'aggregations' : 'facets'; 17 } 18 19 function build(array $facets) 20 { 21 if (empty($facets)) { 22 return []; 23 } 24 25 $out = []; 26 foreach ($facets as $facet) { 27 $out[$facet->getName()] = $this->buildFacet($facet); 28 } 29 30 return [ 31 $this->mainKey => $out, 32 ]; 33 } 34 35 private function buildFacet(Search_Query_Facet_Interface $facet) 36 { 37 $type = $facet->getType(); 38 39 $out = [ 40 'field' => $facet->getField(), 41 ]; 42 43 if ($type === 'date_histogram') { 44 $out['interval'] = $facet->getInterval(); 45 } else if ($type === 'date_range') { 46 $out['ranges'] = $facet->getRanges(); 47 } else { 48 $out['size'] = $facet->getCount() ?: $this->count; 49 $order = $facet->getOrder(); 50 if ($order) { 51 $out['order'] = $order; 52 } 53 $minDocCount = $facet->getMinDocCount(); 54 if ($minDocCount !== null) { 55 $out['min_doc_count'] = $minDocCount; 56 } 57 } 58 59 return [$type => $out]; 60 } 61} 62