/** * define the solr engine, use this only for testing */ public function getSearchService() { if ($this->search_service == null) { $solr = new sfLuceneService($this->getParameter('host'), $this->getParameter('port'), $this->getParameter('base_url') . '/' . $this->getParameter('index_location')); if (!$solr->ping()) { //throw new Exception('Search is not available right now.'); } $this->search_service = $solr; } return $this->search_service; }
/** * sanitize a phrase to be correctly handler by the solr engine * * @param string $keyword * @return string */ public static function sanitize($keyword) { $keyword = str_replace('"', '', $keyword); return sfLuceneService::phrase($keyword); }
<?php require dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new limeade_test(null, limeade_output::get()); $service = new sfLuceneService('127.0.0.1', '8983'); if (!$service->ping()) { // $t->fail('Solr is not running'); die(); } $t->is($service->getHost(), '127.0.0.1', '->getHost() ok'); $t->is($service->getPort(), '8983', '->getPort() ok'); $t->is($service->getPath(), '/solr/', '->getPath() ok'); $service->setPath('/solr/index_fr/'); $t->is($service->getPath(), '/solr/index_fr/', '->setPath() ok'); try { $response = $service->deleteByQuery('non_existent_field:asd'); $t->fail('::deleteByQuery refers to a non existent field'); } catch (Exception $e) { $t->pass('::deleteByQuery raise an error on non existent field'); } $t->diag("search for rande, limit:2, offset:0"); $response = $service->search('rande', 0, 2); $t->ok($response instanceof sfLuceneDocument, '::search returns Apache_Solr_Response object'); $t->cmp_ok($response->getHttpStatusMessage(), '===', 'OK', '::getHttpStatusMessage return OK'); $t->cmp_ok($response->getHttpStatus(), '===', '200', '::getHttpStatus return code 200'); $t->cmp_ok($response->response->numFound, '===', 3, '->response->numFound return 3 entries'); $t->cmp_ok(count($response->response->docs), '===', 2, '->response->numFound return 2 entries'); $t->ok($response->response->docs[0] instanceof sfLuceneDocument, '->response->docs[0] return an instance sfLuceneDocument'); $t->cmp_ok($response->response->docs[0]->sfl_guid, '===', 'GUID_1', '->response->docs[0]->sfl_guid ok'); $t->cmp_ok($response->response->docs[1]->sfl_guid, '===', 'GUID_2', '->response->docs[1]->sfl_guid ok'); // $t->diag("search for rande, limit:1, offset:2");
/** * get unique key list by keywords * * @param keywords str: a user's search terms * @param limit int: limit the resultset to the number specified * @param idFieldName str: the canonical id fieldname for the song's unique id * @return arr: a list of matching keys found by fulltext search */ public function getKeys($keywords, $limit = 100, $idFieldName = 'sfl_guid') { $user_search = preg_match("/[\\*|\\!|\\+|\\-|\\&\\&|\\|\\||\\(|\\)|\\[|\\]|\\^|\\~|\\*|\\?|\\:|\\\"|\\\\]/", $keywords, $void_matches); $service = new sfLuceneService('localhost', 8983, '/solr/index_en'); $query = sprintf('%s%s%s', trim(strtolower($keywords)), $user_search ? '' : '*', $user_search ? '' : ' OR ' . trim(strtolower($keywords))); $response = $service->search($query, 0, $limit, array('fl' => $idFieldName)); $docs = json_decode($response->getRawResponse(), true); return array_values(array_map(array($this, 'result_map'), $docs['response']['docs'])); }