Skip to content

Commit

Permalink
Add Solr CLI search tool
Browse files Browse the repository at this point in the history
Adding a WIP solr CLI search tool to search solr. Deleted the
experimental solr html search page since it is no longer needed.
  • Loading branch information
anvit committed May 16, 2024
1 parent 9297377 commit c9920bd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 457 deletions.
81 changes: 81 additions & 0 deletions lib/task/search/arSolrSearchTask.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Access to Memory (AtoM) software.
*
* Access to Memory (AtoM) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Access to Memory (AtoM) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Populate search index.
*/
class arSolrSearchTask extends sfBaseTask
{
public function execute($arguments = [], $options = [])
{
sfContext::createInstance($this->configuration);
sfConfig::add(QubitSetting::getSettingsArray());

new sfDatabaseManager($this->configuration);

$solr = new arSolrPlugin($options);

$client = $solr->getClient();
if (!$arguments['query']) {
$this->log('Please specify a search query.');
} else {
$this->runSolrQuery($client, $arguments['query']);
}
}

protected function configure()
{
$this->addArguments([
new sfCommandArgument('query', sfCommandArgument::OPTIONAL, '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'),
]);

$this->namespace = 'solr';
$this->name = 'search';

$this->briefDescription = 'Search the search index for a result';
$this->detailedDescription = <<<'EOF'
The [solr:search] task runs a search query on solr. Usage:
php symfony solr:search <query>
EOF;
}

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

$query->setStart(0);
$query->setRows(1000);

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

$response = $searchResponse->getResponse()->response;
if ($response->docs) {
foreach ($response->docs as $resp) {
$this->log(print_r($resp, true));
}
} else {
$this->log("No results found");
}
}
}
14 changes: 14 additions & 0 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ public function addDocument($data, $type)
unset($data['id']);
}

public function getClient() {
return $this->client;
}

/**
* Initialize Solr index if it does not exist.
*/
Expand All @@ -314,8 +318,17 @@ protected function initialize()
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

$solrClientOptions = [
'hostname' => $this->solrClientOptions['hostname'],
'login' => $this->solrClientOptions['username'],
'password' => $this->solrClientOptions['password'],
'port' => $this->solrClientOptions['port'],
'path' => '/solr/'.$this->solrClientOptions['collection'],
];

if (array_search($this->solrClientOptions['collection'], $response->collections) !== false) {
$this->log("Collection found. Not initializing");
$this->client = new SolrClient($solrClientOptions);
} else {
$this->log('Initializing Solr Index');
if (
Expand Down Expand Up @@ -390,6 +403,7 @@ protected function initialize()
}
}
}
$this->client = new SolrClient($solrClientOptions);
}
}

Expand Down
58 changes: 0 additions & 58 deletions solr/bootstrap.php

This file was deleted.

7 changes: 0 additions & 7 deletions solr/composer.json

This file was deleted.

Loading

0 comments on commit c9920bd

Please sign in to comment.