Skip to content

Commit d1c650d

Browse files
authored
Merge pull request #19 from janaculenova/main
Added Functions Query and Function Score Query
2 parents 56bd0bb + 0d3f401 commit d1c650d

File tree

6 files changed

+238
-1
lines changed

6 files changed

+238
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
"symfony/thanks": false
5151
}
5252
}
53-
}
53+
}

src/Query/FunctionScoreQuery.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Query;
6+
7+
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
8+
9+
class FunctionScoreQuery implements QueryInterface
10+
{
11+
protected ?string $boost = null;
12+
13+
protected ?string $boostMode = null;
14+
15+
private ?array $functions = null;
16+
17+
/**
18+
* @param array[]|string[] $fields
19+
*/
20+
public function __construct(
21+
protected array $fields,
22+
protected string $query,
23+
) {
24+
}
25+
26+
public function setBoost(string $boost): self
27+
{
28+
$this->boost = $boost;
29+
30+
return $this;
31+
}
32+
33+
public function setBoostMode(string $boostMode): self
34+
{
35+
$this->boostMode = $boostMode;
36+
37+
return $this;
38+
}
39+
40+
public function setFunctions(array $functions): self
41+
{
42+
$this->functions = $functions;
43+
44+
return $this;
45+
}
46+
47+
public function build(): array
48+
{
49+
$build = [];
50+
if (null !== $this->boostMode) {
51+
$build['boost_mode'] = $this->boostMode;
52+
}
53+
54+
if (null !== $this->functions) {
55+
$build['functions'] = $this->functions;
56+
}
57+
58+
$build['query'] = [
59+
'query_string' => [
60+
'query' => $this->query,
61+
'fields' => $this->fields,
62+
],
63+
];
64+
65+
if (null !== $this->boost) {
66+
$build['query']['query_string'] = $this->boost;
67+
}
68+
69+
return [
70+
'function_score' => $build,
71+
];
72+
}
73+
}

src/Query/FunctionsQuery.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 Erichard\ElasticQueryBuilder\Query;
6+
7+
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
8+
9+
class FunctionsQuery implements QueryInterface
10+
{
11+
protected ?float $weight = null;
12+
13+
public function __construct(
14+
protected string $field,
15+
) {
16+
}
17+
18+
public function setWeight(float $weight): self
19+
{
20+
$this->weight = $weight;
21+
22+
return $this;
23+
}
24+
25+
public function build(): array
26+
{
27+
$functions = [];
28+
$functions['filter'] = [
29+
'term' => [
30+
'_index' => $this->field,
31+
],
32+
];
33+
34+
if (null !== $this->weight) {
35+
$functions['weight'] = $this->weight;
36+
}
37+
38+
return $functions;
39+
}
40+
}

src/Query/Query.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public static function multiMatch(array $fields, string $query): MultiMatchQuery
7676
return new MultiMatchQuery($fields, $query);
7777
}
7878

79+
public static function functionScoreQuery(array $fields, string $query): FunctionScoreQuery
80+
{
81+
return new FunctionScoreQuery($fields, $query);
82+
}
83+
84+
public static function functionsQuery(string $field): FunctionsQuery
85+
{
86+
return new FunctionsQuery($field);
87+
}
88+
7989
/**
8090
* @param float[]|int[] $position
8191
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Query;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class FunctionScoreQueryTest extends TestCase
10+
{
11+
public function testBuildFunctionScoreQuery(): void
12+
{
13+
$fields = ['column1', 'column2'];
14+
$query = '(*name*) OR name';
15+
16+
$functionScoreQuery = new FunctionScoreQuery($fields, $query);
17+
18+
$response = [
19+
'function_score' => [
20+
'query' => [
21+
'query_string' => [
22+
'query' => '(*name*) OR name',
23+
'fields' => ['column1', 'column2'],
24+
],
25+
],
26+
],
27+
];
28+
29+
$this->assertEquals($response, $functionScoreQuery->build());
30+
}
31+
32+
public function testBuildFunctionScoreQuerySetParams(): void
33+
{
34+
$fields = ['column1', 'column2'];
35+
$query = '(*name*) OR name';
36+
37+
$functionScoreQuery = new FunctionScoreQuery($fields, $query);
38+
$functionScoreQuery->setBoostMode('multiply');
39+
$functionScoreQuery->setFunctions($this->functions());
40+
41+
$response = [
42+
'function_score' => [
43+
'boost_mode' => 'multiply',
44+
'functions' => [
45+
'filter' => [
46+
'term' => [
47+
'_index' => 'column2',
48+
],
49+
],
50+
'weight' => 2.50,
51+
],
52+
'query' => [
53+
'query_string' => [
54+
'query' => '(*name*) OR name',
55+
'fields' => ['column1', 'column2'],
56+
],
57+
],
58+
],
59+
];
60+
61+
$this->assertEquals($response, $functionScoreQuery->build());
62+
}
63+
64+
private function functions(): array
65+
{
66+
$functions = new FunctionsQuery('column2');
67+
$functions->setWeight(2.50);
68+
69+
return $functions->build();
70+
}
71+
}

tests/Query/FunctionsQueryTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Erichard\ElasticQueryBuilder\Query;
6+
7+
use Erichard\ElasticQueryBuilder\Query\FunctionsQuery;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class FunctionsQueryTest extends TestCase
11+
{
12+
public function testFunctionsQuery(): void
13+
{
14+
$functionsQuery = new FunctionsQuery('column1');
15+
16+
$response = [
17+
'filter' => [
18+
'term' => [
19+
'_index' => 'column1',
20+
],
21+
],
22+
];
23+
24+
$this->assertEquals($response, $functionsQuery->build());
25+
}
26+
27+
public function testFunctionsQuerySetWeight(): void
28+
{
29+
$functionsQuery = new FunctionsQuery('column1');
30+
$functionsQuery->setWeight(2.50);
31+
32+
$response = [
33+
'filter' => [
34+
'term' => [
35+
'_index' => 'column1',
36+
],
37+
],
38+
'weight' => 2.50,
39+
];
40+
41+
$this->assertEquals($response, $functionsQuery->build());
42+
}
43+
}

0 commit comments

Comments
 (0)