Skip to content

Commit 1f40254

Browse files
committed
Merge pull request #3 from skurfuerst/dev-query-result
Highlighting Support in Search
2 parents 3c9cfd4 + d569ecd commit 1f40254

11 files changed

Lines changed: 198 additions & 32 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
namespace Flowpack\SearchPlugin\Controller;
3+
4+
/* *
5+
* This script belongs to the TYPO3 Flow package "Flowpack.SearchPlugin". *
6+
* *
7+
* It is free software; you can redistribute it and/or modify it under *
8+
* the terms of the GNU Lesser General Public License, either version 3 *
9+
* of the License, or (at your option) any later version. *
10+
* *
11+
* The TYPO3 project - inspiring people to share! *
12+
* */
13+
14+
use TYPO3\Flow\Annotations as Flow;
15+
use TYPO3\Flow\Mvc\Controller\ActionController;
16+
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
17+
18+
/**
19+
* Class SuggestController
20+
*
21+
* @author Jon Klixbüll Langeland <jon@moc.net>
22+
* @package Flowpack\SearchPlugin\Controller
23+
*/
24+
class SuggestController extends ActionController {
25+
26+
/**
27+
* @Flow\Inject
28+
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient
29+
*/
30+
protected $elasticSearchClient;
31+
32+
/**
33+
* The node inside which searching should happen
34+
*
35+
* @var NodeInterface
36+
*/
37+
protected $contextNode;
38+
39+
/**
40+
* @Flow\Inject
41+
* @var \Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface
42+
*/
43+
protected $logger;
44+
45+
/**
46+
* @var boolean
47+
*/
48+
protected $logThisQuery = FALSE;
49+
50+
/**
51+
* @var string
52+
*/
53+
protected $logMessage;
54+
55+
/**
56+
* @var array
57+
*/
58+
protected $viewFormatToObjectNameMap = array(
59+
'json' => 'TYPO3\Flow\Mvc\View\JsonView'
60+
);
61+
62+
/**
63+
* @param NodeInterface $node
64+
* @param string $term
65+
*/
66+
public function indexAction(NodeInterface $node, $term) {
67+
$request = array(
68+
'suggests' => array(
69+
'text' => $term,
70+
'term' => array(
71+
'field' => '_all'
72+
)
73+
)
74+
);
75+
76+
$response = $this->elasticSearchClient->getIndex()->request('GET', '/_suggest', array(), json_encode($request))->getTreatedContent();
77+
$suggestions = array_map(function($option) {
78+
return $option['text'];
79+
}, $response['suggests'][0]['options']);
80+
81+
$this->view->assign('value', $suggestions);
82+
}
83+
84+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace Flowpack\SearchPlugin\EelHelper;
3+
4+
/* *
5+
* This script belongs to the TYPO3 Flow package "Flowpack.SearchPlugin". *
6+
* *
7+
* It is free software; you can redistribute it and/or modify it under *
8+
* the terms of the GNU Lesser General Public License, either version 3 *
9+
* of the License, or (at your option) any later version. *
10+
* *
11+
* The TYPO3 project - inspiring people to share! *
12+
* */
13+
use TYPO3\Flow\Annotations as Flow;
14+
use TYPO3\Eel\ProtectedContextAwareInterface;
15+
16+
/**
17+
* Additional Array Helpers which might once
18+
*
19+
* @Flow\Proxy(false)
20+
*/
21+
class SearchArrayHelper implements ProtectedContextAwareInterface {
22+
23+
/**
24+
* Concatenate arrays or values to a new array
25+
*
26+
* @param array|mixed $array1 First array or value
27+
* @param array|mixed $array2 Second array or value
28+
* @param array|mixed $array_ Optional variable list of additional arrays / values
29+
* @return array The array with concatenated arrays or values
30+
*/
31+
public function flatten($arrays) {
32+
$return = array();
33+
array_walk_recursive($arrays, function($a) use (&$return) { $return[] = $a; });
34+
return $return;
35+
}
36+
37+
/**
38+
* All methods are considered safe
39+
*
40+
* @param string $methodName
41+
* @return boolean
42+
*/
43+
public function allowsCallOfMethod($methodName) {
44+
return TRUE;
45+
}
46+
47+
}

Classes/Flowpack/SearchPlugin/TypoScriptObjects/CanRenderImplementation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
/* *
6-
* This script belongs to the TYPO3 Flow package "Flowpack.SearchPlugin.TypoScriptObjects". *
6+
* This script belongs to the TYPO3 Flow package "Flowpack.SearchPlugin". *
77
* *
88
* It is free software; you can redistribute it and/or modify it under *
99
* the terms of the GNU Lesser General Public License, either version 3 *

Configuration/Policy.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
privilegeTargets:
2+
TYPO3\Flow\Security\Authorization\Privilege\Method\MethodPrivilege:
3+
Flowpack_SearchPlugin_Controller_SuggestController:
4+
matcher: method(Flowpack\SearchPlugin\Controller\SuggestController->indexAction())
5+
6+
roles:
7+
'TYPO3.Flow:Everybody':
8+
privileges:
9+
-
10+
privilegeTarget: Flowpack_SearchPlugin_Controller_SuggestController
11+
permission: GRANT

Configuration/Routes.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DataSourceController routes
2+
-
3+
name: 'flowpack/searchplugin - SuggestController->index'
4+
uriPattern: 'flowpack/searchplugin'
5+
defaults:
6+
'@package': 'Flowpack.SearchPlugin'
7+
'@controller': 'Suggest'
8+
'@action': 'index'
9+
'@format': 'json'
10+
appendExceedingArguments: TRUE
11+
httpMethods: ['GET']

Configuration/Settings.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ TYPO3:
22
Neos:
33
typoScript:
44
autoInclude:
5-
'Flowpack.SearchPlugin': TRUE
5+
'Flowpack.SearchPlugin': TRUE
6+
7+
TypoScript:
8+
defaultContext:
9+
Flowpack.SearchPlugin.Array: 'Flowpack\SearchPlugin\EelHelper\SearchArrayHelper'
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form action="" method="GET">
2-
<input type="text" name="search" value="{searchWord}" />
3-
<button type="submit">Send</button>
1+
<form method="GET">
2+
<input type="search" name="search" value="{searchWord}" placeholder="Search" data-autocomplete-source="{f:uri.action(action: 'index', controller: 'Suggest', package: 'Flowpack.SearchPlugin', format: 'json' absolute: 1, arguments: {node: node})}" />
3+
<button type="submit">Search</button>
44
</form>
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
{namespace neos=TYPO3\Neos\ViewHelpers}
22
{namespace ts=TYPO3\TypoScript\ViewHelpers}
3+
{namespace search=TYPO3\TYPO3CR\Search\ViewHelpers}
4+
<div{attributes -> f:format.raw()}>
5+
{searchForm -> f:format.raw()}
36

4-
{searchForm -> f:format.raw()}
5-
6-
Showing {searchResults -> f:count()} of {totalSearchResults} results
7-
8-
{searchResultRenderer -> f:format.raw()}
7+
<f:if condition="{searchQuery}">
8+
<search:widget.paginate query="{searchQuery}" as="results">
9+
Showing {results.accessibleCount} of {totalSearchResults} results
10+
<ts:render path="searchResultRenderer" context="{searchResults: results}" />
11+
</search:widget.paginate>
12+
</f:if>
13+
</div>
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
{namespace neos=TYPO3\Neos\ViewHelpers}
2+
<neos:link.node node="{node}">{title}</neos:link.node>
23

3-
<b><neos:link.node node="{node}">
4-
{title}
5-
</neos:link.node></b><br />
4+
<f:if condition="{highlight}">
5+
<p>
6+
<f:for each="{highlight}" as="fragment" iteration="fragmentIterator">
7+
{fragment -> f:format.raw()}{f:if(condition: fragmentIterator.isLast, else: '...')}
8+
</f:for>
9+
</p>
610

7-
<f:if condition="{description}">
8-
{description}
11+
<div class="breadcrumb">
12+
<span>Path:</span>
13+
<f:for each="{parents}" as="parent">
14+
<neos:link.node node="{parent}" />
15+
</f:for>
16+
<neos:link.node node="{node}" />
17+
</div>
918
</f:if>

Resources/Private/TypoScript/ResultRendering.ts2

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ prototype(Flowpack.SearchPlugin:SingleResult) < prototype(TYPO3.TypoScript:Case)
1515
prototype(TYPO3.Neos:DocumentSearchResult) < prototype(TYPO3.TypoScript:Template) {
1616
templatePath = 'resource://Flowpack.SearchPlugin/Private/Templates/SearchResult/DocumentSearchResult.html'
1717
node = ${node}
18+
highlight = ${Flowpack.SearchPlugin.Array.flatten(searchHit.highlight)}
1819

1920
title = ${q(node).property('title')}
2021
description = ''
22+
parents = ${Array.reverse(q(node).parents('[instanceof TYPO3.Neos:Document]').get())}
2123
}
2224

23-
prototype(TYPO3.Neos.NodeTypes:PageSearchResult) < prototype(TYPO3.Neos:DocumentSearchResult)
24-
25-
26-
prototype(TYPO3.NeosDemoTypo3Org:ChapterSearchResult) < prototype(TYPO3.Neos:DocumentSearchResult) {
27-
description = ${q(node).property('chapterDescription')}
28-
}
25+
prototype(TYPO3.Neos.NodeTypes:PageSearchResult) < prototype(TYPO3.Neos:DocumentSearchResult)

0 commit comments

Comments
 (0)