Skip to content

Commit

Permalink
Update arSolrBoolQuery (WIP)
Browse files Browse the repository at this point in the history
Updated arSolrBoolQuery to function with arSolrQuery, and added
rudimentary support for it in arSolrSearchTask
  • Loading branch information
anvit authored and melaniekung committed Jul 8, 2024
1 parent 642fa3d commit c908ce2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 53 deletions.
43 changes: 28 additions & 15 deletions lib/task/search/arSolrSearchTask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,41 @@ protected function configure()
EOF;
}

private function createQuery($queryText, $fields)
{
$query = new arSolrQuery(arSolrPluginUtil::escapeTerm($queryText));
if ($fields) {
$fieldsArr = explode(',', $fields);
$newFields = [];
foreach ($fieldsArr as $field) {
$newField = explode('^', $field);
$fieldName = $newField[0];
$fieldBoost = $newField[1];
if (!$fieldBoost) {
$fieldBoost = 1;
}
$newFields[$fieldName] = (int) $fieldBoost;
}
$query->setFields(arSolrPluginUtil::getBoostedSearchFields($newFields));
}

return $query;
}

private function runSolrQuery($solrInstance, $queryText, $rows, $start, $fields, $type)
{
$modelType = 'QubitInformationObject';
if ('matchall' === $type) {
$query = new arSolrMatchAllQuery();
$modelType = null;
} elseif ('bool' === $type) {
$query = new arSolrBoolQuery();
$mustClause = $this->createQuery($queryText, $fields);
$mustClause->setType($modelType);
$query->addMust($mustClause);
} else {
$query = new arSolrQuery(arSolrPluginUtil::escapeTerm($queryText));
if ($fields) {
$fieldsArr = explode(',', $fields);
$newFields = [];
foreach ($fieldsArr as $field) {
$newField = explode('^', $field);
$fieldName = $newField[0];
$fieldBoost = $newField[1];
if (!$fieldBoost) {
$fieldBoost = 1;
}
$newFields[$fieldName] = (int) $fieldBoost;
}
$query->setFields(arSolrPluginUtil::getBoostedSearchFields($newFields));
}
$query = $this->createQuery($queryText, $fields);
$query->setType($modelType);
}
$query->setSize($rows);
$query->setOffset($start);
Expand Down
30 changes: 14 additions & 16 deletions plugins/arSolrPlugin/lib/arSolrPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
class arSolrPlugin extends QubitSearchEngine
{
public $lang_codes = ['ar', 'hy', 'ba', 'br', 'bg', 'ca', 'cz', 'da', 'nl', 'en', 'fi', 'fr', 'gl', 'ge', 'el', 'hi', 'hu', 'id', 'it', 'no', 'fa', 'pt', 'ro', 'ru', 'es', 'sv', 'tr'];

public $langs = [];

/**
* Mappings configuration, mapping.yml.
*
Expand All @@ -36,10 +40,6 @@ class arSolrPlugin extends QubitSearchEngine
*/
protected $enabled = true;

public $lang_codes = ['ar','hy','ba','br','bg','ca','cz','da','nl','en','fi','fr','gl','ge','el','hi','hu','id','it','no','fa','pt','ro','ru','es','sv','tr'];

public $langs = [];

/**
* Constructor.
*/
Expand Down Expand Up @@ -254,9 +254,6 @@ public function getSolrCollection()
public function search($query, $type)
{
$url = $this->getSolrUrl().'/solr/'.$this->getSolrCollection().'/query';
if ($type) {
$query->setType($type);
}
$response = arSolrPlugin::makeHttpRequest($url, 'POST', json_encode($query->getQueryParams()));

return new arSolrResultSet($response);
Expand Down Expand Up @@ -334,14 +331,14 @@ private function addAutoCompleteFields()
// since this field does not include a language code, it is added to
// $addCopyField manually
$autocompleteFields = [
"QubitRepository.i18n.en.authorizedFormOfName",
"QubitInformationObject.aip.type.i18n.en.name",
"QubitInformationObject.i18n.en.title",
"QubitActor.i18n.en.authorizedFormOfName",
"QubitActor.places.i18n.en.name",
"QubitActor.subjects.i18n.en.name",
"QubitTerm.i18n.en.name",
"QubitAip.type.i18n.en.name"
'QubitRepository.i18n.en.authorizedFormOfName',
'QubitInformationObject.aip.type.i18n.en.name',
'QubitInformationObject.i18n.en.title',
'QubitActor.i18n.en.authorizedFormOfName',
'QubitActor.places.i18n.en.name',
'QubitActor.subjects.i18n.en.name',
'QubitTerm.i18n.en.name',
'QubitAip.type.i18n.en.name',
];

foreach ($this->langs as $lang) {
Expand Down Expand Up @@ -421,7 +418,8 @@ private function addSubProperties($properties, &$propertyFields, $parentType = '
}
}

private function setLanguageType($fieldName) {
private function setLanguageType($fieldName)
{
$substrings = explode('.', $fieldName);
$lang = $substrings[count($substrings) - 2];

Expand Down
56 changes: 34 additions & 22 deletions plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,44 @@ class arSolrBoolQuery extends arSolrAbstractQuery
*
* @return arSolrBoolQuery object
*/
public function generateBoolQuery()
public function generateQueryParams()
{
$params = $this->getParams();

$mustQuery = [
'must' => [
[
'dismax' => [
'qf' => implode(' ', $params['fields']),
'query' => $params['must'],
],
$mustQuery = [];
foreach ($params['must'] as $query) {
$mustClause = [
'edismax' => [
'query' => $query->getSearchQuery(),
'q.op' => $query->getOperator(),
'stopwords' => 'true',
'qf' => implode(' ', $query->getFields()),
],
],
];
];
array_push($mustQuery, $mustClause);
}

$mustNotQuery = [
'must_not' => [[
'query' => $params['must_not'],
]],
$mustQuery = [
'must' => $mustQuery,
];

$mmQuery = [
'qf' => $qf.'^'.$params['boost'],
'mm' => $params['minimum_should_match'],
];
if ($params['must_not']) {
$mustNotQuery = [
'must_not' => [[
'query' => $params['must_not'],
]],
];
}

$boolQuery = [
'query' => [
'bool' => array_merge($mustQuery, $mustNotQuery, $mmQuery),
'bool' => $mustQuery,
],
'offset' => $this->offset,
'limit' => $this->size,
];

return $params;
$this->query = $boolQuery;
}

/**
Expand Down Expand Up @@ -121,10 +126,17 @@ public function setMinimumShouldMatch($minimum): self
return $this->setParam('minimum_should_match', $minimum);
}

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

return $this->query;
}

protected function _addQuery(string $type, $args): self
{
if (!\is_array($args) && !($args instanceof arSolrAbstractQuery)) {
throw new Exception('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractQuery');
if (!\is_array($args) && !($args instanceof arSolrQuery)) {
throw new Exception('Invalid parameter. Has to be array or instance of arSolrAbstractQuery');
}

return $this->addParam($type, $args);
Expand Down
15 changes: 15 additions & 0 deletions plugins/arSolrPlugin/lib/query/arSolrQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ public function setSearchQuery($searchQuery)
$this->searchQuery = $searchQuery;
}

public function getFields()
{
return $this->fields;
}

public function getOperator()
{
return $this->operator;
}

public function getSearchQuery()
{
return $this->searchQuery;
}

public function getQueryParams()
{
$this->generateQueryParams();
Expand Down

0 comments on commit c908ce2

Please sign in to comment.