11<?php
2+
23namespace Flowpack \SearchPlugin \Controller ;
34
45/*
1718use Flowpack \SearchPlugin \Suggestion \SuggestionContextInterface ;
1819use Flowpack \SearchPlugin \Utility \SearchTerm ;
1920use Neos \Cache \Frontend \VariableFrontend ;
20- use Neos \ContentRepository \Domain \Model \NodeInterface ;
21+ use Neos \ContentRepository \Core \Projection \ContentGraph \Node ;
22+ use Neos \ContentRepository \Core \SharedModel \Node \NodeAddress ;
23+ use Neos \ContentRepository \Core \SharedModel \Workspace \WorkspaceName ;
24+ use Neos \ContentRepositoryRegistry \ContentRepositoryRegistry ;
2125use Neos \Flow \Annotations as Flow ;
2226use Neos \Flow \Mvc \Controller \ActionController ;
2327use Neos \Flow \Mvc \View \JsonView ;
24- use Neos \Neos \Controller \CreateContentContextTrait ;
2528
2629class SuggestController extends ActionController
2730{
28- use CreateContentContextTrait;
29-
3031 /**
3132 * @var ElasticSearchClient
3233 */
@@ -62,6 +63,10 @@ class SuggestController extends ActionController
6263 */
6364 protected $ searchAsYouTypeSettings = [];
6465
66+
67+ #[Flow \Inject()]
68+ protected ContentRepositoryRegistry $ contentRepositoryRegistry ;
69+
6570 public function initializeObject ()
6671 {
6772 if ($ this ->objectManager ->isRegistered (ElasticSearchClient::class)) {
@@ -72,13 +77,13 @@ public function initializeObject()
7277
7378 /**
7479 * @param string $term
75- * @param string $contextNodeIdentifier
80+ * @param string $contextNode
7681 * @param string $dimensionCombination
7782 * @return void
7883 * @throws QueryBuildingException
7984 * @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
8085 */
81- public function indexAction (string $ term = '' , string $ contextNodeIdentifier = '' , string $ dimensionCombination = null ): void
86+ public function indexAction (string $ term = '' , string $ contextNode = '' ): void
8287 {
8388 if ($ this ->elasticSearchClient === null ) {
8489 throw new \RuntimeException ('The SuggestController needs an ElasticSearchClient, it seems you run without the flowpack/elasticsearch-contentrepositoryadaptor package, though. ' , 1487189823 );
@@ -95,7 +100,8 @@ public function indexAction(string $term = '', string $contextNodeIdentifier = '
95100 return ;
96101 }
97102
98- $ requestJson = $ this ->buildRequestForTerm ($ term , $ contextNodeIdentifier , $ dimensionCombination );
103+ $ contextNodeAddress = NodeAddress::fromJsonString ($ contextNode );
104+ $ requestJson = $ this ->buildRequestForTerm ($ term , $ contextNodeAddress );
99105
100106 try {
101107 $ response = $ this ->elasticSearchClient ->getIndex ()->request ('POST ' , '/_search ' , [], $ requestJson )->getTreatedContent ();
@@ -110,16 +116,16 @@ public function indexAction(string $term = '', string $contextNodeIdentifier = '
110116
111117 /**
112118 * @param string $term
113- * @param string $contextNodeIdentifier
119+ * @param string $contextNode
114120 * @param string|null $dimensionCombination
115121 * @return string
116122 * @throws QueryBuildingException
117123 * @throws \Neos\Cache\Exception
118124 * @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
119125 */
120- protected function buildRequestForTerm (string $ term , string $ contextNodeIdentifier , string $ dimensionCombination = null ): string
126+ protected function buildRequestForTerm (string $ term , NodeAddress $ contextNodeAddress ): string
121127 {
122- $ cacheKey = $ contextNodeIdentifier . '- ' . md5 ( $ dimensionCombination ) ;
128+ $ cacheKey = $ contextNodeAddress -> aggregateId -> value . '- ' . $ contextNodeAddress -> dimensionSpacePoint -> hash ;
123129 $ termPlaceholder = '---term-soh2gufuNi--- ' ;
124130 $ term = strtolower ($ term );
125131
@@ -128,11 +134,11 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi
128134 $ suggestTerm = SearchTerm::sanitize (explode (' ' , $ term )[0 ]);
129135
130136 if (!$ this ->elasticSearchQueryTemplateCache ->has ($ cacheKey )) {
131- $ contentContext = $ this ->createContentContext ( ' live ' , $ dimensionCombination ? json_decode ( $ dimensionCombination , true ) : [] );
132- $ contextNode = $ contentContext -> getNodeByIdentifier ( $ contextNodeIdentifier );
137+ $ subgraph = $ this ->contentRepositoryRegistry -> get ( $ contextNodeAddress -> contentRepositoryId )-> getContentSubgraph (WorkspaceName:: forLive (), $ contextNodeAddress -> dimensionSpacePoint );
138+ $ contextNode = $ subgraph -> findNodeById ( $ contextNodeAddress -> aggregateId );
133139
134- if (!$ contextNode instanceof NodeInterface ) {
135- throw new \Exception (sprintf ('The context node for search with identifier %s could not be found ' , $ contextNodeIdentifier ), 1634467679 );
140+ if (!$ contextNode instanceof Node ) {
141+ throw new \Exception (sprintf ('The context node for search with identifier %s could not be found ' , $ contextNodeAddress -> aggregateId -> value ), 1634467679 );
136142 }
137143
138144 $ sourceFields = array_filter ($ this ->searchAsYouTypeSettings ['suggestions ' ]['sourceFields ' ] ?? ['neos_path ' ]);
@@ -178,7 +184,7 @@ protected function buildRequestForTerm(string $term, string $contextNodeIdentifi
178184
179185 $ requestTemplate = json_encode ($ request );
180186
181- $ this ->elasticSearchQueryTemplateCache ->set ($ contextNodeIdentifier , $ requestTemplate );
187+ $ this ->elasticSearchQueryTemplateCache ->set ($ contextNodeAddress -> aggregateId -> value , $ requestTemplate );
182188 } else {
183189 $ requestTemplate = $ this ->elasticSearchQueryTemplateCache ->get ($ cacheKey );
184190 }
0 commit comments