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+ }
0 commit comments