1313 * */
1414
1515use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Exception \QueryBuildingException ;
16+ use TYPO3 \Eel \ProtectedContextAwareInterface ;
1617use TYPO3 \Flow \Annotations as Flow ;
1718use TYPO3 \TYPO3CR \Domain \Model \NodeInterface ;
19+ use TYPO3 \TYPO3CR \Search \Search \QueryBuilderInterface ;
1820
1921/**
2022 * Query Builder for ElasticSearch Queries
2123 */
22- class ElasticSearchQueryBuilder implements \ TYPO3 \ Eel \ ProtectedContextAwareInterface {
24+ class ElasticSearchQueryBuilder implements QueryBuilderInterface, ProtectedContextAwareInterface {
2325
2426 /**
2527 * @Flow\Inject
@@ -77,6 +79,14 @@ class ElasticSearchQueryBuilder implements \TYPO3\Eel\ProtectedContextAwareInter
7779 // Reference: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html
7880 'filtered ' => array (
7981 'query ' => array (
82+ 'bool ' => array (
83+ 'must ' => array (
84+ array (
85+ 'match_all ' => array ()
86+ )
87+ )
88+ )
89+
8090 ),
8191 'filter ' => array (
8292 // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html
@@ -108,22 +118,6 @@ class ElasticSearchQueryBuilder implements \TYPO3\Eel\ProtectedContextAwareInter
108118 'fields ' => array ('__path ' )
109119 );
110120
111- /**
112- * @param NodeInterface $contextNode
113- */
114- public function __construct (NodeInterface $ contextNode ) {
115- // on indexing, the __parentPath is tokenized to contain ALL parent path parts,
116- // e.g. /foo, /foo/bar/, /foo/bar/baz; to speed up matching.. That's why we use a simple "term" filter here.
117- // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
118- $ this ->queryFilter ('term ' , array ('__parentPath ' => $ contextNode ->getPath ()));
119-
120- //
121- // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html
122- $ this ->queryFilter ('terms ' , array ('__workspace ' => array ('live ' , $ contextNode ->getContext ()->getWorkspace ()->getName ())));
123-
124- $ this ->contextNode = $ contextNode ;
125- }
126-
127121 /**
128122 * HIGH-LEVEL API
129123 */
@@ -299,16 +293,6 @@ public function getRequest() {
299293 return $ this ->request ;
300294 }
301295
302- /**
303- * All methods are considered safe
304- *
305- * @param string $methodName
306- * @return boolean
307- */
308- public function allowsCallOfMethod ($ methodName ) {
309- return TRUE ;
310- }
311-
312296 /**
313297 * Log the current request to the ElasticSearch log for debugging after it has been executed.
314298 *
@@ -401,4 +385,52 @@ public function count() {
401385 return $ count ;
402386 }
403387
388+ /**
389+ * Match the searchword against the fulltext index
390+ *
391+ * @param string $searchWord
392+ * @return QueryBuilderInterface
393+ */
394+ public function fulltext ($ searchWord ) {
395+
396+ $ this ->appendAtPath ('query.filtered.query.bool.must ' , array (
397+ 'query_string ' => array (
398+ 'query ' => $ searchWord
399+ )
400+ ));
401+ return $ this ;
402+ }
403+
404+ /**
405+ * Sets the starting point for this query. Search result should only contain nodes that
406+ * match the context of the given node and have it as parent node in their rootline.
407+ *
408+ * @param NodeInterface $contextNode
409+ * @return QueryBuilderInterface
410+ */
411+ public function query (NodeInterface $ contextNode ) {
412+ // on indexing, the __parentPath is tokenized to contain ALL parent path parts,
413+ // e.g. /foo, /foo/bar/, /foo/bar/baz; to speed up matching.. That's why we use a simple "term" filter here.
414+ // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-term-filter.html
415+ $ this ->queryFilter ('term ' , array ('__parentPath ' => $ contextNode ->getPath ()));
416+
417+ //
418+ // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html
419+ $ this ->queryFilter ('terms ' , array ('__workspace ' => array ('live ' , $ contextNode ->getContext ()->getWorkspace ()->getName ())));
420+
421+ $ this ->contextNode = $ contextNode ;
422+
423+ return $ this ;
424+ }
425+
426+
427+ /**
428+ * All methods are considered safe
429+ *
430+ * @param string $methodName
431+ * @return boolean
432+ */
433+ public function allowsCallOfMethod ($ methodName ) {
434+ return TRUE ;
435+ }
404436}
0 commit comments