Skip to content

Commit

Permalink
feat(Solr): Removed $proximity argument from search and `searchWi…
Browse files Browse the repository at this point in the history
…thHighlight` SearchHandlerInterface methods

BREAKING CHANGE: Removed `$proximity` argument from `search` and `searchWithHighlight` SearchHandlerInterface methods
  • Loading branch information
ambroisemaupate committed Apr 11, 2024
1 parent c74dc6f commit e0e36b5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function handle(bool $disabled = false)
$this->criteria, # a simple criteria array to filter search results
$this->getItemPerPage(), # result count
$this->searchInTags, # Search in tags too,
1,
$this->getPage()
);
} else {
Expand All @@ -59,7 +58,6 @@ public function handle(bool $disabled = false)
$this->criteria, # a simple criteria array to filter search results
$this->getItemPerPage(), # result count
$this->searchInTags, # Search in tags too,
2,
$this->getPage()
);
}
Expand Down
55 changes: 38 additions & 17 deletions lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function getSolr(): Client
* @param array $args
* @param int $rows
* @param bool $searchTags Search in tags/folders too, even if a node don’t match
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away.
* @param int $page
*
* @return SearchResultsInterface Return a SearchResultsInterface iterable object.
Expand All @@ -64,14 +63,13 @@ public function searchWithHighlight(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): SearchResultsInterface {
$args = $this->argFqProcess($args);
$args["fq"][] = "document_type_s:" . $this->getDocumentType();
$args["hl.q"] = $this->escapeQuery(trim($q));
$args["hl.q"] = $this->buildHighlightingQuery($q);
$args = array_merge($this->getHighlightingOptions($args), $args);
$response = $this->nativeSearch($q, $args, $rows, $searchTags, $proximity, $page);
$response = $this->nativeSearch($q, $args, $rows, $searchTags, $page);
return $this->createSearchResultsFromResponse($response);
}

Expand Down Expand Up @@ -152,7 +150,6 @@ public function setHighlightingFragmentSize(int $highlightingFragmentSize): Abst
* @param array $args
* @param int $rows
* @param bool $searchTags
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away.
* @param int $page
*
* @return array|null
Expand All @@ -162,7 +159,6 @@ abstract protected function nativeSearch(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): ?array;

Expand Down Expand Up @@ -195,7 +191,6 @@ abstract protected function nativeSearch(
* @param array $args
* @param int $rows Results per page
* @param bool $searchTags Search in tags/folders too, even if a node don’t match
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away. Default 10000000
* @param int $page Retrieve a specific page
*
* @return SearchResultsInterface Return an array of doctrine Entities (Document, NodesSources)
Expand All @@ -205,15 +200,14 @@ public function search(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): SearchResultsInterface {
$args = $this->argFqProcess($args);
$args["fq"][] = "document_type_s:" . $this->getDocumentType();
$tmp = [];
$args = array_merge($tmp, $args);

$response = $this->nativeSearch($q, $args, $rows, $searchTags, $proximity, $page);
$response = $this->nativeSearch($q, $args, $rows, $searchTags, $page);
return $this->createSearchResultsFromResponse($response);
}

Expand All @@ -235,10 +229,9 @@ public function escapeQuery(string $input): string
/**
* @param string $q
* @param int $proximity
* @return array [$exactQuery, $fuzzyQuery, $wildcardQuery]
*/
protected function getFormattedQuery(string $q, int $proximity = 1): array
protected function getFormattedQuery(string $q): array
{
$q = trim($q);
/**
Expand All @@ -249,13 +242,13 @@ protected function getFormattedQuery(string $q, int $proximity = 1): array
if (false === $words) {
throw new \RuntimeException('Cannot split query string.');
}
$fuzzyiedQuery = implode(' ', array_map(function (string $word) use ($proximity) {
$fuzzyiedQuery = implode(' ', array_map(function (string $word) {
/*
* Do not fuzz short words: Solr crashes
* Proximity is set to 1 by default for single-words
*/
if (\mb_strlen($word) > 3) {
return $this->escapeQuery($word) . '~' . $proximity;
return $this->escapeQuery($word) . '~2';
}
return $this->escapeQuery($word);
}, $words));
Expand All @@ -266,7 +259,7 @@ protected function getFormattedQuery(string $q, int $proximity = 1): array
/*
* Wildcard search for allowing autocomplete
*/
$wildcardQuery = $this->escapeQuery($q) . '*~' . $proximity;
$wildcardQuery = $this->escapeQuery($q) . '*~2';
return [$exactQuery, $fuzzyiedQuery, $wildcardQuery];
}
Expand All @@ -279,15 +272,14 @@ protected function getFormattedQuery(string $q, int $proximity = 1): array
* @param string $q
* @param array $args
* @param bool $searchTags
* @param int $proximity
* @return string
*/
protected function buildQuery(string $q, array &$args, bool $searchTags = false, int $proximity = 1): string
protected function buildQuery(string $q, array &$args, bool $searchTags = false): string
{
$titleField = $this->getTitleField($args);
$collectionField = $this->getCollectionField($args);
$tagsField = $this->getTagsField($args);
[$exactQuery, $fuzzyiedQuery, $wildcardQuery] = $this->getFormattedQuery($q, $proximity);
[$exactQuery, $fuzzyiedQuery, $wildcardQuery] = $this->getFormattedQuery($q);
/*
* Search in node-sources tags name…
Expand Down Expand Up @@ -316,6 +308,35 @@ protected function buildQuery(string $q, array &$args, bool $searchTags = false,
}
}
protected function buildHighlightingQuery(string $q): string
{
$q = trim($q);
$words = preg_split('#[\s,]+#', $q, -1, PREG_SPLIT_NO_EMPTY);
if (\is_array($words) && \count($words) > 1) {
return $this->escapeQuery($q);
}
$q = $this->escapeQuery($q);
return sprintf('%s~2', $q);
}
/**
* @param array $args
* @param bool $searchTags
* @return string
*/
protected function buildQueryFields(array &$args, bool $searchTags = true): string
{
$titleField = $this->getTitleField($args);
$collectionField = $this->getCollectionField($args);
$tagsField = $this->getTagsField($args);
if ($searchTags) {
return $titleField . '^10 ' . $collectionField . '^2 ' . $tagsField . ' slug_s';
}
return $titleField . ' ' . $collectionField . ' slug_s';
}
/**
* @param string $q
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class DocumentSearchHandler extends AbstractSearchHandler
* @param array $args
* @param integer $rows
* @param bool $searchTags
* @param integer $proximity Proximity matching: Lucene supports finding words are a within a specific distance away.
* @param integer $page
*
* @return array|null
Expand All @@ -28,14 +27,13 @@ protected function nativeSearch(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): ?array {
if (empty($q)) {
return null;
}
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);
$queryTxt = $this->buildQuery($q, $args, $searchTags);
$query->setQuery($queryTxt);

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class NodeSourceSearchHandler extends AbstractSearchHandler implements NodeSourc
* @param array $args
* @param integer $rows
* @param bool $searchTags
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away.
* @param int $page
*
* @return array|null
Expand All @@ -35,14 +34,13 @@ protected function nativeSearch(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): ?array {
if (empty($q)) {
return null;
}
$query = $this->createSolrQuery($args, $rows, $page);
$queryTxt = $this->buildQuery($q, $args, $searchTags, $proximity);
$queryTxt = $this->buildQuery($q, $args, $searchTags);

if ($this->boostByPublicationDate) {
$boost = '{!boost b=recip(ms(NOW,published_at_dt),3.16e-11,1,1)}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface SearchHandlerInterface
* @param array $args
* @param int $rows Results per page
* @param bool $searchTags Search in tags/folders too, even if a node don’t match
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away. Default 10000000
* @param int $page Retrieve a specific page
*
* @return SearchResultsInterface Return an array of doctrine Entities (Document, NodesSources)
Expand All @@ -21,7 +20,6 @@ public function search(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): SearchResultsInterface;

Expand All @@ -32,7 +30,6 @@ public function search(
* @param array $args
* @param int $rows
* @param boolean $searchTags Search in tags/folders too, even if a node don’t match
* @param int $proximity Proximity matching: Lucene supports finding words are a within a specific distance away.
* @param int $page
*
* @return SearchResultsInterface Return a SearchResultsInterface iterable object.
Expand All @@ -42,7 +39,6 @@ public function searchWithHighlight(
array $args = [],
int $rows = 20,
bool $searchTags = false,
int $proximity = 1,
int $page = 1
): SearchResultsInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ protected function getSolrSearchResults(
$arrayFilter,
$this->getItemPerPage(),
true,
2,
(int) $currentPage
);
$pageCount = ceil($results->getResultCount() / $this->getItemPerPage());
Expand Down

0 comments on commit e0e36b5

Please sign in to comment.