Skip to content

Commit

Permalink
[FEATURE] Add more search operators and fix highlighting (#948)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kubina <[email protected]>
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
3 people authored Jun 26, 2023
1 parent d0e29da commit 72acb39
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
15 changes: 5 additions & 10 deletions Classes/Common/Solr.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function createCore($core = '')
}

/**
* Escape all special characters in a query string
* Escape special characters in a query string
*
* @access public
*
Expand All @@ -189,15 +189,10 @@ public static function createCore($core = '')
public static function escapeQuery($query)
{
$helper = GeneralUtility::makeInstance(\Solarium\Core\Query\Helper::class);
// Escape query phrase or term.
if (preg_match('/^".*"$/', $query)) {
return $helper->escapePhrase(trim($query, '"'));
} else {
// Using a modified escape function here to retain whitespace, '*' and '?' for search truncation.
// @see https://github.com/solariumphp/solarium/blob/5.x/src/Core/Query/Helper.php#L70 for reference
/* return $helper->escapeTerm($query); */
return preg_replace('/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|:|\/|\\\)/', '\\\$1', $query);
}
// Escape query by dissallowing range and field operators
// Permit operators: wildcard, boolean, fuzzy, proximity, boost, grouping
// https://solr.apache.org/guide/solr/latest/query-guide/standard-query-parser.html
return preg_replace('/(\{|}|\[|]|:|\/|\\\)/', '\\\$1', $query);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion Classes/Common/SolrSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ public function submit($start, $rows, $processResults = true)
if ($this->searchParams['fulltext'] == '1') {
$searchResult['snippet'] = $doc['snippet'];
$searchResult['highlight'] = $doc['highlight'];
$searchResult['highlight_word'] = $this->searchParams['query'];
$searchResult['highlight_word'] = preg_replace('/^;|;$/', '', // remove ; at beginning or end
preg_replace('/;+/', ';', // replace any multiple of ; with a single ;
preg_replace('/[{~\d*}{\s+}{^=*\d+.*\d*}`~!@#$%\^&*()_|+-=?;:\'",.<>\{\}\[\]\\\]/', ';', $this->searchParams['query']))); // replace search operators and special characters with ;
}
$documents[$doc['uid']]['searchResults'][] = $searchResult;
}
Expand Down Expand Up @@ -521,6 +523,10 @@ protected function searchSolr($parameters = [], $enableCache = true)

// Perform search for all documents with the same uid that either fit to the search or marked as toplevel.
$response = $solr->service->executeRequest($solrRequest);
// return empty resultSet on error-response
if ($response->getStatusCode() == "400") {
return $resultSet;
}
$result = $solr->service->createResult($selectQuery, $response);

$uidGroup = $result->getGrouping()->getGroup('uid');
Expand Down
7 changes: 7 additions & 0 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ public function makeFacetsMenuArray($facets)
$search['params']['query'] = $search['query'];
// Perform search.
$selectQuery = $solr->service->createSelect($search['params']);
// check for solr response
$solrRequest = $solr->service->createRequest($selectQuery);
$response = $solr->service->executeRequest($solrRequest);
// return empty facet on solr error
if ($response->getStatusCode() == "400") {
return [];
}
$results = $solr->service->select($selectQuery);
$facet = $results->getFacetSet();

Expand Down
6 changes: 6 additions & 0 deletions Resources/Public/JavaScript/PageView/PageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ dlfViewer.prototype.displayHighlightWord = function(highlightWords = null) {
this.highlightWords = highlightWords;
}

// exctract highlighWords from URL
if (this.highlightWords === null) {
var urlParams = dlfUtils.getUrlParams();
this.highlightWords = urlParams['tx_dlf[highlight_word]'];
}

if (!dlfUtils.exists(this.highlightLayer)) {

this.highlightLayer = new ol.layer.Vector({
Expand Down

0 comments on commit 72acb39

Please sign in to comment.