Skip to content

Commit 2bcb7fb

Browse files
Spamerczclaude
andcommitted
feat(query): add constant score compound query
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f3c4313 commit 2bcb7fb

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

doc/02-query-objects.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,19 @@ new \Spameri\ElasticQuery\Query\WildCard(
297297

298298
Wrap other queries to combine, filter, or modify their scoring.
299299

300+
##### ConstantScore Query
301+
Wraps a filter so all matching docs share the same score.
302+
- Class: `\Spameri\ElasticQuery\Query\ConstantScore`
303+
- [Documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html)
304+
- [Implementation](https://github.com/Spameri/ElasticQuery/blob/master/src/Query/ConstantScore.php)
305+
306+
```php
307+
new \Spameri\ElasticQuery\Query\ConstantScore(
308+
filter: new \Spameri\ElasticQuery\Query\Term('status', 'active'),
309+
boost: 1.2,
310+
);
311+
```
312+
300313
##### Boosting Query
301314
Match `positive` docs but lower the score of `negative` matches.
302315
- Class: `\Spameri\ElasticQuery\Query\Boosting`

src/Query/ConstantScore.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Spameri\ElasticQuery\Query;
6+
7+
/**
8+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
9+
*/
10+
class ConstantScore implements \Spameri\ElasticQuery\Query\LeafQueryInterface
11+
{
12+
13+
public function __construct(
14+
private \Spameri\ElasticQuery\Query\LeafQueryInterface $filter,
15+
private float $boost = 1.0,
16+
)
17+
{
18+
}
19+
20+
21+
public function key(): string
22+
{
23+
return 'constant_score_' . $this->filter->key();
24+
}
25+
26+
27+
/**
28+
* @return array<string, array<string, mixed>>
29+
*/
30+
public function toArray(): array
31+
{
32+
return [
33+
'constant_score' => [
34+
'filter' => $this->filter->toArray(),
35+
'boost' => $this->boost,
36+
],
37+
];
38+
}
39+
40+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SpameriTests\ElasticQuery\Query;
4+
5+
require_once __DIR__ . '/../../bootstrap.php';
6+
7+
8+
class ConstantScore extends \Tester\TestCase
9+
{
10+
11+
private const INDEX = 'spameri_test_query_constant_score';
12+
13+
14+
public function setUp(): void
15+
{
16+
$ch = \curl_init();
17+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
18+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
19+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
20+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
21+
22+
\curl_exec($ch);
23+
}
24+
25+
26+
public function testToArray(): void
27+
{
28+
$cs = new \Spameri\ElasticQuery\Query\ConstantScore(
29+
filter: new \Spameri\ElasticQuery\Query\Term('status', 'active'),
30+
boost: 1.2,
31+
);
32+
33+
$array = $cs->toArray();
34+
35+
\Tester\Assert::same(1.2, $array['constant_score']['boost']);
36+
\Tester\Assert::same('active', $array['constant_score']['filter']['term']['status']['value']);
37+
}
38+
39+
40+
public function testKey(): void
41+
{
42+
$cs = new \Spameri\ElasticQuery\Query\ConstantScore(
43+
new \Spameri\ElasticQuery\Query\Term('status', 'active'),
44+
);
45+
46+
\Tester\Assert::same('constant_score_term_status_active', $cs->key());
47+
}
48+
49+
50+
public function tearDown(): void
51+
{
52+
$ch = \curl_init();
53+
\curl_setopt($ch, \CURLOPT_URL, \ELASTICSEARCH_HOST . '/' . self::INDEX);
54+
\curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
55+
\curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
56+
\curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
57+
58+
\curl_exec($ch);
59+
}
60+
61+
}
62+
63+
(new ConstantScore())->run();

0 commit comments

Comments
 (0)