Skip to content

Commit

Permalink
Merge pull request #65 from daniellienert/bugfix/fix-suggestion-context
Browse files Browse the repository at this point in the history
!!! BUGFIX: Contexts are respected in a multi-site environment.
  • Loading branch information
daniellienert authored Jul 13, 2021
2 parents 74d9af0 + 0e0951c commit 75473a3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
10 changes: 8 additions & 2 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,8 +160,7 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi
'fuzzy' => true,
'size' => $this->searchAsYouTypeSettings['suggestions']['size'] ?? 10,
'contexts' => [
'parent_path' => $contextNode->getPath(),
'workspace' => 'live',
'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 75473a3

Please sign in to comment.