Skip to content

Commit a1650eb

Browse files
Merge pull request #49 from aertmann/master
BUGFIX: Make completions/suggestions work with ES 5.x
2 parents c822255 + 40f7c4d commit a1650eb

4 files changed

Lines changed: 71 additions & 37 deletions

File tree

Classes/Controller/SuggestController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public function initializeObject()
5656
}
5757

5858
/**
59+
* @param string $term
5960
* @param string $contextNodeIdentifier
6061
* @param string $dimensionCombination
61-
* @param string $term
6262
* @return void
6363
* @throws QueryBuildingException
6464
*/
65-
public function indexAction($contextNodeIdentifier, $dimensionCombination, $term)
65+
public function indexAction($term, $contextNodeIdentifier, $dimensionCombination = null)
6666
{
6767
if ($this->elasticSearchClient === null) {
6868
throw new \RuntimeException('The SuggestController needs an ElasticSearchClient, it seems you run without the flowpack/elasticsearch-contentrepositoryadaptor package, though.', 1487189823);
@@ -79,14 +79,14 @@ public function indexAction($contextNodeIdentifier, $dimensionCombination, $term
7979
return;
8080
}
8181

82-
$requestJson = $this->buildRequestForTerm($contextNodeIdentifier, $dimensionCombination, $term);
82+
$requestJson = $this->buildRequestForTerm($term, $contextNodeIdentifier, $dimensionCombination);
8383

8484
try {
8585
$response = $this->elasticSearchClient->getIndex()->request('POST', '/_search', [], $requestJson)->getTreatedContent();
8686
$result['completions'] = $this->extractCompletions($response);
8787
$result['suggestions'] = $this->extractSuggestions($response);
8888
} catch (\Exception $e) {
89-
$result['errors'] = ['Could not execute query'];
89+
$result['errors'] = ['Could not execute query: ' . $e->getMessage()];
9090
}
9191

9292
$this->view->assign('value', $result);
@@ -99,7 +99,7 @@ public function indexAction($contextNodeIdentifier, $dimensionCombination, $term
9999
* @return ElasticSearchQueryBuilder
100100
* @throws QueryBuildingException
101101
*/
102-
protected function buildRequestForTerm($contextNodeIdentifier, $dimensionCombination, $term)
102+
protected function buildRequestForTerm($term, $contextNodeIdentifier, $dimensionCombination = null)
103103
{
104104
$cacheKey = $contextNodeIdentifier . '-' . md5($dimensionCombination);
105105
$termPlaceholder = '---term-soh2gufuNi---';
@@ -109,8 +109,8 @@ protected function buildRequestForTerm($contextNodeIdentifier, $dimensionCombina
109109
// and the term is trimmed to alnum characters to avoid errors
110110
$suggestTerm = preg_replace('/[[:^alnum:]]/', '', explode(' ', $term)[0]);
111111

112-
if(!$this->elasticSearchQueryTemplateCache->has($cacheKey)) {
113-
$contentContext = $this->createContentContext('live', json_decode($dimensionCombination, true));
112+
if (!$this->elasticSearchQueryTemplateCache->has($cacheKey)) {
113+
$contentContext = $this->createContentContext('live', $dimensionCombination ? json_decode($dimensionCombination, true) : []);
114114
$contextNode = $contentContext->getNodeByIdentifier($contextNodeIdentifier);
115115

116116
/** @var ElasticSearchQueryBuilder $query */
@@ -132,7 +132,7 @@ protected function buildRequestForTerm($contextNodeIdentifier, $dimensionCombina
132132
]
133133
])
134134
->suggestions('suggestions', [
135-
'text' => $termPlaceholder,
135+
'prefix' => $termPlaceholder,
136136
'completion' => [
137137
'field' => '__suggestions',
138138
'fuzzy' => true,

Classes/EelHelper/SuggestionIndexHelper.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ class SuggestionIndexHelper implements ProtectedContextAwareInterface
2929
* @param int $weight A positive integer or a string containing a positive integer, which defines a weight and allows you to rank your suggestions.
3030
* @return array
3131
*/
32-
public function build($input, $output = '', array $payload = [], $weight = 1)
32+
public function build($input, $weight = 1)
3333
{
3434
return [
3535
'input' => $this->prepareInput($input),
36-
'output' => $this->prepareOutput($output),
37-
'payload' => json_encode($payload),
3836
'weight' => $weight
3937
];
4038
}
@@ -66,15 +64,6 @@ protected function prepareInput($input)
6664
}
6765
}
6866

69-
/**
70-
* @param string $input
71-
* @return array
72-
*/
73-
protected function prepareOutput($input)
74-
{
75-
return strip_tags($input);
76-
}
77-
7867
/**
7968
* All methods are considered safe
8069
*

Configuration/NodeTypes.Mixins.yaml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
search:
66
elasticSearchMapping:
77
type: completion
8-
payloads: true
9-
context:
10-
workspace:
8+
contexts:
9+
-
10+
name: 'workspace'
1111
type: category
1212
path: '__workspace'
13-
parentPath:
13+
-
14+
name: 'parentPath'
1415
type: category
1516
path: '__parentPath'
16-
dimensionCombinationHash:
17+
-
18+
name: 'dimensionCombinationHash'
1719
type: category
1820
path: '__dimensionCombinationHash'
19-
indexing: "${Flowpack.SearchPlugin.Suggestion.build(q(node).property('title') ? q(node).property('title') : '', q(node).is('[instanceof Neos.Neos:Document]') ? node.identifier : q(node).parents('[instanceof Neos.Neos:Document]').get(0).identifier, {nodeIdentifier: node.identifier}, 20)}"
21+
indexing: "${Flowpack.SearchPlugin.Suggestion.build(q(node).property('title') ? q(node).property('title') : '', 20)}"
2022

2123
'Flowpack.SearchPlugin:AutocompletableMixin':
2224
abstract: true
2325
properties:
2426
'__completion':
2527
search:
2628
elasticSearchMapping:
27-
type: string
29+
type: text
2830
analyzer: autocomplete
31+
fielddata: true
2932
indexing: "${String.stripTags(q(node).property('title'))}"

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This plugin is a Search Plugin, to be used together with
66

7-
* [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor) or
7+
* [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor) or
88
* [Flowpack.SimpleSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.SimpleSearch.ContentRepositoryAdaptor).
99

1010
## Installation
@@ -36,7 +36,7 @@ To specify a custom index name, the following is needed:
3636
elasticSearch:
3737
indexName: acmecom
3838

39-
### Pagination
39+
### Pagination
4040

4141
The pagination search results can be configured via Fusion. The following shows the defaults:
4242

@@ -60,7 +60,7 @@ Feel free to use the `DocumentSearchResult.html` in the Flowpack.SearchPlugin as
6060

6161
## Search completions and suggestions
6262

63-
The default search form template comes with a `data-autocomplete-source` attribute pointing to the
63+
The default search form template comes with a `data-autocomplete-source` attribute pointing to the
6464
`SuggestController` of this package.
6565

6666
To use this term suggester, you need to configure the indexing like this, to define a custom
@@ -91,12 +91,12 @@ done like this:
9191
superTypes:
9292
'Flowpack.SearchPlugin:SuggestableMixin': true
9393
'Flowpack.SearchPlugin:AutocompletableMixin': true
94-
94+
9595
'Neos.Neos:Shortcut':
9696
superTypes:
9797
'Flowpack.SearchPlugin:SuggestableMixin': false
9898
'Flowpack.SearchPlugin:AutocompletableMixin': false
99-
99+
100100
'Neos.NodeTypes:TitleMixin':
101101
superTypes:
102102
'Flowpack.SearchPlugin:SuggestableMixin': true
@@ -126,23 +126,65 @@ The returned JSON looks like this (with a `term` of "content" after indexing the
126126
"suggestions": [
127127
{
128128
"text": "995c9174-ddd6-4d5c-cfc0-1ffc82184677",
129-
"score": 20,
129+
"_index": "acmecom-1536833562",
130+
"_type": "Neos-Neos:Page",
131+
"_id": "03da089f6495852dc9e7b796adde85f21093b3c7",
132+
"score": 40,
130133
"payload": {
131-
"nodeIdentifier": "d17caff2-f50c-d30b-b735-9b9216de02e9"
134+
"__path": "/sites/acmecom/node-2"
135+
},
136+
"contexts": {
137+
"workspace": {
138+
0: "live"
139+
},
140+
"parentPath": {
141+
0: "/sites/acmecom"
142+
},
143+
"dimensionCombinationHash": {
144+
0: "d751713988987e9331980363e24189ce"
145+
},
132146
}
133147
},
134148
{
135149
"text": "a66ec7db-3459-b67b-7bcb-16e2508a89f0",
150+
"_index": "acmecom-1536833562",
151+
"_type": "Neos-Neos:Page",
152+
"_id": "151a1d0531f1ac5c1a267a3d6a3af84967e0c35f",
136153
"score": 20,
137154
"payload": {
138-
"nodeIdentifier": "fd283257-9b12-8412-f922-6643ac818294"
155+
"__path": "/sites/acmecom/node-1"
156+
},
157+
"contexts": {
158+
"workspace": {
159+
0: "live"
160+
},
161+
"parentPath": {
162+
0: "/sites/acmecom"
163+
},
164+
"dimensionCombinationHash": {
165+
0: "d751713988987e9331980363e24189ce"
166+
},
139167
}
140168
},
141169
{
142170
"text": "a3474e1d-dd60-4a84-82b1-18d2f21891a3",
171+
"_index": "acmecom-1536833562",
172+
"_id": "c443d53c76de1af2438b8af0bf33dc7befe291f5",
173+
"_type": "Neos-Neos:Page",
143174
"score": 20,
144-
"payload": {
145-
"nodeIdentifier": "7eee2ee6-2a4e-5240-3674-2fb84a51900b"
175+
"_source": {
176+
"__path": "/sites/acmecom/node-3"
177+
},
178+
"contexts": {
179+
"workspace": {
180+
0: "live"
181+
},
182+
"parentPath": {
183+
0: "/sites/acmecom"
184+
},
185+
"dimensionCombinationHash": {
186+
0: "d751713988987e9331980363e24189ce"
187+
},
146188
}
147189
}
148190
]

0 commit comments

Comments
 (0)