Exemplo n.º 1
0
 /**
  * Parse result data into result objects.
  *
  * @param Query             $query
  * @param GroupingComponent $grouping
  * @param array             $data
  *
  * @return Result
  */
 public function parse($query, $grouping, $data)
 {
     $groups = array();
     if (isset($data['grouped'])) {
         // parse field groups
         $valueResultClass = $grouping->getOption('resultvaluegroupclass');
         $documentClass = $query->getOption('documentclass');
         foreach ($grouping->getFields() as $field) {
             if (isset($data['grouped'][$field])) {
                 $result = $data['grouped'][$field];
                 $matches = isset($result['matches']) ? $result['matches'] : null;
                 $groupCount = isset($result['ngroups']) ? $result['ngroups'] : null;
                 $valueGroups = array();
                 foreach ($result['groups'] as $valueGroupResult) {
                     $value = isset($valueGroupResult['groupValue']) ? $valueGroupResult['groupValue'] : null;
                     $numFound = isset($valueGroupResult['doclist']['numFound']) ? $valueGroupResult['doclist']['numFound'] : null;
                     $start = isset($valueGroupResult['doclist']['start']) ? $valueGroupResult['doclist']['start'] : null;
                     $maxScore = isset($valueGroupResult['doclist']['maxScore']) ? $valueGroupResult['doclist']['maxScore'] : null;
                     // create document instances
                     $documents = array();
                     if (isset($valueGroupResult['doclist']['docs']) && is_array($valueGroupResult['doclist']['docs'])) {
                         foreach ($valueGroupResult['doclist']['docs'] as $doc) {
                             $documents[] = new $documentClass($doc);
                         }
                     }
                     $valueGroups[] = new $valueResultClass($value, $numFound, $start, $documents, $maxScore, $query);
                 }
                 $groups[$field] = new FieldGroup($matches, $groupCount, $valueGroups);
             }
         }
         // parse query groups
         $groupResultClass = $grouping->getOption('resultquerygroupclass');
         foreach ($grouping->getQueries() as $groupQuery) {
             if (isset($data['grouped'][$groupQuery])) {
                 $result = $data['grouped'][$groupQuery];
                 // get statistics
                 $matches = isset($result['matches']) ? $result['matches'] : null;
                 $numFound = isset($result['doclist']['numFound']) ? $result['doclist']['numFound'] : null;
                 $start = isset($result['doclist']['start']) ? $result['doclist']['start'] : null;
                 $maxScore = isset($result['doclist']['maxScore']) ? $result['doclist']['maxScore'] : null;
                 // create document instances
                 $documentClass = $query->getOption('documentclass');
                 $documents = array();
                 if (isset($result['doclist']['docs']) && is_array($result['doclist']['docs'])) {
                     foreach ($result['doclist']['docs'] as $doc) {
                         $documents[] = new $documentClass($doc);
                     }
                 }
                 // create a group result object
                 $group = new $groupResultClass($matches, $numFound, $start, $maxScore, $documents, $query);
                 $groups[$groupQuery] = $group;
             }
         }
     }
     return new Result($groups);
 }
Exemplo n.º 2
0
 /**
  * Add request settings for Grouping.
  *
  * @param GroupingComponent $component
  * @param Request           $request
  *
  * @return Request
  */
 public function buildComponent($component, $request)
 {
     // enable grouping
     $request->addParam('group', 'true');
     $request->addParam('group.field', $component->getFields());
     $request->addParam('group.query', $component->getQueries());
     $request->addParam('group.limit', $component->getLimit());
     $request->addParam('group.offset', $component->getOffset());
     $request->addParam('group.sort', $component->getSort());
     $request->addParam('group.main', $component->getMainResult());
     $request->addParam('group.ngroups', $component->getNumberOfGroups());
     $request->addParam('group.cache.percent', $component->getCachePercentage());
     $request->addParam('group.truncate', $component->getTruncate());
     $request->addParam('group.func', $component->getFunction());
     $request->addParam('group.facet', $component->getFacet());
     $request->addParam('group.format', $component->getFormat());
     return $request;
 }
Exemplo n.º 3
0
 public function testSetAndGetFieldsArray()
 {
     $values = array('fieldD', 'fieldE');
     $this->grouping->setFields($values);
     $this->assertEquals($values, $this->grouping->getFields());
 }