Skip to content

Commit 0473d37

Browse files
janaculenovaiana.culenova
andauthored
Function queries
* QunctionScoreQuery + FunctionsQuery * lint * composer.json * composer.json * lin+stan * composer.json * composer.json * composer.json original * clean up * setters * lint Co-authored-by: iana.culenova <jana.culenova@websupport.sk>
1 parent 100a28e commit 0473d37

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ public static function multiMatch(array $fields, string $query): MultiMatchQuery
7676
return new MultiMatchQuery($fields, $query);
7777
}
7878

79+
public static function functionScoreQuery(
80+
array $fields,
81+
string $query,
82+
string $boost = null,
83+
string $boostMode = null
84+
): FunctionScoreQuery {
85+
return new FunctionScoreQuery($fields, $query, $boost, $boostMode);
86+
}
87+
88+
public static function functionsQuery(?string $field = null, ?float $weight = null): FunctionsQuery
89+
{
90+
return new FunctionsQuery($field, $weight);
91+
}
92+
7993
/**
8094
* @param float[]|int[] $position
8195
*/

0 commit comments

Comments
 (0)