Skip to content

Commit 2c29ac6

Browse files
committed
TASK: Clean up SuggestController, add docs
1 parent 0784489 commit 2c29ac6

3 files changed

Lines changed: 24 additions & 41 deletions

File tree

Classes/Flowpack/SearchPlugin/Controller/SuggestController.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use TYPO3\Flow\Annotations as Flow;
1515
use TYPO3\Flow\Mvc\Controller\ActionController;
16-
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
1716

1817
/**
1918
* Class SuggestController
@@ -26,29 +25,6 @@ class SuggestController extends ActionController
2625
*/
2726
protected $elasticSearchClient;
2827

29-
/**
30-
* The node inside which searching should happen
31-
*
32-
* @var NodeInterface
33-
*/
34-
protected $contextNode;
35-
36-
/**
37-
* @Flow\Inject
38-
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
39-
*/
40-
protected $logger;
41-
42-
/**
43-
* @var boolean
44-
*/
45-
protected $logThisQuery = false;
46-
47-
/**
48-
* @var string
49-
*/
50-
protected $logMessage;
51-
5228
/**
5329
* @var array
5430
*/
@@ -57,11 +33,10 @@ class SuggestController extends ActionController
5733
];
5834

5935
/**
60-
* @param NodeInterface $node
6136
* @param string $term
6237
* @return void
6338
*/
64-
public function indexAction(NodeInterface $node, $term)
39+
public function indexAction($term)
6540
{
6641
$request = [
6742
'suggests' => [
@@ -72,7 +47,7 @@ public function indexAction(NodeInterface $node, $term)
7247
]
7348
];
7449

75-
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_suggest', [], json_encode($request))->getTreatedContent();
50+
$response = $this->elasticSearchClient->getIndex()->request('POST', '/_suggest', [], json_encode($request))->getTreatedContent();
7651
$suggestions = array_map(function ($option) {
7752
return $option['text'];
7853
}, $response['suggests'][0]['options']);

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ Flowpack.ElasticSearch.ContentRepositoryAdaptor or Flowpack.SimpleSearch.Content
77

88
Make sure to include the Routes from this package into your main `Configuration/Routes.yaml` by the following snippet:
99

10-
```
11-
-
12-
name: 'Flowpack.SearchPlugin'
13-
uriPattern: '<SearchSubroutes>'
14-
subRoutes:
15-
'SearchSubroutes':
16-
package: 'Flowpack.SearchPlugin'
17-
```
10+
-
11+
name: 'Flowpack.SearchPlugin'
12+
uriPattern: '<SearchSubroutes>'
13+
subRoutes:
14+
'SearchSubroutes':
15+
package: 'Flowpack.SearchPlugin'
1816

1917
## Custom result rendering
2018

2119
The result list is rendered using a TypoScript object of type `nodeType + 'SearchResult'` for each hit.
2220
Thus you can easily adjust the rendering per type like this for an imaginary `Acme.AcmeCom:Product` nodetype:
2321

24-
```
25-
prototype(Acme.AcmeCom:ProductSearchResult) < prototype(TYPO3.Neos:DocumentSearchResult) {
26-
templatePath = 'resource://Acme.AcmeCom/Private/Templates/SearchResult/ProductSearchResult.html'
27-
}
28-
```
22+
prototype(Acme.AcmeCom:ProductSearchResult) < prototype(TYPO3.Neos:DocumentSearchResult) {
23+
templatePath = 'resource://Acme.AcmeCom/Private/Templates/SearchResult/ProductSearchResult.html'
24+
}
2925

3026
Feel free to use the `DocumentSearchResult.html` in the Flowpack.SearchPlugin as an example.
27+
28+
## Search suggestions
29+
30+
The default search form template comes with a `data-autocomplete-source` attribute pointing to the
31+
`SuggestController` of this package. Fed with a `term` parameter via a `GET` request, it returns a
32+
JSON-encoded array of suggestions from Elasticsearch. These are fetched with a term suggester from
33+
`_all` field, i.e. "the fulltext index".
34+
35+
These can be used to provide autocompletion on the search input using a JS library of your choice.
36+
In case you need to build the URI to the suggest controller yourself, this is what the form uses:
37+
38+
{f:uri.action(action: 'index', controller: 'Suggest', package: 'Flowpack.SearchPlugin', format: 'json', absolute: 1)}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<form method="GET">
2-
<input type="search" name="search" value="{searchWord}" placeholder="{f:translate(id: 'search', package: 'Flowpack.SearchPlugin')}" data-autocomplete-source="{f:uri.action(action: 'index', controller: 'Suggest', package: 'Flowpack.SearchPlugin', format: 'json' absolute: 1, arguments: {node: node})}"/>
2+
<input type="search" name="search" value="{searchWord}" placeholder="{f:translate(id: 'search', package: 'Flowpack.SearchPlugin')}" data-autocomplete-source="{f:uri.action(action: 'index', controller: 'Suggest', package: 'Flowpack.SearchPlugin', format: 'json', absolute: 1)}"/>
33
<button type="submit">{f:translate(id: 'search', package: 'Flowpack.SearchPlugin')}</button>
44
</form>

0 commit comments

Comments
 (0)