Skip to content

Commit

Permalink
!!! BUGFIX: Contexts are respected in a multi-site environment.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniellienert committed Jul 13, 2021
1 parent 0a65351 commit 0e0951c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
11 changes: 8 additions & 3 deletions Classes/Controller/SuggestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,12 @@ class SuggestController extends ActionController
*/
protected $elasticSearchQueryTemplateCache;

/**
* @Flow\Inject
* @var SuggestionIndexHelper
*/
protected $suggestionIndexHelper;

/**
* @var array
*/
Expand Down Expand Up @@ -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)
]
]
]);
Expand Down
21 changes: 20 additions & 1 deletion Classes/EelHelper/SuggestionIndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,20 +26,38 @@
*/
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),
'weight' => $weight
];
}

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
Expand Down
19 changes: 9 additions & 10 deletions Configuration/NodeTypes.Mixin.Suggestable.yaml
Original file line number Diff line number Diff line change
@@ -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)}"

0 comments on commit 0e0951c

Please sign in to comment.