From 8311a92af38845643aee2f0204590e826c14457e Mon Sep 17 00:00:00 2001 From: Anvit Srivastav Date: Fri, 5 Jul 2024 17:01:57 -0700 Subject: [PATCH] Update arSolrBoolQuery (WIP) Updated arSolrBoolQuery to function with arSolrQuery, and added rudimentary support for it in arSolrSearchTask --- lib/task/search/arSolrSearchTask.class.php | 43 +++++++++----- .../arSolrPlugin/lib/arSolrPlugin.class.php | 33 ++++++----- .../lib/query/arSolrBoolQuery.class.php | 56 +++++++++++-------- .../lib/query/arSolrQuery.class.php | 15 +++++ 4 files changed, 93 insertions(+), 54 deletions(-) diff --git a/lib/task/search/arSolrSearchTask.class.php b/lib/task/search/arSolrSearchTask.class.php index f1c3b7bc58..00c4300ca5 100644 --- a/lib/task/search/arSolrSearchTask.class.php +++ b/lib/task/search/arSolrSearchTask.class.php @@ -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); diff --git a/plugins/arSolrPlugin/lib/arSolrPlugin.class.php b/plugins/arSolrPlugin/lib/arSolrPlugin.class.php index 682709ea4a..1780a66018 100644 --- a/plugins/arSolrPlugin/lib/arSolrPlugin.class.php +++ b/plugins/arSolrPlugin/lib/arSolrPlugin.class.php @@ -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. * @@ -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. */ @@ -252,9 +252,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); @@ -327,19 +324,20 @@ protected function initialize() } } - private function addAutoComplete() { + private function addAutoComplete() + { // list should also include QubitInformationObject.referenceCode but // 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) { @@ -414,7 +412,8 @@ private function addSubProperties($properties, &$propertyFields, $parentType = ' } } - private function setLanguageType($fieldName) { + private function setLanguageType($fieldName) + { $substrings = explode('.', $fieldName); $lang = $substrings[count($substrings) - 2]; diff --git a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php index d53bdade39..8f92d285a3 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php @@ -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; } /** @@ -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); diff --git a/plugins/arSolrPlugin/lib/query/arSolrQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrQuery.class.php index aef3f2c2e9..9488f29dc9 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrQuery.class.php @@ -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();