Skip to content

Commit e1890de

Browse files
authored
Merge pull request #135 from apisearch-io/feature/score-strategy-multi-filter
Added multi-filter score-strategy
2 parents 42306e3 + 069e478 commit e1890de

10 files changed

+142
-43
lines changed

.phpunit.result.cache

+1-1
Large diffs are not rendered by default.

Query/ScoreStrategy.php

+45-6
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ class ScoreStrategy implements HttpTransportable
183183
*/
184184
private $filter;
185185

186+
/**
187+
* Filters.
188+
*
189+
* @var Filter[]
190+
*/
191+
private $filters = [];
192+
186193
/**
187194
* @var float
188195
*
@@ -256,6 +263,14 @@ public function getFilter(): ? Filter
256263
return $this->filter;
257264
}
258265

266+
/**
267+
* @return Filter[]
268+
*/
269+
public function getFilters(): array
270+
{
271+
return $this->filters;
272+
}
273+
259274
/**
260275
* Create empty.
261276
*
@@ -267,8 +282,6 @@ public static function createDefault(): ScoreStrategy
267282
}
268283

269284
/**
270-
* Create default field scoring.
271-
*
272285
* @param string $field
273286
* @param float $factor
274287
* @param float $missing
@@ -302,8 +315,6 @@ public static function createFieldBoosting(
302315
}
303316

304317
/**
305-
* Create custom function scoring.
306-
*
307318
* @param string $function
308319
* @param float $weight
309320
* @param Filter $filter
@@ -328,8 +339,6 @@ public static function createCustomFunction(
328339
}
329340

330341
/**
331-
* Create custom function scoring.
332-
*
333342
* @param float $weight
334343
* @param Filter $filter
335344
* @param bool $matchMainQuery
@@ -350,6 +359,29 @@ public static function createWeightFunction(
350359
return $scoreStrategy;
351360
}
352361

362+
/**
363+
* @param float $weight
364+
* @param Filter[] $filters
365+
* @param bool $matchMainQuery
366+
*
367+
* @return ScoreStrategy
368+
*/
369+
public static function createWeightMultiFilterFunction(
370+
float $weight = self::DEFAULT_WEIGHT,
371+
array $filters = [],
372+
bool $matchMainQuery = true
373+
): ScoreStrategy {
374+
$scoreStrategy = self::createDefault();
375+
$scoreStrategy->type = 'weight';
376+
$scoreStrategy->weight = $weight;
377+
$scoreStrategy->filters = array_map(function (Filter $filter) {
378+
return self::fixFilterFieldPath($filter);
379+
}, $filters);
380+
$scoreStrategy->configuration['match_main_query'] = $matchMainQuery;
381+
382+
return $scoreStrategy;
383+
}
384+
353385
/**
354386
* Create custom function scoring.
355387
*
@@ -427,6 +459,9 @@ public function toArray(): array
427459
->filter
428460
->toArray()
429461
: null),
462+
'filters' => array_map(function (Filter $filter) {
463+
return $filter->toArray();
464+
}, $this->filters),
430465
];
431466
}
432467

@@ -448,6 +483,10 @@ public static function createFromArray(array $array)
448483
? Filter::createFromArray($array['filter'])
449484
: null;
450485

486+
$scoreStrategy->filters = array_map(function (array $filterAsArray) {
487+
return Filter::createFromArray($filterAsArray);
488+
}, $array['filters'] ?? []);
489+
451490
return $scoreStrategy;
452491
}
453492
}

Result/Aggregation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function toArray(): array
314314
'highest_active_level' => 0 === $this->highestActiveLevel
315315
? null
316316
: $this->highestActiveLevel,
317-
'metadata' => $this->metadata
317+
'metadata' => $this->metadata,
318318
], function ($element) {
319319
return
320320
!(

Tests/Http/TCPClientTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Apisearch\Http\HttpAdapter;
2020
use Apisearch\Http\RetryMap;
2121
use Apisearch\Http\TCPClient;
22+
use Apisearch\Tests\ProphecyTrait;
2223
use PHPUnit\Framework\TestCase;
2324
use Prophecy\Argument;
2425

@@ -27,6 +28,8 @@
2728
*/
2829
class TCPClientTest extends TestCase
2930
{
31+
use ProphecyTrait;
32+
3033
/**
3134
* Test n query retries.
3235
*/

Tests/ProphecyTrait.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Apisearch PHP Client.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Apisearch\Tests;
17+
18+
use Prophecy\PhpUnit\ProphecyTrait as BaseProphecyTrait;
19+
20+
if (trait_exists(BaseProphecyTrait::class)) {
21+
trait ProphecyTrait
22+
{
23+
use BaseProphecyTrait;
24+
}
25+
} else {
26+
trait ProphecyTrait
27+
{
28+
}
29+
}

Tests/Query/ScoreStrategyTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,28 @@ public function testDecay()
407407
$scoreStrategy->getScoreMode()
408408
);
409409
}
410+
411+
public function testWeightMultiFilter()
412+
{
413+
$scoreStrategy = ScoreStrategy::createWeightMultiFilterFunction(
414+
20,
415+
[],
416+
true
417+
);
418+
419+
$this->assertNull($scoreStrategy->getFilter());
420+
$this->assertEmpty($scoreStrategy->getFilters());
421+
422+
$scoreStrategy = ScoreStrategy::createWeightMultiFilterFunction(
423+
20,
424+
[
425+
Filter::create('1', ['2'], Filter::AT_LEAST_ONE, Filter::TYPE_FIELD),
426+
Filter::create('2', ['2'], Filter::AT_LEAST_ONE, Filter::TYPE_FIELD),
427+
],
428+
true
429+
);
430+
431+
$this->assertNull($scoreStrategy->getFilter());
432+
$this->assertCount(2, $scoreStrategy->getFilters());
433+
}
410434
}

Tests/Repository/HttpRepositoryTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Apisearch\Query\Query;
2424
use Apisearch\Repository\HttpRepository;
2525
use Apisearch\Repository\RepositoryReference;
26+
use Apisearch\Tests\ProphecyTrait;
2627
use PHPUnit\Framework\TestCase;
2728
use Prophecy\Argument;
2829

@@ -31,6 +32,8 @@
3132
*/
3233
class HttpRepositoryTest extends TestCase
3334
{
35+
use ProphecyTrait;
36+
3437
/**
3538
* Test add, delete and query items by UUID.
3639
*/

Tests/Result/AggregationTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testConstruct()
3737
['1', '2'],
3838
[
3939
'a' => 1,
40-
'b' => 2
40+
'b' => 2,
4141
]
4242
);
4343

@@ -52,7 +52,7 @@ public function testConstruct()
5252
$this->assertFalse($aggregation->isEmpty());
5353
$this->assertEquals([
5454
'a' => 1,
55-
'b' => 2
55+
'b' => 2,
5656
], $aggregation->getMetadata());
5757
}
5858

@@ -193,7 +193,7 @@ public function testToArrayDefaultValues()
193193
*/
194194
public function testToArrayAllValues()
195195
{
196-
$aggregation = new Aggregation('name', Filter::MUST_ALL, 100, ['1'], ['a' => 1, 'b' => 2 ]);
196+
$aggregation = new Aggregation('name', Filter::MUST_ALL, 100, ['1'], ['a' => 1, 'b' => 2]);
197197
$aggregation->addCounter('1', 10);
198198
$aggregation->addCounter('2', 10);
199199
$this->assertEquals(
@@ -208,7 +208,7 @@ public function testToArrayAllValues()
208208
'1',
209209
],
210210
'total_elements' => 100,
211-
'metadata' => ['a' => 1, 'b' => 2 ]
211+
'metadata' => ['a' => 1, 'b' => 2],
212212
],
213213
$aggregation->toArray()
214214
);
@@ -242,7 +242,7 @@ public function testCreateFromArrayAllValues()
242242
'1',
243243
],
244244
'total_elements' => 100,
245-
'metadata' => ['a' => 1, 'b' => 2 ]
245+
'metadata' => ['a' => 1, 'b' => 2],
246246
]);
247247
$this->assertEquals('agg1', $aggregation->getName());
248248
$this->assertCount(2, $aggregation->getCounters());

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"nesbot/carbon": "^1.22 || ^2.0"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "^7 | ^9"
20+
"phpunit/phpunit": "^7 | ^9",
21+
"phpspec/prophecy-phpunit": "^1 | ^2"
2122
},
2223
"autoload": {
2324
"psr-4": {

phpunit.xml

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="false"
7-
convertNoticesToExceptions="false"
8-
convertWarningsToExceptions="false"
9-
processIsolation="false"
10-
stopOnFailure="true"
11-
bootstrap="vendor/autoload.php"
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
convertErrorsToExceptions="false"
8+
convertNoticesToExceptions="false"
9+
convertWarningsToExceptions="false"
10+
processIsolation="false"
11+
stopOnFailure="true"
12+
bootstrap="vendor/autoload.php"
13+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
1214
>
13-
<testsuites>
14-
<testsuite name="Test Suite">
15-
<directory>./Tests</directory>
16-
</testsuite>
17-
</testsuites>
18-
19-
<filter>
20-
<whitelist>
21-
<directory>./</directory>
22-
<exclude>
23-
<directory>./Tests/</directory>
24-
<directory>./Resources/</directory>
25-
<directory>./DependencyInjection/</directory>
26-
<directory>./vendor/</directory>
27-
</exclude>
28-
</whitelist>
29-
</filter>
30-
31-
</phpunit>
15+
<coverage>
16+
<include>
17+
<directory>./</directory>
18+
</include>
19+
<exclude>
20+
<directory>./Tests/</directory>
21+
<directory>./Resources/</directory>
22+
<directory>./DependencyInjection/</directory>
23+
<directory>./vendor/</directory>
24+
</exclude>
25+
</coverage>
26+
<testsuites>
27+
<testsuite name="Test Suite">
28+
<directory>./Tests</directory>
29+
</testsuite>
30+
</testsuites>
31+
</phpunit>

0 commit comments

Comments
 (0)