/** * Looks up a given $query with the given $config * * @param string $query String to look up in data * @param array $config Array of configuration options * @return array */ public function lookUp($query, $config) { // require the class require_once 'comb.php'; // create a hash for this search $hash = 'searches/' . $this->getSearchHash($config); // while we're here, clean up old unusable cached searches if ($config['query_cache_length'] == "on cache update") { $this->cache->purgeFromBefore(Cache::getLastCacheUpdate(), 'searches'); } else { $this->cache->purgeOlderThan($config['query_cache_length'] * 60, 'searches'); } // perform the search if ($this->cache->exists($hash)) { // use cached results, they're still relevant $output = unserialize($this->cache->get($hash)); // record search if (isset($output['not_enough_characters'])) { // do nothing } elseif (isset($output['no_query']) || isset($output['no_results'])) { $this->logSearch($config['query'], 0); } elseif (isset($output['too_many_results'])) { $this->logSearch($config['query'], 'too many'); } else { $this->logSearch($config['query'], count($output)); } } else { // get data $data = $this->getData($config); try { $comb = Comb::create($data, $config); // look up query $results = $comb->lookUp($query); // fix results $output = array(); $i = 1; foreach ($results['data'] as $result) { $item = array('_score' => $result['score'], '_category' => $result['category'], '_query' => $results['info']['raw_query'], '_parsed_query' => $results['info']['parsed_query'], '_query_time' => $results['info']['query_time']); array_push($output, array_merge($item, $result['data'])); $i++; } // save this $this->cache->put($hash, serialize($output)); // record successful search $this->logSearch($config['query'], count($output)); } catch (CombNoResultsFoundException $e) { $output = array(array('no_results' => true, '_query' => htmlentities($query))); $this->cache->put($hash, serialize($output)); // record failed search $this->logSearch($config['query'], 0); } catch (CombNoQueryException $e) { $this->log->error($e->getMessage()); $output = array(array('no_query' => true, '_query' => htmlentities($query))); $this->cache->put($hash, serialize($output)); // record failed search $this->logSearch($config['query'], 0); } catch (CombTooManyResultsException $e) { $this->log->error($e->getMessage()); $output = array(array('too_many_results' => true, '_query' => htmlentities($query))); $this->cache->put($hash, serialize($output)); // record failed search $this->logSearch($config['query'], 'too many'); } catch (CombNotEnoughCharactersException $e) { $output = array(array('not_enough_characters' => true, 'no_query' => true, 'no_results' => true, '_query' => htmlentities($query))); $this->cache->put($hash, serialize($output)); } catch (CombException $e) { $this->log->error($e->getMessage()); $output = array(array('no_results' => true, '_query' => htmlentities($query))); $this->cache->put($hash, serialize($output)); // record failed search $this->logSearch($config['query'], 0); } } return $output; }