Skip to content

Commit 9e8c63e

Browse files
committed
FEATURE: Add prefix() QueryBuilder operation
1 parent 0129d8c commit 9e8c63e

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

Classes/Eel/ElasticSearchQueryBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,22 @@ public function fulltext(string $searchWord, array $options = []): QueryBuilderI
681681
return $this;
682682
}
683683

684+
/**
685+
* Adds a prefix filter to the query
686+
* See: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-prefix-query.html
687+
*
688+
* @param string $propertyName
689+
* @param string $prefix
690+
* @return $this|QueryBuilderInterface
691+
* @throws QueryBuildingException
692+
*/
693+
public function prefix(string $propertyName, string $prefix): QueryBuilderInterface
694+
{
695+
$this->request->queryFilter('prefix', [$propertyName => $prefix]);
696+
697+
return $this;
698+
}
699+
684700
/**
685701
* Configure Result Highlighting. Only makes sense in combination with fulltext(). By default, highlighting is enabled.
686702
* It can be disabled by calling "highlight(FALSE)".

Classes/Indexer/NodeIndexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ public function updateIndexAlias(): void
558558
* @throws Exception
559559
* @throws Exception\ConfigurationException
560560
*/
561-
public function updateMainAlias()
561+
public function updateMainAlias(): void
562562
{
563563
$aliasActions = [];
564564
$aliasNamePrefix = $this->searchClient->getIndexNamePrefix(); // The alias name is the unprefixed index name

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,20 @@ Furthermore, the following operators are supported:
408408

409409
As **value**, the following methods accept a simple type, a node object or a DateTime object.
410410

411-
* `nodeType('Your.Node:Type')`
412-
* `exactMatch('propertyName', value)` -- supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`
413-
* `exclude('propertyName', value)` -- excludes results by property - the negation of exactMatch.
414-
* `greaterThan('propertyName', value, [clauseType])` -- range filter with property values greater than the given value
415-
* `greaterThanOrEqual('propertyName', value, [clauseType])` -- range filter with property values greater than or equal to the given value
416-
* `lessThan('propertyName', value, [clauseType])` -- range filter with property values less than the given value
417-
* `lessThanOrEqual('propertyName', value, [clauseType])` -- range filter with property values less than or equal to the given value
418-
* `sortAsc('propertyName')` and `sortDesc('propertyName')` -- can also be used multiple times, e.g. `sortAsc('tag').sortDesc(`date')`
419-
will first sort by tag ascending, and then by date descending.
420-
* `limit(5)` -- only return five results. If not specified, the default limit by Elasticsearch applies (which is at 10 by default)
421-
* `from(5)` -- return the results starting from the 6th one
422-
* `fulltext('searchWord', options)` -- do a query_string query on the Fulltext index using the searchword and additional [options](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-query-string-query.html) to the query_string
411+
| Query Operator | Description |
412+
|----------------|-------------|
413+
|`nodeType('Your.Node:Type')` |Filters on the given NodeType|
414+
|`exactMatch('propertyName', value)` |Supports simple types: `exactMatch('tag', 'foo')`, or node references: `exactMatch('author', authorNode)`|
415+
|`exclude('propertyName', value)` |Excludes results by property - the negation of exactMatch.
416+
|`greaterThan('propertyName', value, [clauseType])` |Range filter with property values greater than the given value|
417+
|`greaterThanOrEqual('propertyName', value, [clauseType])`|Range filter with property values greater than or equal to the given value|
418+
|`lessThan('propertyName', value, [clauseType])` |Range filter with property values less than the given value|
419+
|`lessThanOrEqual('propertyName', value, [clauseType])`|Range filter with property values less than or equal to the given value|
420+
|`sortAsc('propertyName')` / `sortDesc('propertyName')`|Can also be used multiple times, e.g. `sortAsc('tag').sortDesc(`date')` will first sort by tag ascending, and then by date descending.|
421+
|`limit(5)` |Only return five results. If not specified, the default limit by Elasticsearch applies (which is at 10 by default)|
422+
|`from(5)` |Return the results starting from the 6th one|
423+
|`prefix('propertyName', 'prefix')` |Does a prefix on the given field with the given prefix|
424+
|`fulltext('searchWord', options)` |Does a query_string query on the Fulltext index using the searchword and additional [options](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html) to the query_string|
423425

424426
## moreLikeThis(like, fields, options)
425427

Tests/Functional/Eel/ElasticSearchQueryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ public function filterNodeByProperty(): void
191191
static::assertEquals(1, $resultCount);
192192
}
193193

194+
/**
195+
* @test
196+
*
197+
* @throws QueryBuildingException
198+
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
199+
* @throws \Flowpack\ElasticSearch\Exception
200+
* @throws \Neos\Flow\Http\Exception
201+
*/
202+
public function prefixFilter(): void
203+
{
204+
$resultCount = $this->getQueryBuilder()
205+
->log($this->getLogMessagePrefix(__METHOD__))
206+
->prefix('title', 'chi')
207+
->count();
208+
static::assertEquals(2, $resultCount);
209+
}
210+
194211
/**
195212
* @test
196213
*/

0 commit comments

Comments
 (0)