Skip to content

Commit 57a70c9

Browse files
committed
!!! TASK: Fix highlighting by using anylyzed fulltext fields instead of _all
1 parent 9d3d861 commit 57a70c9

3 files changed

Lines changed: 51 additions & 9 deletions

File tree

Classes/Driver/Version5/Query/FilteredQuery.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\AbstractQuery;
1515
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
16+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\QueryBuildingException;
1617

1718
/**
1819
* Filtered query for elastic version 5
@@ -22,6 +23,7 @@ class FilteredQuery extends AbstractQuery
2223

2324
/**
2425
* {@inheritdoc}
26+
* @throws QueryBuildingException
2527
*/
2628
public function getCountRequestAsJson()
2729
{
@@ -57,7 +59,10 @@ public function from($size)
5759
public function fulltext(string $searchWord, array $options = [])
5860
{
5961
$this->appendAtPath('query.bool.must', [
60-
'query_string' => array_merge($options, ['query' => $searchWord])
62+
'query_string' => array_merge($options, [
63+
'query' => $searchWord,
64+
'fields' => ['__fulltext*']
65+
])
6166
]);
6267
}
6368

@@ -67,7 +72,7 @@ public function fulltext(string $searchWord, array $options = [])
6772
public function queryFilter($filterType, $filterOptions, $clauseType = 'must')
6873
{
6974
if (!in_array($clauseType, ['must', 'should', 'must_not'])) {
70-
throw new Exception\QueryBuildingException('The given clause type "' . $clauseType . '" is not supported. Must be one of "must", "should", "must_not".', 1383716082);
75+
throw new QueryBuildingException('The given clause type "' . $clauseType . '" is not supported. Must be one of "must", "should", "must_not".', 1383716082);
7176
}
7277

7378
$this->appendAtPath('query.bool.filter.bool.' . $clauseType, [$filterType => $filterOptions]);

Configuration/NodeTypes.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,31 @@
136136
properties:
137137
'h1':
138138
type: string
139-
include_in_all: true
139+
index: analyzed
140140
boost: 20
141141
'h2':
142142
type: string
143-
include_in_all: true
143+
index: analyzed
144144
boost: 12
145145
'h3':
146146
type: string
147-
include_in_all: true
147+
index: analyzed
148148
boost: 10
149149
'h4':
150150
type: string
151-
include_in_all: true
151+
index: analyzed
152152
boost: 5
153153
'h5':
154154
type: string
155-
include_in_all: true
155+
index: analyzed
156156
boost: 3
157157
'h6':
158158
type: string
159-
include_in_all: true
159+
index: analyzed
160160
boost: 2
161161
'text':
162162
type: string
163-
include_in_all: true
163+
index: analyzed
164164
boost: 1
165165
'_hiddenInIndex':
166166
search:

Tests/Functional/Eel/ElasticSearchQueryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,43 @@ public function elasticSearchQueryBuilderStartsClean()
120120
$this->assertEquals($cleanRequestArray, $query2->getRequest()->toArray());
121121
}
122122

123+
/**
124+
* @test
125+
*/
126+
public function fullTextSearchReturnsTheDocumentNode()
127+
{
128+
/** @var ElasticSearchQueryResult $result */
129+
$result = $this->getQueryBuilder()
130+
->fulltext('circum*')
131+
->execute();
132+
$this->assertEquals(1, $result->count());
133+
134+
/** @var NodeInterface $node */
135+
$node = $result->current();
136+
$this->assertEquals('Neos.NodeTypes:Page', $node->getNodeType()->getName());
137+
$this->assertEquals('test-node-1', $node->getName());
138+
}
139+
140+
/**
141+
* @test
142+
*/
143+
public function fullTextHighlighting()
144+
{
145+
/** @var ElasticSearchQueryBuilder $queryBuilder */
146+
$queryBuilder = $this->getQueryBuilder();
147+
148+
/** @var NodeInterface $resultNode */
149+
$resultNode = $queryBuilder
150+
->fulltext('whistles')
151+
->log($this->getLogMessagePrefix(__METHOD__))
152+
->execute()
153+
->current();
154+
$searchHitForNode = $queryBuilder->getFullElasticSearchHitForNode($resultNode);
155+
$highlightedText = current($searchHitForNode['highlight']['__fulltext.text']);
156+
$expected = 'A Scout smiles and <em>whistles</em> under all circumstances.';
157+
$this->assertEquals($expected, $highlightedText);
158+
}
159+
123160
/**
124161
* @test
125162
*/

0 commit comments

Comments
 (0)