Skip to content

Commit

Permalink
Add Solr Query class
Browse files Browse the repository at this point in the history
Added a class to handles Solr Queries and its parameters
  • Loading branch information
anvit committed May 23, 2024
1 parent db7a3b7 commit bfeeaad
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 36 deletions.
33 changes: 9 additions & 24 deletions lib/task/search/arSolrSearchTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ protected function configure()

private function runSolrQuery($solrInstance, $queryText, $rows, $start, $fields)
{
$url = $solrInstance->getSolrUrl().'/solr/'.$solrInstance->getSolrCollection().'/select';
if (!$fields) {
$fields = arSolrPluginUtil::getBoostedSearchFields([
'identifier' => 10,
Expand All @@ -75,46 +74,32 @@ private function runSolrQuery($solrInstance, $queryText, $rows, $start, $fields)
'i18n.en.processingNotes' => 5,
'i18n.en.sourceOfAcquisition' => 5,
'i18n.en.archivalHistory' => 5,
//'i18n.en.appraisal' => 1,
'i18n.en.physicalCharacteristics' => 1,
'i18n.en.receivedExtentUnits' => 1,
//'alternativeIdentifiers.i18n.en.name' => 1,
'creators.i18n.en.authorizedFormOfName' => 1,
//'alternativeIdentifiers.i18n.en.note' => 1,
//'alternativeIdentifiers.type.i18n.en.name' => 1,
//'accessionEvents.i18n.en.agent' => 1,
//'accessionEvents.type.i18n.en.name' => 1,
//'accessionEvents.notes.i18n.%s.content' => 1,
'donors.contactInformations.contactPerson' => 1,
'accessionEvents.dateString' => 1,
]);
} else {
$fields = arSolrPluginUtil::getBoostedSearchFields($fields);
}

$queryParams = [
'params' => [
'start' => $start,
'rows' => $rows,
'q.op' => 'AND',
'defType' => 'edismax',
'stopwords' => 'true',
'q' => $queryText,
'qf' => implode(' ', $fields),
],
];
$query = new arSolrQuery(arSolrPluginUtil::escapeTerm($queryText));
$query->setFields($fields);
$query->setSize($rows);
$query->setOffset($start);

$response = arSolrPlugin::makeHttpRequest($url.$query, 'POST', json_encode($queryParams));

$docs = $response->response->docs;
$docs = $solrInstance->search($query);
if ($docs) {
foreach ($docs as $resp) {
$this->log(sprintf('%s - %s', $resp->id, $resp->{'i18n.en.title'}[0]));
if (!$resp->{'i18n.en.title'}[0]) {
$this->log(print_r($resp, true));
}
}
} else {
$this->log('No results found');
$this->log(print_r($response->response, true));
$this->log(print_r($queryParams, true));
$this->log(print_r($docs, true));
}
}
}
28 changes: 16 additions & 12 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
*/
class arSolrPlugin extends QubitSearchEngine
{
/**
* Elastic_Index object.
*
* @var mixed defaults to null
*/
public $index;

/**
* Mappings configuration, mapping.yml.
*
Expand Down Expand Up @@ -281,6 +274,14 @@ public function getSolrCollection()
return $this->solrClientOptions['collection'];
}

public function search($query)
{
$url = $this->getSolrUrl().'/solr/'.$this->getSolrCollection().'/select';
$response = arSolrPlugin::makeHttpRequest($url, 'POST', json_encode($query->getQueryParams()));

return $response->response->docs;
}

public static function makeHttpRequest($url, $method = 'GET', $body = null)
{
$options = [
Expand Down Expand Up @@ -380,14 +381,14 @@ protected function initialize()
$addFieldQuery .= $q[0];
$addCopyFieldQuery .= $q[1];
}
} else if (null != $value['type']) {
} elseif (null != $value['type']) {
$fields .= '"'.$key.':/'.$key.'",';
$q = $this->getFieldQuery($key, $this->setType($value['type']), false, $includeInCopy);
$addFieldQuery .= $q[0];
$addCopyFieldQuery .= $q[1];
}
}
$fields = rtrim($fields, ',') . ']';
$fields = rtrim($fields, ',').']';
$this->defineConfigParams($key, $fields);
}
}
Expand All @@ -402,7 +403,8 @@ protected function initialize()
}
}

private function addNestedFields($key, $properties) {
private function addNestedFields($key, $properties)
{
foreach ($properties as $k => $v) {
if (null === $v['type']) {
$this->addNestedFields($k, $v['properties']);
Expand All @@ -413,7 +415,8 @@ private function addNestedFields($key, $properties) {
}
}

private function defineConfigParams($name, $fields) {
private function defineConfigParams($name, $fields)
{
$url = $this->solrBaseUrl.'/solr/'.$this->solrClientOptions['collection'].'/config/params';
$query = '"set": {"'.$name.'": {"split": "/'.$name.'", "f":'.$fields.'}}';
arSolrPlugin::makeHttpRequest($url, 'POST', $query);
Expand All @@ -439,6 +442,7 @@ private function setType($type)
if ('geo_point' === $type) {
return 'location';
}

return $type;
}

Expand All @@ -452,7 +456,7 @@ private function getFieldQuery($field, $type, $multiValue, $includeInCopy = true
}
$this->log(sprintf('Defining mapping %s...', $field));

return [$addFieldQuery. $addCopyFieldQuery];
return [$addFieldQuery.$addCopyFieldQuery];
}

private function addFieldsToType($query)
Expand Down
147 changes: 147 additions & 0 deletions plugins/arSolrPlugin/lib/arSolrQuery.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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/>.
*/

/**
* arSolrQuery.
*/
class arSolrQuery
{
/**
* Array of fields to be queried.
*
* @var array
*/
protected $fields;

/**
* Default operator.
*
* @var string defaults to 'AND'
*/
protected $operator = 'AND';

/**
* Search query.
*
* @var string
*/
protected $searchQuery = '*:*';

/**
* Query Params.
*
* @var mixed
*/
protected $query;

/**
* Number of results to fetch.
*
* @var number defaults to 10
*/
protected $size = 10;

/**
* Offset for search results.
*
* @var number defaults to 0
*/
protected $offset = 0;

/**
* Constructor.
*
* @param mixed $searchQuery
*/
public function __construct($searchQuery)
{
if (!$this->fields) {
$fields = arSolrPluginUtil::getBoostedSearchFields([
'identifier' => 10,
'donors.i18n.%s.authorizedFormOfName' => 10,
'i18n.%s.title' => 10,
'i18n.%s.scopeAndContent' => 10,
'i18n.%s.locationInformation' => 5,
'i18n.%s.processingNotes' => 5,
'i18n.%s.sourceOfAcquisition' => 5,
'i18n.%s.archivalHistory' => 5,
'i18n.%s.appraisal' => 1,
'i18n.%s.physicalCharacteristics' => 1,
'i18n.%s.receivedExtentUnits' => 1,
'alternativeIdentifiers.i18n.%s.name' => 1,
'creators.i18n.%s.authorizedFormOfName' => 1,
'alternativeIdentifiers.i18n.%s.note' => 1,
'alternativeIdentifiers.type.i18n.%s.name' => 1,
'accessionEvents.i18n.%s.agent' => 1,
'accessionEvents.type.i18n.%s.name' => 1,
'accessionEvents.notes.i18n.%s.content' => 1,
'donors.contactInformations.contactPerson' => 1,
'accessionEvents.dateString' => 1,
]);
}
$this->setSearchQuery($searchQuery);
$this->generateQueryParams();
}

public function setSize($size)
{
$this->size = $size;
}

public function setOffset($offset)
{
$this->offset = $offset;
}

public function setFields($fields)
{
$this->fields = $fields;
}

public function setDefaultOperator($operator)
{
$this->operator = $operator;
}

public function setSearchQuery($searchQuery)
{
$this->searchQuery = $searchQuery;
}

public function getQueryParams()
{
$this->generateQueryParams();
return $this->query;
}

public function generateQueryParams()
{
$this->query = [
'params' => [
'start' => $this->offset,
'rows' => $this->size,
'q.op' => $this->operator,
'defType' => 'dismax',
'stopwords' => 'true',
'q' => $this->searchQuery,
'qf' => implode(' ', $this->fields),
],
];
}
}

0 comments on commit bfeeaad

Please sign in to comment.