Skip to content

Commit 35d0b0c

Browse files
authored
Merge pull request #395 from puchsteink/fulltext-search-with-simple-query-string
FEATURE: Support `.simpleQueryStringFulltext()` simple query string query for fulltext search
2 parents 307b252 + d79b7b6 commit 35d0b0c

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

Classes/Driver/QueryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public function from(int $size): void;
8181
*/
8282
public function fulltext(string $searchWord, array $options = []): void;
8383

84+
/**
85+
* Match the search word against the fulltext index using the elasticsearch
86+
* [simple query string query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html)
87+
*
88+
* @param string $searchWord
89+
* @param array $options Options to configure the query_string
90+
* @return void
91+
*/
92+
public function simpleQueryStringFulltext(string $searchWord, array $options = []): void;
93+
8494
/**
8595
* Configure Result Highlighting. Only makes sense in combination with fulltext(). By default, highlighting is enabled.
8696
* It can be disabled by calling "highlight(FALSE)".

Classes/Driver/Version6/Query/FilteredQuery.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public function fulltext(string $searchWord, array $options = []): void
6767
]);
6868
}
6969

70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function simpleQueryStringFulltext(string $searchWord, array $options = []): void
74+
{
75+
$this->appendAtPath('query.bool.must', [
76+
'simple_query_string' => array_merge(
77+
$this->queryStringParameters,
78+
$options,
79+
[ 'query' => $searchWord ]
80+
)
81+
]);
82+
}
83+
7084
/**
7185
* {@inheritdoc}
7286
*/

Classes/Eel/ElasticSearchQueryBuilder.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,9 @@ public function count(): int
672672
}
673673

674674
/**
675-
* Match the searchword against the fulltext index
675+
* Match the searchword against the fulltext index.
676+
*
677+
* NOTE: Please use {@see simpleQueryStringFulltext} instead, as it is more robust.
676678
*
677679
* @param string $searchWord
678680
* @param array $options Options to configure the query_string, see https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html
@@ -689,6 +691,32 @@ public function fulltext(string $searchWord, array $options = []): QueryBuilderI
689691
return $this;
690692
}
691693

694+
/**
695+
* Match the searchword against the fulltext index using the elasticsearch
696+
* [simple query string query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html).
697+
*
698+
* This method has two main benefits over {@see fulltext()}:
699+
* - Supports phrase searches like `"Neos and Flow"`, where
700+
* quotes mean "I want this exact phrase" (similar to many search engines).
701+
* - do not crash if the user does not enter a fully syntactically valid query
702+
* (invalid query parts are ignored).
703+
*
704+
* This is exactly what we want for a good search field behavior.
705+
*
706+
* @param string $searchWord
707+
* @param array $options Options to configure the query_string, see https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-query-string-query.html
708+
* @return QueryBuilderInterface
709+
* @api
710+
*/
711+
public function simpleQueryStringFulltext(string $searchWord, array $options = []): QueryBuilderInterface
712+
{
713+
// We automatically enable result highlighting when doing fulltext searches. It is up to the user to use this information or not use it.
714+
$this->request->simpleQueryStringFulltext($searchWord, $options);
715+
$this->request->highlight(150, 2);
716+
717+
return $this;
718+
}
719+
692720
/**
693721
* Adds a prefix filter to the query
694722
* See: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/query-dsl-prefix-query.html

Configuration/Settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Flowpack:
5353
- 'aggregations'
5454
- 'suggest'
5555

56-
# Parameters for the query string query used by the fullText() operation
56+
# Parameters for the query string query used by the fullText() and simpleQueryStringFulltext() operation
5757
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-multi-field
5858
# for all available parameters
5959
queryStringParameters:

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,19 @@ As **value**, the following methods accept a simple type, a node object or a Dat
441441
|`from(5)` |Return the results starting from the 6th one|
442442
|`prefix('propertyName', 'prefix', [clauseType])` |Adds a prefix filter on the given field with the given prefix|
443443
|`geoDistance(propertyName, geoPoint, distance, [clauseType])`. |Filters documents that include only hits that exists within a specific distance from a geo point.|
444-
|`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|
444+
|`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. Recommendation: **use simpleQueryStringFulltext instead, as it yields better results and is more tolerant to user input**.|
445+
|`simpleQueryStringFulltext('searchWord', options)` |Does a simple_query_string query on the Fulltext index using the searchword and additional [options](https://www.elastic.co/guide/en/elasticsearch/reference/8.3/query-dsl-simple-query-string-query.html) to the simple_query_string. Supports phrase matching like `"firstname lastname"` and tolerates broken input without exceptions (in contrast to `fulltext()`)|
445446
|`highlight(fragmentSize, fragmentCount, noMatchSize, field)` |Configure result highlighting for every fulltext field individually|
446447

447448
## Search Result Highlighting
448449

449-
When using the `.fulltext()` oprtator to do a fulltext, **highlight snippets** ([elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/highlighting.html#highlighting)) are automatically queried. By default snippets with 150 characters are queried for all available fulltext fields with a 150 character fallback text.
450+
When using the `.fulltext()` or `.simpleQueryStringFulltext()` operator to do a fulltext, **highlight snippets** ([elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/highlighting.html#highlighting)) are automatically queried. By default snippets with 150 characters are queried for all available fulltext fields with a 150 character fallback text.
450451

451452
To adjust this behavior you can first deactivate the default and then configure highlighting for every field individually:
452453

453454
Search.highlight(false).highlight(150, 2, 150, 'neos_fulltext.text').highlight(100, 1, 0, 'neos_fulltext.h2')
454455

455-
This deactivates the default highlighting and then queries 2 snipets of 150 characters each from hits in `neos_fulltext.text`, with a fallback to 150 charchters of the beginning of the text if no match was found and additional 100 characters from `neos_fulltext.h2` without a fallback.
456+
This deactivates the default highlighting and then queries 2 snipets of 150 characters each from hits in `neos_fulltext.text`, with a fallback to 150 characters of the beginning of the text if no match was found and additional 100 characters from `neos_fulltext.h2` without a fallback.
456457

457458
The highlight snippets can be accessed by
458459

0 commit comments

Comments
 (0)