Skip to content

Commit

Permalink
fix(Solr): Added a new wildcardQuery to search and autocomplete at th…
Browse files Browse the repository at this point in the history
…e same time
  • Loading branch information
ambroisemaupate committed May 5, 2023
1 parent d236f1a commit 37746af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
45 changes: 31 additions & 14 deletions lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,14 @@ public function escapeQuery(string $input): string
}
/**
* Default Solr query builder.
*
* Extends this method to customize your Solr queries. Eg. to boost custom fields.
*
* @param string $q
* @param array $args
* @param bool $searchTags
* @param int $proximity
* @return string
* @return array [$exactQuery, $fuzzyQuery, $wildcardQuery]
*/
protected function buildQuery(string $q, array &$args, bool $searchTags = false, int $proximity = 1): string
protected function getFormattedQuery(string $q, int $proximity = 1): array
{
$q = trim($q);
$singleWord = $this->isQuerySingleWord($q);
$titleField = $this->getTitleField($args);
$collectionField = $this->getCollectionField($args);
$tagsField = $this->getTagsField($args);
/**
* Generate a fuzzy query by appending proximity to each word
* @see https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html#TheStandardQueryParser-FuzzySearches
Expand All @@ -272,26 +262,53 @@ protected function buildQuery(string $q, array &$args, bool $searchTags = false,
*/
$exactQuery = '"' . $exactQuery . '"';
}
/*
* Wildcard search for allowing autocomplete
*/
$wildcardQuery = $this->escapeQuery($q) . '*~' . $proximity;

return [$exactQuery, $fuzzyiedQuery, $wildcardQuery];
}

/**
* Default Solr query builder.
*
* Extends this method to customize your Solr queries. Eg. to boost custom fields.
*
* @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
{
$titleField = $this->getTitleField($args);
$collectionField = $this->getCollectionField($args);
$tagsField = $this->getTagsField($args);
[$exactQuery, $fuzzyiedQuery, $wildcardQuery] = $this->getFormattedQuery($q, $proximity);

/*
* Search in node-sources tags name…
*/
if ($searchTags) {
// Need to use Fuzzy search AND Exact search
return sprintf(
'(' . $titleField . ':%s)^10 (' . $titleField . ':%s) (' . $collectionField . ':%s)^2 (' . $collectionField . ':%s) (' . $tagsField . ':%s) (' . $tagsField . ':%s)',
'(' . $titleField . ':%s)^10 (' . $titleField . ':%s) (' . $titleField . ':%s) (' . $collectionField . ':%s)^2 (' . $collectionField . ':%s) (' . $tagsField . ':%s) (' . $tagsField . ':%s)',
$exactQuery,
$fuzzyiedQuery,
$wildcardQuery,
$exactQuery,
$fuzzyiedQuery,
$exactQuery,
$fuzzyiedQuery
);
} else {
return sprintf(
'(' . $titleField . ':%s)^10 (' . $titleField . ':%s) (' . $collectionField . ':%s)^2 (' . $collectionField . ':%s)',
'(' . $titleField . ':%s)^10 (' . $titleField . ':%s) (' . $titleField . ':%s) (' . $collectionField . ':%s)^2 (' . $collectionField . ':%s)',
$exactQuery,
$fuzzyiedQuery,
$wildcardQuery,
$exactQuery,
$fuzzyiedQuery
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Themes\Rozier\AjaxControllers;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
Expand Down Expand Up @@ -190,7 +191,7 @@ protected function getSolrSearchResults(
$arrayFilter,
$this->getItemPerPage(),
true,
10000000,
2,
(int) $currentPage
);
$pageCount = ceil($results->getResultCount() / $this->getItemPerPage());
Expand All @@ -216,6 +217,7 @@ protected function getSolrSearchResults(
*
* @param Request $request
* @return JsonResponse
* @throws NotSupported
*/
public function listAction(Request $request): JsonResponse
{
Expand Down

0 comments on commit 37746af

Please sign in to comment.