From e93aad87d16e7df277b218badd3e3edde0a1866d Mon Sep 17 00:00:00 2001 From: Anvit Srivastav Date: Tue, 14 May 2024 16:58:29 -0700 Subject: [PATCH] Clean up Solr HTTP requests Added a helper method for all the Solr HTTP requests and cleaned up the plugin code making the requests. --- lib/search/QubitSearch.class.php | 4 - lib/task/search/arSolrSearchTask.class.php | 15 ++- .../arSolrPlugin/lib/arSolrPlugin.class.php | 105 ++++++------------ 3 files changed, 42 insertions(+), 82 deletions(-) diff --git a/lib/search/QubitSearch.class.php b/lib/search/QubitSearch.class.php index 31ac20aee0..3feaede32d 100644 --- a/lib/search/QubitSearch.class.php +++ b/lib/search/QubitSearch.class.php @@ -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; diff --git a/lib/task/search/arSolrSearchTask.class.php b/lib/task/search/arSolrSearchTask.class.php index 09b01ce2e1..0ed09e04b4 100644 --- a/lib/task/search/arSolrSearchTask.class.php +++ b/lib/task/search/arSolrSearchTask.class.php @@ -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'; @@ -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"); diff --git a/plugins/arSolrPlugin/lib/arSolrPlugin.class.php b/plugins/arSolrPlugin/lib/arSolrPlugin.class.php index b971d1c76b..28d15b5f6c 100644 --- a/plugins/arSolrPlugin/lib/arSolrPlugin.class.php +++ b/plugins/arSolrPlugin/lib/arSolrPlugin.class.php @@ -18,7 +18,7 @@ */ /** - * arElasticSearchPlugin main class. + * arSolrPlugin main class. * * @author MJ Suhonos * @author Jesús García Crespo @@ -26,7 +26,7 @@ class arSolrPlugin extends QubitSearchEngine { /** - * Elastic_Client object. + * SolrClient object. * * @var mixed defaults to null */ @@ -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(); } @@ -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) { } @@ -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']); } @@ -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'], @@ -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(); @@ -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 = ""; @@ -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)); } @@ -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; + } }