Skip to content

Commit 94d0fc6

Browse files
Merge pull request #328 from daniellienert/feature/refactor-to-query-time-boosting
!!! FEATURE: Refactor index-time boosting to query-time boosting
2 parents 9038c0d + 14ce893 commit 94d0fc6

5 files changed

Lines changed: 40 additions & 16 deletions

File tree

Classes/Driver/AbstractQuery.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ abstract class AbstractQuery implements QueryInterface, \JsonSerializable, \Arra
3636
*/
3737
protected $unsupportedFieldsInCountRequest = [];
3838

39+
/**
40+
* Default parameters for the query_string filter used for fulltext search
41+
*
42+
* @var array
43+
*/
44+
protected $queryStringParameters = [];
45+
3946
/**
4047
* @param array $request
4148
* @param array $unsupportedFieldsInCountRequest
49+
* @param array $queryStringParameters
4250
*/
43-
public function __construct(array $request, array $unsupportedFieldsInCountRequest)
51+
public function __construct(array $request, array $unsupportedFieldsInCountRequest, array $queryStringParameters)
4452
{
4553
$this->request = $request;
4654
$this->unsupportedFieldsInCountRequest = $unsupportedFieldsInCountRequest;
55+
$this->queryStringParameters = $queryStringParameters;
4756
}
4857

4958
/**
@@ -78,10 +87,11 @@ public function toArray(): array
7887

7988
/**
8089
* {@inheritdoc}
90+
* @throws \JsonException
8191
*/
8292
public function getRequestAsJson(): string
8393
{
84-
return json_encode($this);
94+
return json_encode($this, JSON_THROW_ON_ERROR, 512);
8595
}
8696

8797
/**

Classes/Driver/Version6/Query/FilteredQuery.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ public function from(int $size): void
5959
public function fulltext(string $searchWord, array $options = []): void
6060
{
6161
$this->appendAtPath('query.bool.must', [
62-
'query_string' => array_merge($options, [
63-
'query' => $searchWord,
64-
'fields' => ['__fulltext*']
65-
])
62+
'query_string' => array_merge(
63+
$this->queryStringParameters,
64+
$options,
65+
[ 'query' => $searchWord ]
66+
)
6667
]);
6768
}
6869

Configuration/NodeTypes.Override.Document.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,18 @@
2424
properties:
2525
'h1':
2626
type: text
27-
boost: 20
2827
'h2':
2928
type: text
30-
boost: 12
3129
'h3':
3230
type: text
33-
boost: 10
3431
'h4':
3532
type: text
36-
boost: 5
3733
'h5':
3834
type: text
39-
boost: 3
4035
'h6':
4136
type: text
42-
boost: 2
4337
'text':
4438
type: text
45-
boost: 1
4639
'_hiddenInIndex':
4740
search:
4841
indexing: '${node.hiddenInIndex}'

Configuration/Settings.yaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Flowpack:
22
ElasticSearch:
33
ContentRepositoryAdaptor:
44
command:
5-
useSubProcesses: false
5+
useSubProcesses: true
66
indexing:
77
batchSize:
88
elements: 500
@@ -39,6 +39,7 @@ Flowpack:
3939
lt: now
4040
_source:
4141
- '__path'
42+
4243
unsupportedFieldsInCountRequest:
4344
- '_source'
4445
- 'sort'
@@ -48,6 +49,21 @@ Flowpack:
4849
- 'aggs'
4950
- 'aggregations'
5051
- 'suggest'
52+
53+
# Parameters for the query string query used by the fullText() operation
54+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-multi-field
55+
# for all available parameters
56+
queryStringParameters:
57+
default_operator: or
58+
fields:
59+
- __fulltext.h1^20
60+
- __fulltext.h2^12
61+
- __fulltext.h3^10
62+
- __fulltext.h4^5
63+
- __fulltext.h5^3
64+
- __fulltext.h6^2
65+
- __fulltext.text^1
66+
5167
document:
5268
className: 'Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\Version6\DocumentDriver'
5369
indexer:

Tests/Unit/Eel/ElasticSearchQueryBuilderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ public function setUp(): void
9090
];
9191
$unsupportedFieldsInCountRequest = ['fields', 'sort', 'from', 'size', 'highlight', 'aggs', 'aggregations'];
9292

93+
$queryStringParameters = [
94+
'fields' => ['__fulltext.h1^2']
95+
];
96+
9397
$this->inject($this->queryBuilder, 'elasticSearchClient', $elasticsearchClient);
94-
$this->inject($this->queryBuilder, 'request', new FilteredQuery($request, $unsupportedFieldsInCountRequest));
98+
$this->inject($this->queryBuilder, 'request', new FilteredQuery($request, $unsupportedFieldsInCountRequest, $queryStringParameters));
9599

96-
$query = new FilteredQuery($this->queryBuilder->getRequest()->toArray(), []);
100+
$query = new FilteredQuery($this->queryBuilder->getRequest()->toArray(), [], []);
97101
$this->inject($this->queryBuilder, 'request', $query);
98102
$this->queryBuilder->query($node);
99103
}

0 commit comments

Comments
 (0)