Skip to content

Commit b0fe848

Browse files
committed
Merge branch 'main' of github.com:janaculenova/elasticsearch-query-builder
2 parents 56bd0bb + 9ed9116 commit b0fe848

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ 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+
return new FunctionScoreQuery($fields, $query);
81+
}
82+
83+
public static function functionsQuery(string $field): FunctionsQuery
84+
{
85+
return new FunctionsQuery($field);
86+
}
87+
7988
/**
8089
* @param float[]|int[] $position
8190
*/

0 commit comments

Comments
 (0)