Skip to content

Commit

Permalink
Merge pull request #49 from aertmann/master
Browse files Browse the repository at this point in the history
BUGFIX: Make completions/suggestions work with ES 5.x
  • Loading branch information
daniellienert committed May 21, 2020
2 parents c822255 + 40f7c4d commit a1650eb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
16 changes: 8 additions & 8 deletions Classes/Controller/SuggestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public function initializeObject()
}

/**
* @param string $term
* @param string $contextNodeIdentifier
* @param string $dimensionCombination
* @param string $term
* @return void
* @throws QueryBuildingException
*/
public function indexAction($contextNodeIdentifier, $dimensionCombination, $term)
public function indexAction($term, $contextNodeIdentifier, $dimensionCombination = null)
{
if ($this->elasticSearchClient === null) {
throw new \RuntimeException('The SuggestController needs an ElasticSearchClient, it seems you run without the flowpack/elasticsearch-contentrepositoryadaptor package, though.', 1487189823);
Expand All @@ -79,14 +79,14 @@ public function indexAction($contextNodeIdentifier, $dimensionCombination, $term
return;
}

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

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

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

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

/** @var ElasticSearchQueryBuilder $query */
Expand All @@ -132,7 +132,7 @@ protected function buildRequestForTerm($contextNodeIdentifier, $dimensionCombina
]
])
->suggestions('suggestions', [
'text' => $termPlaceholder,
'prefix' => $termPlaceholder,
'completion' => [
'field' => '__suggestions',
'fuzzy' => true,
Expand Down
13 changes: 1 addition & 12 deletions Classes/EelHelper/SuggestionIndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ class SuggestionIndexHelper implements ProtectedContextAwareInterface
* @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
*/
public function build($input, $output = '', array $payload = [], $weight = 1)
public function build($input, $weight = 1)
{
return [
'input' => $this->prepareInput($input),
'output' => $this->prepareOutput($output),
'payload' => json_encode($payload),
'weight' => $weight
];
}
Expand Down Expand Up @@ -66,15 +64,6 @@ protected function prepareInput($input)
}
}

/**
* @param string $input
* @return array
*/
protected function prepareOutput($input)
{
return strip_tags($input);
}

/**
* All methods are considered safe
*
Expand Down
17 changes: 10 additions & 7 deletions Configuration/NodeTypes.Mixins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
search:
elasticSearchMapping:
type: completion
payloads: true
context:
workspace:
contexts:
-
name: 'workspace'
type: category
path: '__workspace'
parentPath:
-
name: 'parentPath'
type: category
path: '__parentPath'
dimensionCombinationHash:
-
name: 'dimensionCombinationHash'
type: category
path: '__dimensionCombinationHash'
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)}"
indexing: "${Flowpack.SearchPlugin.Suggestion.build(q(node).property('title') ? q(node).property('title') : '', 20)}"

'Flowpack.SearchPlugin:AutocompletableMixin':
abstract: true
properties:
'__completion':
search:
elasticSearchMapping:
type: string
type: text
analyzer: autocomplete
fielddata: true
indexing: "${String.stripTags(q(node).property('title'))}"
62 changes: 52 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

* [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor) or
* [Flowpack.ElasticSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.ElasticSearch.ContentRepositoryAdaptor) or
* [Flowpack.SimpleSearch.ContentRepositoryAdaptor](https://github.com/Flowpack/Flowpack.SimpleSearch.ContentRepositoryAdaptor).

## Installation
Expand Down Expand Up @@ -36,7 +36,7 @@ To specify a custom index name, the following is needed:
elasticSearch:
indexName: acmecom

### Pagination
### Pagination

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

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

## Search completions and suggestions

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

To use this term suggester, you need to configure the indexing like this, to define a custom
Expand Down Expand Up @@ -91,12 +91,12 @@ done like this:
superTypes:
'Flowpack.SearchPlugin:SuggestableMixin': true
'Flowpack.SearchPlugin:AutocompletableMixin': true

'Neos.Neos:Shortcut':
superTypes:
'Flowpack.SearchPlugin:SuggestableMixin': false
'Flowpack.SearchPlugin:AutocompletableMixin': false

'Neos.NodeTypes:TitleMixin':
superTypes:
'Flowpack.SearchPlugin:SuggestableMixin': true
Expand Down Expand Up @@ -126,23 +126,65 @@ The returned JSON looks like this (with a `term` of "content" after indexing the
"suggestions": [
{
"text": "995c9174-ddd6-4d5c-cfc0-1ffc82184677",
"score": 20,
"_index": "acmecom-1536833562",
"_type": "Neos-Neos:Page",
"_id": "03da089f6495852dc9e7b796adde85f21093b3c7",
"score": 40,
"payload": {
"nodeIdentifier": "d17caff2-f50c-d30b-b735-9b9216de02e9"
"__path": "/sites/acmecom/node-2"
},
"contexts": {
"workspace": {
0: "live"
},
"parentPath": {
0: "/sites/acmecom"
},
"dimensionCombinationHash": {
0: "d751713988987e9331980363e24189ce"
},
}
},
{
"text": "a66ec7db-3459-b67b-7bcb-16e2508a89f0",
"_index": "acmecom-1536833562",
"_type": "Neos-Neos:Page",
"_id": "151a1d0531f1ac5c1a267a3d6a3af84967e0c35f",
"score": 20,
"payload": {
"nodeIdentifier": "fd283257-9b12-8412-f922-6643ac818294"
"__path": "/sites/acmecom/node-1"
},
"contexts": {
"workspace": {
0: "live"
},
"parentPath": {
0: "/sites/acmecom"
},
"dimensionCombinationHash": {
0: "d751713988987e9331980363e24189ce"
},
}
},
{
"text": "a3474e1d-dd60-4a84-82b1-18d2f21891a3",
"_index": "acmecom-1536833562",
"_id": "c443d53c76de1af2438b8af0bf33dc7befe291f5",
"_type": "Neos-Neos:Page",
"score": 20,
"payload": {
"nodeIdentifier": "7eee2ee6-2a4e-5240-3674-2fb84a51900b"
"_source": {
"__path": "/sites/acmecom/node-3"
},
"contexts": {
"workspace": {
0: "live"
},
"parentPath": {
0: "/sites/acmecom"
},
"dimensionCombinationHash": {
0: "d751713988987e9331980363e24189ce"
},
}
}
]
Expand Down

0 comments on commit a1650eb

Please sign in to comment.