示例#1
0
 /**
  * @throws \RuntimeException if the client cannot connect so Solr host
  * @return \SolrClient
  */
 public function getClient()
 {
     try {
         $this->client->ping();
     } catch (\Exception $e) {
         $host = $this->connection['hostname'];
         $port = $this->connection['port'];
         $path = $this->connection['path'];
         throw new \RuntimeException(sprintf('Cannot connect to Solr host: %s:%s, path: %s', $host, $port, $path));
     }
     return $this->client;
 }
示例#2
0
 /**
  * Gets the solr client options
  * @return CAttributeCollection the client options
  */
 public function getClientOptions()
 {
     if ($this->_clientOptions === null) {
         $clientOptions = new CAttributeCollection();
         $clientOptions->caseSensitive = true;
         if ($this->_client !== null) {
             foreach ($this->_client->getOptions() as $key => $value) {
                 $clientOptions->add($key, $value);
             }
         }
         $this->_clientOptions = $clientOptions;
     }
     return $this->_clientOptions;
 }
示例#3
0
 public function __construct($params)
 {
     $succ = false;
     $i = -1;
     $servers = $params['servers'];
     $path = $params['path'];
     $retry = $params['retry'];
     $timeout = $params['timeout'];
     shuffle($servers);
     while (!$succ && $retry--) {
         $i = ($i + 1) % count($servers);
         $server = $servers[$i];
         $options = array('hostname' => $server['host'], 'port' => $server['port'], 'path' => $path, 'timeout' => $timeout);
         try {
             parent::__construct($options);
             $this->ping();
             $succ = true;
         } catch (Exception $e) {
             if ($retry == 1) {
                 $ci =& get_instance();
                 $ci->log->log('warning', 'connect to solr failed', $options);
                 throw $e;
             }
         }
     }
 }
示例#4
0
 /**
  * Commit the search and close the connection.
  * @return void
  */
 public function commit()
 {
     try {
         $this->client->commit();
     } catch (Exception $e) {
     }
 }
示例#5
0
 /**
  * Indexes the given entity
  * 
  * @param object $entity
  */
 public function indexEntity($entity)
 {
     $document = $this->createSolrDocument($entity);
     $this->client->addDocument($document);
     $this->client->commit();
     $this->fields = $this->getEntityFields(get_class($entity));
 }
示例#6
0
 /**
  * @param \SolrInputDocument $doc
  */
 private function addDocumentToIndex($doc)
 {
     try {
         $updateResponse = $this->solrClient->addDocument($doc);
         $this->solrClient->commit();
     } catch (\Exception $e) {
         $errorEvent = new ErrorEvent(null, null, 'add-document');
         $errorEvent->setException($e);
         $this->eventManager->handle(EventManager::ERROR, $errorEvent);
     }
 }
示例#7
0
function getResults($userQuery)
{
    $core = 'techknowledgy_core';
    $options = array('hostname' => 'localhost', 'port' => 8983, 'timeout' => 10, 'path' => '/solr/' . $core);
    $client = new SolrClient($options);
    #if (!$client->ping()) {
    #	exit('Solr service not responding.');
    #}
    #else{
    #	print "Worked!";
    #}
    $query = new SolrQuery();
    $query->setQuery($userQuery);
    $query->setStart(0);
    $query->setRows(1000);
    $query->addField('url')->addField('title')->addField('host')->addField('content');
    $query_response = $client->query($query);
    $response = $query_response->getResponse();
    #print_r($response);
    return $response;
}
示例#8
0
 public function testAutoUpdate()
 {
     $s = new Entity\TestAutoUpdate();
     $s->setContent('Lorem');
     $this->em->persist($s);
     $this->em->flush();
     $query = new \SolrQuery();
     $query->setQuery("id:" . $s->getSolrId());
     $response = $this->client->query($query)->getResponse();
     $this->assertEquals(1, $response->response->numFound);
     $s->setContent('Ipsum');
     $this->em->persist($s);
     $this->em->flush();
     $query = new \SolrQuery();
     $query->setQuery("content:Ipsum");
     $response = $this->client->query($query)->getResponse();
     $this->assertGreaterThanOrEqual(1, $response->response->numFound);
     $query = new \SolrQuery();
     $query->setQuery("id:" . $s->getSolrId());
     $this->em->remove($s);
     $this->em->flush();
     $response = $this->client->query($query)->getResponse();
     $this->assertEquals(0, $response->response->numFound);
 }
示例#9
0
 /**
  * Pings the Solr server using search_solr config
  *
  * @return true|string Returns true if all good or an error string.
  */
 public function is_server_ready()
 {
     if (empty($this->config->server_hostname) || empty($this->config->indexname)) {
         return 'No solr configuration found';
     }
     if (!($this->client = $this->get_search_client(false))) {
         return get_string('engineserverstatus', 'search');
     }
     try {
         @$this->client->ping();
     } catch (\SolrClientException $ex) {
         return 'Solr client error: ' . $ex->getMessage();
     } catch (\SolrServerException $ex) {
         return 'Solr server error: ' . $ex->getMessage();
     }
     // Check that setup schema has already run.
     try {
         $schema = new \search_solr\schema();
         $schema->validate_setup();
     } catch (\moodle_exception $e) {
         return $e->getMessage();
     }
     return true;
 }
<?php 
function commit2()
{
    $solrAddress = 'http://' . SOLR_SERVER_HOSTNAME . ':' . SOLR_SERVER_PORT . '/solr/collection1';
    $response = file_get_contents($solrAddress . '/update?commit=true');
}
include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334456);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$updateResponse = $client->addDocument($doc);
print_r($updateResponse->getResponse());
// Broken!
//$client->commit();
// Use our own solution instead!
commit2();
示例#11
0
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);
$query->setTermsField('cat');
$updateResponse = $client->query($query);
print_r($updateResponse->getResponse());
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$update_response = $client->optimize(4, false, false);
$response = $update_response->getRawRequest();
print_r($response);
 public static function buildStringCondition(&$conditions, &$values, $field, $param, $model, $join = array(null, null))
 {
     $has_result = false;
     $has_id = false;
     $nb_result = 0;
     $join_result = null;
     $param = urldecode($param);
     if (strlen($param) > 2) {
         if (sfConfig::get('app_solr_enable') == true) {
             /* init solr 
              */
             $max_row = sfConfig::get('app_solr_maxrow');
             $options = array('hostname' => sfConfig::get('app_solr_host'), 'port' => sfConfig::get('app_solr_port'), 'path' => sfConfig::get('app_solr_path'), 'timeout' => sfConfig::get('app_solr_timeout'));
             $client = new SolrClient($options);
             try {
                 // 1st search : exact search
                 $query_solr_exact = new SolrQuery();
                 $query_solr_exact->setQuery('*' . $param . '*');
                 $query_solr_exact->setRows($max_row);
                 if ($model == 'User' || $model == 'UserPrivateData') {
                     if (!sfContext::getInstance()->getUser()->isConnected()) {
                         $query_solr_exact->addFilterQuery('user_private_public:true');
                     }
                 }
                 $query_solr_exact->addFilterQuery('module:' . strtolower($model) . 's');
                 $query_solr_exact->addField('name')->addField('module')->addField('id_doc');
                 $res_exact = $client->query($query_solr_exact)->getResponse();
                 if ($res_exact['response']['numFound'] > 0) {
                     for ($i = 0; $i < $res_exact['response']['numFound']; $i++) {
                         $ids_tmp[]['id'] = $res_exact['response']['docs'][$i]['id_doc'];
                     }
                 } else {
                     // No exact serach ... so try fuzzy search
                     $query_solr = new SolrQuery();
                     // Fuzzy search word > 3 letters
                     $query_words = explode(" ", $param);
                     foreach ($query_words as &$word) {
                         switch (true) {
                             case in_array(strlen($word), range(0, 3)):
                                 $word = $word;
                                 break;
                             case in_array(strlen($word), range(4, 5)):
                                 $word = $word . '~1';
                                 break;
                             case in_array(strlen($word), range(6, 7)):
                                 $word = $word . '~2';
                                 break;
                             case in_array(strlen($word), range(8, 9)):
                                 $word = $word . '~3';
                                 break;
                             default:
                                 $word = $word . '~4';
                                 break;
                         }
                     }
                     $query_search_fuzzy = implode(' ', $query_words);
                     $query_search = "({$param})^20 OR ({$query_search_fuzzy})^5";
                     c2cTools::log(" solr request : " . $query_search);
                     $query_solr->setQuery($query_search);
                     $query_solr->setRows($max_row);
                     if ($model == 'User' || $model == 'UserPrivateData') {
                         if (!sfContext::getInstance()->getUser()->isConnected()) {
                             $query_solr->addFilterQuery('user_private_public:true');
                         }
                     }
                     $query_solr->addFilterQuery('module:' . strtolower($model) . 's');
                     $query_solr->addField('name')->addField('module')->addField('id_doc');
                     $res = $client->query($query_solr)->getResponse();
                     for ($i = 0; $i < $res['response']['numFound']; $i++) {
                         $ids_tmp[]['id'] = $res['response']['docs'][$i]['id_doc'];
                     }
                 }
             } catch (Exception $e) {
                 c2cTools::log(" exception solr : " . $e);
                 $ids_tmp = self::idSearchByName($param, $model);
             }
         } else {
             $ids_tmp = self::idSearchByName($param, $model);
         }
         if (count($ids_tmp)) {
             $ids = array();
             foreach ($ids_tmp as $id) {
                 $ids[] = $id['id'];
             }
             $conditions[] = $field[0] . ' IN (' . implode(',', $ids) . ')';
             $has_result = true;
             $nb_result = count($ids);
             $join_result = $join[0];
             if (!$join[0]) {
                 $has_id = true;
             }
         }
     } else {
         $conditions[] = $field[1] . ' LIKE make_search_name(?)||\'%\'';
         $values[] = $param;
         $has_result = true;
         if ($join[1]) {
             $join_result = $join[1];
         }
     }
     return array('has_result' => $has_result, 'has_id' => $has_id, 'nb_result' => $nb_result, 'join' => $join_result);
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$update_response = $client->commit(true, false, false);
$response = $update_response->getRawRequest();
print_r($response);
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('manu:"Apple Computer Inc." OR text:apple');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$response = $query_response->getResponse();
print_r($response);
示例#16
0
 public function facet_date()
 {
     $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'port' => SOLR_SERVER_PORT);
     $client = new SolrClient($options);
     $query = new SolrQuery('*:*');
     //$query->setQuery('input_datetime:[2010-01-01T00:00:00Z TO 2010-06-01T00:00:00Z]');
     $query->setFacet(TRUE);
     $query->setFacetSort(SolrQuery::FACET_SORT_INDEX);
     $query->setFacetLimit(20000);
     $query->addFacetDateField('input_datetime');
     //$query->setFacetDateStart('2010-06-01T00:00:00Z');
     $query->setFacetDateStart('2008-06-01T00:00:00Z');
     $query->setFacetDateEnd('2010-12-01T00:00:00Z');
     $query->setFacetDateGap('+1MONTH');
     $query->setFacetDateHardEnd(TRUE);
     //$query->setFacetMinCount(2);
     $query->setFacetOffset(0);
     //$query->setFacetDateStart('2012-01-01T00:00:00:Z', 'input_date');
     //$query->setFacetPrefix('c');
     $updateResponse = $client->query($query);
     $response_array = $updateResponse->getResponse();
     $facet_datas = $response_array->facet_counts->facet_fields;
     echo '<pre>';
     print_r($response_array->facet_counts);
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_STORE_PATH);
$client = new SolrClient($options);
$product = new SolrInputDocument();
$product->addField('id', 'black');
$product->addField('cat', 'tshirt');
$product->addField('cat', 'polo');
$product->addField('content_type', 'product');
$small = new SolrInputDocument();
$small->addField('id', 'TS-BLK-S');
$small->addField('content_type', 'sku');
$small->addField('size', 'S');
$small->addField('inventory', 100);
$medium = new SolrInputDocument();
$medium->addField('id', 'TS-BLK-M');
$medium->addField('content_type', 'sku');
$medium->addField('size', 'M');
$medium->addField('inventory', 200);
$large = new SolrInputDocument();
$large->addField('id', 'TS-BLK-L');
$large->addField('content_type', 'sku');
$large->addField('size', 'L');
$large->addField('inventory', 300);
// add child documents
$product->addChildDocument($small);
$product->addChildDocument($medium);
$product->addChildDocument($large);
// or
// $skus = [$small, $medium, $large];
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$query = new SolrQuery('*:*');
$collapseFunction = new SolrCollapseFunction('manu_id_s');
$collapseFunction->setSize(2)->setNullPolicy(SolrCollapseFunction::NULLPOLICY_IGNORE);
// $collapseFunction->setMax('sum(cscore(),field(A))');
$query->collapse($collapseFunction)->setRows(4);
$queryResponse = $client->query($query);
$response = $queryResponse->getResponse();
print_r($response);
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$updateResponse = $client->addDocument($doc);
print_r($updateResponse->getResponse());
/* you will have to commit changes to be written if you didn't use $commitWithin */
$client->commit();
示例#20
0
<?php

$core = 'techknowledgy_core';
#$core = 'demo';
$options = array('hostname' => 'localhost', 'port' => 8983, 'timeout' => 10, 'path' => '/solr/' . $core);
$client = new SolrClient($options);
if (!$client->ping()) {
    exit('Solr service not responding.');
} else {
    print "Worked!";
}
# CODE FOR CLEARING CORE OF ALL DOCS ===>
$deleteResponse = $client->deleteByQuery("*.htm*");
$client->commit(true);
print_r($deleteResponse->getResponse());
# <=== END OF CODE FOR CLEARING CORE OF ALL DOCS
# CODE FOR ADDING NEW DOC ===>
#$doc = new SolrInputDocument();
#$doc->addField('id', '090945');
#$doc->addField('link', 'www.test.com');
#$doc->addField('text', 'this is another test');
#$updateResponse = $client->addDocument($doc);
#$client->commit(true);
#print_r($updateResponse->getResponse());
# <=== END OF ADDING NEW DOC CODE
# QUERYING DOCUMENTS CODE ===>
#$query = new SolrQuery();
#$query->setQuery('technology');
#$query->setStart(0);
#$query->setRows(50);
#$query->addField('url')->addField('title')->addField('host')->addField('content');
示例#21
0
 public function addListKeywordtoSolr()
 {
     try {
         $keywords = $this->m_backend->jqxGets('keywords');
         if (empty($keywords) === FALSE) {
             $this->config->load('solr');
             $solr = $this->config->item('solr');
             $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords');
             $client = new SolrClient($options);
             foreach ($keywords as $keyword) {
                 $doc = new SolrInputDocument();
                 $doc->addField('id', $keyword['id']);
                 $doc->addField('name', $keyword['name']);
                 $doc->addField('alias', $keyword['alias']);
                 $doc->addField('id_news', $keyword['id_news']);
                 $doc->addField('type', $keyword['type']);
                 $doc->addField('priority', $keyword['priority']);
                 $updateResponse = $client->addDocument($doc);
                 $client->commit();
             }
         } else {
             return FALSE;
         }
     } catch (Exception $e) {
         return FALSE;
     }
 }
示例#22
0
<?php

define('SOLR_SERVER_HOSTNAME', 'localhost');
define('SOLR_SERVER_PORT', '8983');
define('SOLR_SERVER_PATH', 'solr/europeana');
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$client->deleteByQuery("*:*");
$client->commit();
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$response = $client->getByIds(['GB18030TEST', '6H500F0']);
print_r($response->getResponse());
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$doc2 = clone $doc;
$doc2->deleteField('id');
$doc2->addField('id', 334456);
$docs = array($doc, $doc2);
$updateResponse = $client->addDocuments($docs, true, 1024);
print $updateResponse->getRawRequest();
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
// No need to call commit() because $commitWithin is passed, so Solr Server will auto commit within 10 seconds
$updateResponse = $client->addDocument($doc, false, 10000);
print_r($updateResponse->getResponse());
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$query_response = $client->system();
$response = $query_response->getResponse();
print_r($response);
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$response = $client->getByIds(['GB18030TEST', '6H500F0']);
$client = new SolrClient($options);
$response = $client->getById('GB18030TEST');
print_r($response->getResponse());
示例#28
0
 public function deleteKeywordsintoSolrDocument($id_news, $type)
 {
     try {
         $this->config->load('solr');
         $solr = $this->config->item('solr');
         $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords');
         $client = new SolrClient($options);
         $client->deleteByQuery("id_news:{$id_news} AND type:{$type}");
         return $client->commit();
     } catch (Exception $e) {
         return FALSE;
     }
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$disMaxQuery = new SolrDisMaxQuery();
$disMaxQuery->setStart(0)->setQuery('solr')->setTimeAllowed(500)->setRows(17);
$disMaxQuery->addSortField('price', 0);
$disMaxQuery->setQueryAlt('*:*')->addQueryField('text', 1.5)->addQueryField('title', 5)->addBoostQuery('cat', 'electronics', 2)->setQueryPhraseSlop(1)->setBigramPhraseSlop(3)->addBigramPhraseField('text', 2.5)->addBigramPhraseField('type', 3, 4)->setTrigramPhraseFields('content^0.5 anchor~4^1.5 title^1.2 site^1.5');
$queryResponse = $client->query($disMaxQuery);
$response = $queryResponse->getResponse();
if ($response->response->numFound > 0) {
    $docs = $response->response->docs;
    print_r($docs);
} else {
    echo "No Documents Found" . PHP_EOL;
}