Skip to content

Commit

Permalink
Clean up Solr HTTP requests
Browse files Browse the repository at this point in the history
Added a helper method for all the Solr HTTP requests and cleaned up the
plugin code making the requests.
  • Loading branch information
anvit committed May 16, 2024
1 parent 7e6366e commit 47237fb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 82 deletions.
4 changes: 0 additions & 4 deletions lib/search/QubitSearch.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public static function getSolrInstance(array $options = [])

if (!isset(self::$solrInstance)) {
self::$solrInstance = new arSolrPlugin($options);
//$configuration = ProjectConfiguration::getActive();
//if ($configuration->isPluginEnabled('arSolrPlugin')) {
//self::$solr = new arSolrPlugin($options);
//}
}

return self::$solrInstance;
Expand Down
15 changes: 9 additions & 6 deletions lib/task/search/arSolrSearchTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,21 @@ public function execute($arguments = [], $options = [])
if (!$arguments['query']) {
$this->log('Please specify a search query.');
} else {
$this->runSolrQuery($client, $arguments['query']);
$this->runSolrQuery($client, $arguments['query'], $options['rows']);
}
}

protected function configure()
{
$this->addArguments([
new sfCommandArgument('query', sfCommandArgument::OPTIONAL, 'Search query.'),
new sfCommandArgument('query', sfCommandArgument::REQUIRED, 'Search query.'),
]);

$this->addOptions([
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', 'qubit'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'),
new sfCommandOption('rows', null, sfCommandOption::PARAMETER_OPTIONAL, 'Number of rows to return in the results', 5),
//new sfCommandOption('fields', null, sfCommandOption::PARAMETER_OPTIONAL, 'Fields to query("comma seperated")', null),
]);

$this->namespace = 'solr';
Expand All @@ -60,19 +62,20 @@ protected function configure()
EOF;
}

private function runSolrQuery($client, $queryText) {
private function runSolrQuery($client, $queryText, $rows) {
$query = new SolrQuery();
$query->setQuery($queryText);
$query->setQuery(arSolrPluginUtil::escapeTerm($queryText));

$query->setStart(0);
$query->setRows(1000);
$rows ? $query->setRows((int)$rows) : $query->setRows(100);

$searchResponse = $client->query($query);

$response = $searchResponse->getResponse()->response;
if ($response->docs) {
foreach ($response->docs as $resp) {
$this->log(print_r($resp, true));
//$this->log(print_r($resp, true));
$this->log(sprintf('%s - %s', $resp['id'], $resp['i18n.en.title'][0]));
}
} else {
$this->log("No results found");
Expand Down
105 changes: 33 additions & 72 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/

/**
* arElasticSearchPlugin main class.
* arSolrPlugin main class.
*
* @author MJ Suhonos <[email protected]>
* @author Jesús García Crespo <[email protected]>
*/
class arSolrPlugin extends QubitSearchEngine
{
/**
* Elastic_Client object.
* SolrClient object.
*
* @var mixed defaults to null
*/
Expand Down Expand Up @@ -86,6 +86,7 @@ public function __construct(array $options = [])
'collection' => $SOLR_COLLECTION,
'path' => '/solr/'.$SOLR_COLLECTION,
];
$this->solrBaseUrl = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'];
$this->initialize();
}

Expand Down Expand Up @@ -139,17 +140,8 @@ public function loadDiacriticsMappings()
public function flush()
{
try {
$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/solr/admin/collections?action=DELETE&name='.$this->solrClientOptions['collection'];
$options = [
'http' => [
'method' => 'GET',
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
$url = $this->solrBaseUrl.'/solr/admin/collections?action=DELETE&name='.$this->solrClientOptions['collection'];
arSolrPlugin::makeHttpRequest($url);
} catch (Exception $e) {
}

Expand Down Expand Up @@ -278,18 +270,8 @@ public function addDocument($data, $type)

$id = $data['id'];

$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/solr/'.$this->solrClientOptions['collection'].'/update/json/docs';
$options = [
'http' => [
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/update/json/docs';
arSolrPlugin::makeHttpRequest($url, 'POST', json_encode($data));

unset($data['id']);
}
Expand All @@ -306,17 +288,8 @@ protected function initialize()
if (sfConfig::get('app_diacritics')) {
$this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings();
}
$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/solr/admin/collections?action=LIST';
$options = [
'http' => [
'method' => 'GET',
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
$url = $this->solrBaseUrl.'/solr/admin/collections?action=LIST';
$response = arSolrPlugin::makeHttpRequest($url);

$solrClientOptions = [
'hostname' => $this->solrClientOptions['hostname'],
Expand Down Expand Up @@ -351,34 +324,15 @@ protected function initialize()
}

$this->log('Creating Solr Collection');
$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/solr/admin/collections?action=CREATE&name='.$this->solrClientOptions['collection'].'&numShards=2&replicationFactor=1&wt=json';
$options = [
'http' => [
'method' => 'GET',
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
$url = $this->solrBaseUrl.'/solr/admin/collections?action=CREATE&name='.$this->solrClientOptions['collection'].'&numShards=2&replicationFactor=1&wt=json';
arSolrPlugin::makeHttpRequest($url);

// Add fields to 'all' field
$this->addFieldToType('all', 'text_general', true, false, false);

$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/api/collections/'.$this->solrClientOptions['collection'].'/config/';
$url = $this->solrBaseUrl.'/api/collections/'.$this->solrClientOptions['collection'].'/config/';
$updateDefaultHandler = '{"update-requesthandler": {"name": "/select", "class": "solr.SearchHandler", "defaults": {"df": "all", "rows": 10, "echoParams": "explicit"}}}';
$options = [
'http' => [
'method' => 'POST',
'content' => $updateDefaultHandler,
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
arSolrPlugin::makeHttpRequest($url, 'POST', $updateDefaultHandler);

// Load and normalize mappings
$this->loadAndNormalizeMappings();
Expand Down Expand Up @@ -426,7 +380,7 @@ private function setType($type) {
}

private function addFieldToType($field, $type, $multiValue, $includeInCopy = true, $stored = true) {
$url = 'http://'.$this->solrClientOptions['hostname'].':'.$this->solrClientOptions['port'].'/solr/'.$this->solrClientOptions['collection'].'/schema/';
$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/schema/';
$stored = $stored ? 'true' : 'false';
$multiValue = $multiValue ? 'true' : 'false';
$addFieldQuery = "";
Expand All @@ -437,18 +391,7 @@ private function addFieldToType($field, $type, $multiValue, $includeInCopy = tru
} else {
$addFieldQuery = "{".$baseQuery."}";
}
$options = [
'http' => [
'method' => 'POST',
'content' => $addFieldQuery,
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

arSolrPlugin::makeHttpRequest($url, 'POST', $addFieldQuery);
$this->log(sprintf('Defining mapping %s...', $field));
}

Expand All @@ -460,4 +403,22 @@ private function loadAndNormalizeMappings()
$this->mappings = $mappings->asArray();
}
}

public static function makeHttpRequest($url, $method = 'GET', $body = null) {
$options = [
'http' => [
'method' => $method,
'header' => "Content-Type: application/json\r\n".
"Accept: application/json\r\n",
],
];
if ($body) {
$options['http']['content'] = $body;
}
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

return $response;
}
}

0 comments on commit 47237fb

Please sign in to comment.