From 0a65351ad783bd2b3e02302d424639c9a2e76f95 Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Mon, 12 Jul 2021 21:23:28 +0200 Subject: [PATCH 1/2] !!! BUGFIX: Exclude hidden nodes from suggestions --- Classes/Controller/SuggestController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Controller/SuggestController.php b/Classes/Controller/SuggestController.php index 2c70e78..6b72550 100644 --- a/Classes/Controller/SuggestController.php +++ b/Classes/Controller/SuggestController.php @@ -155,6 +155,7 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi 'contexts' => [ 'parent_path' => $contextNode->getPath(), 'workspace' => 'live', + 'hidden' => false, ] ] ]); From 0e0951c969305f814e24aecddf449f816e8e5685 Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Mon, 12 Jul 2021 22:11:30 +0200 Subject: [PATCH 2/2] !!! BUGFIX: Contexts are respected in a multi-site environment. Multiple contexts are treaded as disjunction. That means, nodes are suggested if they either are in the live workspace or from the requested site, which makes the site restriction worthless. This fix merges the site, workspace and hidden state into one context and uses that for filtering. Fixes: #54 --- Classes/Controller/SuggestController.php | 11 +++++++--- Classes/EelHelper/SuggestionIndexHelper.php | 21 ++++++++++++++++++- .../NodeTypes.Mixin.Suggestable.yaml | 19 ++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Classes/Controller/SuggestController.php b/Classes/Controller/SuggestController.php index 6b72550..43edfd3 100644 --- a/Classes/Controller/SuggestController.php +++ b/Classes/Controller/SuggestController.php @@ -14,6 +14,7 @@ use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Eel\ElasticSearchQueryBuilder; use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient; use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException; +use Flowpack\SearchPlugin\EelHelper\SuggestionIndexHelper; use Flowpack\SearchPlugin\Utility\SearchTerm; use Neos\Cache\Frontend\VariableFrontend; use Neos\Flow\Annotations as Flow; @@ -41,6 +42,12 @@ class SuggestController extends ActionController */ protected $elasticSearchQueryTemplateCache; + /** + * @Flow\Inject + * @var SuggestionIndexHelper + */ + protected $suggestionIndexHelper; + /** * @var array */ @@ -153,9 +160,7 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi 'fuzzy' => true, 'size' => $this->searchAsYouTypeSettings['suggestions']['size'] ?? 10, 'contexts' => [ - 'parent_path' => $contextNode->getPath(), - 'workspace' => 'live', - 'hidden' => false, + 'suggestion_context' => $this->suggestionIndexHelper->buildContext($contextNode) ] ] ]); diff --git a/Classes/EelHelper/SuggestionIndexHelper.php b/Classes/EelHelper/SuggestionIndexHelper.php index 7334189..2934d66 100644 --- a/Classes/EelHelper/SuggestionIndexHelper.php +++ b/Classes/EelHelper/SuggestionIndexHelper.php @@ -15,6 +15,7 @@ use Flowpack\SearchPlugin\Exception; use Flowpack\SearchPlugin\Utility\SearchTerm; +use Neos\ContentRepository\Domain\Model\NodeInterface; use Neos\Eel\ProtectedContextAwareInterface; use Neos\Flow\Annotations as Flow; @@ -25,13 +26,20 @@ */ class SuggestionIndexHelper implements ProtectedContextAwareInterface { + + /** + * Rhe length of '/sites/' + * @var int + */ + protected const SITES_OFFSET = 7; + /** * @param string|array $input The input to store, this can be a an array of strings or just a string. This field is mandatory. * @param int $weight A positive integer or a string containing a positive integer, which defines a weight and allows you to rank your suggestions. * @return array * @throws Exception */ - public function build($input, $weight = 1) + public function build($input, $weight = 1): array { return [ 'input' => $this->prepareInput($input), @@ -39,6 +47,17 @@ public function build($input, $weight = 1) ]; } + public function buildContext(NodeInterface $node): string + { + $siteName = substr($node->getPath(), self::SITES_OFFSET, strpos($node->getPath() . '/', '/', self::SITES_OFFSET) - self::SITES_OFFSET); + return sprintf( + '%s-%s-%s', + $siteName, + $node->getWorkspace()->getName(), + $node->isHidden() ? 'hidden' : 'visible' + ); + } + /** * @param string|array $input * @return array diff --git a/Configuration/NodeTypes.Mixin.Suggestable.yaml b/Configuration/NodeTypes.Mixin.Suggestable.yaml index f465110..9804f62 100644 --- a/Configuration/NodeTypes.Mixin.Suggestable.yaml +++ b/Configuration/NodeTypes.Mixin.Suggestable.yaml @@ -1,21 +1,20 @@ 'Flowpack.SearchPlugin:SuggestableMixin': abstract: true properties: + 'neos_suggestion_context': + search: + elasticSearchMapping: + type: keyword + indexing: "${Flowpack.SearchPlugin.Suggestion.buildContext(node)}" + 'neos_suggestion': search: elasticSearchMapping: type: completion contexts: - - name: 'workspace' - type: category - path: 'neos_workspace' - - - name: 'parent_path' - type: category - path: 'neos_parent_path' - - - name: 'hidden' + name: 'suggestion_context' type: category - path: 'neos_hidden' + path: 'neos_suggestion_context' + indexing: "${Flowpack.SearchPlugin.Suggestion.build(q(node).property('title') ? q(node).property('title') : '', 20)}"