예제 #1
0
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");
$response = $service->search('rande', 2, 1);
$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');
 /**
  * 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']));
 }