diff --git a/lib/RoadizCoreBundle/src/Api/ListManager/SolrSearchListManager.php b/lib/RoadizCoreBundle/src/Api/ListManager/SolrSearchListManager.php index 168eed4a..d4c6e75f 100644 --- a/lib/RoadizCoreBundle/src/Api/ListManager/SolrSearchListManager.php +++ b/lib/RoadizCoreBundle/src/Api/ListManager/SolrSearchListManager.php @@ -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 { @@ -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() ); } diff --git a/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php b/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php index 9e12ac68..133f3691 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/AbstractSearchHandler.php @@ -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. @@ -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); } @@ -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 @@ -162,7 +159,6 @@ abstract protected function nativeSearch( array $args = [], int $rows = 20, bool $searchTags = false, - int $proximity = 1, int $page = 1 ): ?array; @@ -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) @@ -205,7 +200,6 @@ public function search( array $args = [], int $rows = 20, bool $searchTags = false, - int $proximity = 1, int $page = 1 ): SearchResultsInterface { $args = $this->argFqProcess($args); @@ -213,7 +207,7 @@ public function search( $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); } @@ -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); /** @@ -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)); @@ -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]; } @@ -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… @@ -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 * diff --git a/lib/RoadizCoreBundle/src/SearchEngine/DocumentSearchHandler.php b/lib/RoadizCoreBundle/src/SearchEngine/DocumentSearchHandler.php index 58cb4084..eb83052a 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/DocumentSearchHandler.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/DocumentSearchHandler.php @@ -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 @@ -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); /* diff --git a/lib/RoadizCoreBundle/src/SearchEngine/NodeSourceSearchHandler.php b/lib/RoadizCoreBundle/src/SearchEngine/NodeSourceSearchHandler.php index bc4dc90d..56c6a166 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/NodeSourceSearchHandler.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/NodeSourceSearchHandler.php @@ -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 @@ -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)}'; diff --git a/lib/RoadizCoreBundle/src/SearchEngine/SearchHandlerInterface.php b/lib/RoadizCoreBundle/src/SearchEngine/SearchHandlerInterface.php index 373c3a66..9612829c 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/SearchHandlerInterface.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/SearchHandlerInterface.php @@ -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) @@ -21,7 +20,6 @@ public function search( array $args = [], int $rows = 20, bool $searchTags = false, - int $proximity = 1, int $page = 1 ): SearchResultsInterface; @@ -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. @@ -42,7 +39,6 @@ public function searchWithHighlight( array $args = [], int $rows = 20, bool $searchTags = false, - int $proximity = 1, int $page = 1 ): SearchResultsInterface; diff --git a/lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php b/lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php index 0b99e28f..7efcfcd4 100644 --- a/lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php +++ b/lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php @@ -185,7 +185,6 @@ protected function getSolrSearchResults( $arrayFilter, $this->getItemPerPage(), true, - 2, (int) $currentPage ); $pageCount = ceil($results->getResultCount() / $this->getItemPerPage());