/** * Store the Facet values for a given Sphinx query. * If replace is TRUE, always rewrite the values in the cache. * If sticky is TRUE, make the cached values always survive. * * @param MultiFieldQuery $query Sphinx query as a MultiFieldQuery object. * @param array $facets Array of Facet objects. * @param boolean $replace Whether to replace existing values. * @param boolean $sticky Whether to make the cached values sticky (for preloaded Facets). * @return boolean TRUE on success, FALSE on failure. */ public function setFacets(MultiFieldQuery $query, array $facets, $replace = false, $sticky = false) { $key = $this->getKey($query->toCanonical()); $results = array(); foreach ($facets as $facet) { $results[] = $facet->getResults(); } $results = serialize($results); return $this->_cache->set($key, $results, $replace, $sticky); }
/** * Used internally to set the computed Facet results, metadata and terms. * * @param MultiFieldQuery $query Sphinx query as a MultiFieldQuery object. * @param array $results Computed results from Sphinx. * @param DataSourceInterface $datasource Data source object. */ public function setValues(MultiFieldQuery $query, array $results, DataSourceInterface $datasource = null) { foreach (array_keys($this->_results) as $key) { if ($key != 'matches' && array_key_exists($key, $results)) { $this->_results[$key] = $results[$key]; } } if (!isset($results['matches'])) { return; } $terms = array(); // internal data source always overrides external data source if ($this->_datasource) { $datasource = $this->_datasource; } // fetch string terms if data source attached and configured if ($datasource && $this->_source) { $terms = $datasource->fetchTerms($results['matches'], $this->_source, function ($match) { return $match['attrs']['@groupby']; }); } // extend results array with additional attributes (@term, @selected) foreach ($results['matches'] as $match) { if (count($terms) && isset($terms[$match['attrs']['@groupby']])) { $match['@hit'] = $terms[$match['attrs']['@groupby']]; } else { $match['@hit'] = $match['attrs']['@groupby']; } $value = array(); foreach ($match['attrs'] as $key => $item) { if ($key[0] == '@') { $value[$key] = $item; } } $value['@term'] = $match['@hit']; $value['@groupfunc'] = isset($value['@groupfunc']) ? $value['@groupfunc'] : $value['@count']; $term = sprintf('@%s %s', $this->_sph_field, strtolower($value['@term'])); $id = sprintf('@%s %s', $this->_sph_field, strtolower($value['@groupby'])); $value['@selected'] = $query->hasQueryTerm($term) || $query->hasQueryTerm($id) ? 'True' : 'False'; $this->_results['matches'][] = $value; } // supply the query object with matched terms as well foreach ($query as $qt) { if ($qt->hasField($this->_sph_field) && isset($terms[$qt->getTerm()])) { $qt->setUserTerm($terms[$qt->getTerm()]); } } }