예제 #1
0
 /**
  * Get result data for the response
  *
  * @throws RuntimeException
  * @param  Result           $result
  * @return array
  */
 public function parse($result)
 {
     $data = $result->getData();
     /**
      * @var $query Query
      */
     $query = $result->getQuery();
     // create document instances
     $documentClass = $query->getOption('documentclass');
     $classes = class_implements($documentClass);
     if (!in_array('Solarium\\QueryType\\Select\\Result\\DocumentInterface', $classes) && !in_array('Solarium\\QueryType\\Update\\Query\\Document\\DocumentInterface', $classes)) {
         throw new RuntimeException('The result document class must implement a document interface');
     }
     $documents = array();
     if (isset($data['response']['docs'])) {
         foreach ($data['response']['docs'] as $doc) {
             $fields = (array) $doc;
             $documents[] = new $documentClass($fields);
         }
     }
     // component results
     $components = array();
     foreach ($query->getComponents() as $component) {
         $componentParser = $component->getResponseParser();
         if ($componentParser) {
             $components[$component->getType()] = $componentParser->parse($query, $component, $data);
         }
     }
     if (isset($data['response']['numFound'])) {
         $numFound = $data['response']['numFound'];
     } else {
         $numFound = null;
     }
     return $this->addHeaderInfo($data, array('numfound' => $numFound, 'documents' => $documents, 'components' => $components));
 }
예제 #2
0
/**
 * Lets modules alter the search results returned from a Solr search.
 *
 * @param \Drupal\search_api\Query\ResultSetInterface $results
 *   The results array that will be returned for the search.
 * @param \Drupal\search_api\Query\QueryInterface $query
 *   The SearchApiQueryInterface object representing the executed search query.
 * @param \Solarium\QueryType\Select\Result\Result $resultset
 *   The Solarium result object.
 */
function hook_search_api_solr_search_results_alter(\Drupal\search_api\Query\ResultSetInterface $results, \Drupal\search_api\Query\QueryInterface $query, \Solarium\QueryType\Select\Result\Result $resultset)
{
    $resultset_data = $resultset->getData();
    if (isset($resultset_data['facet_counts']['facet_fields']['custom_field'])) {
        // Do something with $results.
    }
}
예제 #3
0
 /**
  * Extracts facets from a Solarium result set.
  *
  * @param \Drupal\search_api\Query\QueryInterface $query
  *   The search query.
  * @param \Solarium\QueryType\Select\Result\Result $resultset
  *   A Solarium select response object.
  *
  * @return array
  *   An array describing facets that apply to the current results.
  */
 protected function extractFacets(QueryInterface $query, Result $resultset)
 {
     $facets = array();
     if (!$resultset->getFacetSet()) {
         return $facets;
     }
     $index = $query->getIndex();
     $field_names = $this->getFieldNames($index);
     $fields = $index->getFields();
     $extract_facets = $query->getOption('search_api_facets', array());
     if ($facet_fields = $resultset->getFacetSet()->getFacets()) {
         foreach ($extract_facets as $delta => $info) {
             $field = $field_names[$info['field']];
             if (!empty($facet_fields[$field])) {
                 $min_count = $info['min_count'];
                 $terms = $facet_fields[$field]->getValues();
                 if ($info['missing']) {
                     // We have to correctly incorporate the "_empty_" term.
                     // This will ensure that the term with the least results is dropped,
                     // if the limit would be exceeded.
                     if (isset($terms[''])) {
                         if ($terms[''] < $min_count) {
                             unset($terms['']);
                         } else {
                             arsort($terms);
                             if ($info['limit'] > 0 && count($terms) > $info['limit']) {
                                 array_pop($terms);
                             }
                         }
                     }
                 } elseif (isset($terms[''])) {
                     unset($terms['']);
                 }
                 $type = isset($fields[$info['field']]) ? $fields[$info['field']]->getType() : 'string';
                 foreach ($terms as $term => $count) {
                     if ($count >= $min_count) {
                         if ($term === '') {
                             $term = '!';
                         } elseif ($type == 'boolean') {
                             if ($term == 'true') {
                                 $term = '"1"';
                             } elseif ($term == 'false') {
                                 $term = '"0"';
                             }
                         } elseif ($type == 'date') {
                             $term = $term ? '"' . strtotime($term) . '"' : NULL;
                         } else {
                             $term = "\"{$term}\"";
                         }
                         if ($term) {
                             $facets[$delta][] = array('filter' => $term, 'count' => $count);
                         }
                     }
                 }
                 if (empty($facets[$delta])) {
                     unset($facets[$delta]);
                 }
             }
         }
     }
     $result_data = $resultset->getData();
     if (isset($result_data['facet_counts']['facet_queries'])) {
         if ($spatials = $query->getOption('search_api_location')) {
             foreach ($result_data['facet_counts']['facet_queries'] as $key => $count) {
                 if (!preg_match('/^spatial-(.*)-(\\d+(?:\\.\\d+)?)$/', $key, $m)) {
                     continue;
                 }
                 if (empty($extract_facets[$m[1]])) {
                     continue;
                 }
                 $facet = $extract_facets[$m[1]];
                 if ($count >= $facet['min_count']) {
                     $facets[$m[1]][] = array('filter' => "[* {$m[2]}]", 'count' => $count);
                 }
             }
         }
     }
     return $facets;
 }