/** * @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; }
/** * 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 $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');