|
| 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 | +} |
0 commit comments