Skip to content

Commit 0d3f401

Browse files
janaculenovaiana.culenova
andauthored
Function queries - test (#5)
* QunctionScoreQuery + FunctionsQuery * lint * composer.json * composer.json * lin+stan * composer.json * composer.json * composer.json original * clean up * setters * lint * Fixed forgotten arguments * lint * lint * tests Co-authored-by: iana.culenova <jana.culenova@websupport.sk>
1 parent b0fe848 commit 0d3f401

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

src/Query/Query.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ 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 {
79+
public static function functionScoreQuery(array $fields, string $query): FunctionScoreQuery
80+
{
8081
return new FunctionScoreQuery($fields, $query);
8182
}
8283

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)