Skip to content

Added multi-filter score-strategy #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache

Large diffs are not rendered by default.

51 changes: 45 additions & 6 deletions Query/ScoreStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ class ScoreStrategy implements HttpTransportable
*/
private $filter;

/**
* Filters.
*
* @var Filter[]
*/
private $filters = [];

/**
* @var float
*
Expand Down Expand Up @@ -256,6 +263,14 @@ public function getFilter(): ? Filter
return $this->filter;
}

/**
* @return Filter[]
*/
public function getFilters(): array
{
return $this->filters;
}

/**
* Create empty.
*
Expand All @@ -267,8 +282,6 @@ public static function createDefault(): ScoreStrategy
}

/**
* Create default field scoring.
*
* @param string $field
* @param float $factor
* @param float $missing
Expand Down Expand Up @@ -302,8 +315,6 @@ public static function createFieldBoosting(
}

/**
* Create custom function scoring.
*
* @param string $function
* @param float $weight
* @param Filter $filter
Expand All @@ -328,8 +339,6 @@ public static function createCustomFunction(
}

/**
* Create custom function scoring.
*
* @param float $weight
* @param Filter $filter
* @param bool $matchMainQuery
Expand All @@ -350,6 +359,29 @@ public static function createWeightFunction(
return $scoreStrategy;
}

/**
* @param float $weight
* @param Filter[] $filters
* @param bool $matchMainQuery
*
* @return ScoreStrategy
*/
public static function createWeightMultiFilterFunction(
float $weight = self::DEFAULT_WEIGHT,
array $filters = [],
bool $matchMainQuery = true
): ScoreStrategy {
$scoreStrategy = self::createDefault();
$scoreStrategy->type = 'weight';
$scoreStrategy->weight = $weight;
$scoreStrategy->filters = array_map(function (Filter $filter) {
return self::fixFilterFieldPath($filter);
}, $filters);
$scoreStrategy->configuration['match_main_query'] = $matchMainQuery;

return $scoreStrategy;
}

/**
* Create custom function scoring.
*
Expand Down Expand Up @@ -427,6 +459,9 @@ public function toArray(): array
->filter
->toArray()
: null),
'filters' => array_map(function (Filter $filter) {
return $filter->toArray();
}, $this->filters),
];
}

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

$scoreStrategy->filters = array_map(function (array $filterAsArray) {
return Filter::createFromArray($filterAsArray);
}, $array['filters'] ?? []);

return $scoreStrategy;
}
}
2 changes: 1 addition & 1 deletion Result/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function toArray(): array
'highest_active_level' => 0 === $this->highestActiveLevel
? null
: $this->highestActiveLevel,
'metadata' => $this->metadata
'metadata' => $this->metadata,
], function ($element) {
return
!(
Expand Down
3 changes: 3 additions & 0 deletions Tests/Http/TCPClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Apisearch\Http\HttpAdapter;
use Apisearch\Http\RetryMap;
use Apisearch\Http\TCPClient;
use Apisearch\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

Expand All @@ -27,6 +28,8 @@
*/
class TCPClientTest extends TestCase
{
use ProphecyTrait;

/**
* Test n query retries.
*/
Expand Down
29 changes: 29 additions & 0 deletions Tests/ProphecyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Apisearch PHP Client.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Apisearch\Tests;

use Prophecy\PhpUnit\ProphecyTrait as BaseProphecyTrait;

if (trait_exists(BaseProphecyTrait::class)) {
trait ProphecyTrait
{
use BaseProphecyTrait;
}
} else {
trait ProphecyTrait
{
}
}
24 changes: 24 additions & 0 deletions Tests/Query/ScoreStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,28 @@ public function testDecay()
$scoreStrategy->getScoreMode()
);
}

public function testWeightMultiFilter()
{
$scoreStrategy = ScoreStrategy::createWeightMultiFilterFunction(
20,
[],
true
);

$this->assertNull($scoreStrategy->getFilter());
$this->assertEmpty($scoreStrategy->getFilters());

$scoreStrategy = ScoreStrategy::createWeightMultiFilterFunction(
20,
[
Filter::create('1', ['2'], Filter::AT_LEAST_ONE, Filter::TYPE_FIELD),
Filter::create('2', ['2'], Filter::AT_LEAST_ONE, Filter::TYPE_FIELD),
],
true
);

$this->assertNull($scoreStrategy->getFilter());
$this->assertCount(2, $scoreStrategy->getFilters());
}
}
3 changes: 3 additions & 0 deletions Tests/Repository/HttpRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Apisearch\Query\Query;
use Apisearch\Repository\HttpRepository;
use Apisearch\Repository\RepositoryReference;
use Apisearch\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

Expand All @@ -31,6 +32,8 @@
*/
class HttpRepositoryTest extends TestCase
{
use ProphecyTrait;

/**
* Test add, delete and query items by UUID.
*/
Expand Down
10 changes: 5 additions & 5 deletions Tests/Result/AggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testConstruct()
['1', '2'],
[
'a' => 1,
'b' => 2
'b' => 2,
]
);

Expand All @@ -52,7 +52,7 @@ public function testConstruct()
$this->assertFalse($aggregation->isEmpty());
$this->assertEquals([
'a' => 1,
'b' => 2
'b' => 2,
], $aggregation->getMetadata());
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public function testToArrayDefaultValues()
*/
public function testToArrayAllValues()
{
$aggregation = new Aggregation('name', Filter::MUST_ALL, 100, ['1'], ['a' => 1, 'b' => 2 ]);
$aggregation = new Aggregation('name', Filter::MUST_ALL, 100, ['1'], ['a' => 1, 'b' => 2]);
$aggregation->addCounter('1', 10);
$aggregation->addCounter('2', 10);
$this->assertEquals(
Expand All @@ -208,7 +208,7 @@ public function testToArrayAllValues()
'1',
],
'total_elements' => 100,
'metadata' => ['a' => 1, 'b' => 2 ]
'metadata' => ['a' => 1, 'b' => 2],
],
$aggregation->toArray()
);
Expand Down Expand Up @@ -242,7 +242,7 @@ public function testCreateFromArrayAllValues()
'1',
],
'total_elements' => 100,
'metadata' => ['a' => 1, 'b' => 2 ]
'metadata' => ['a' => 1, 'b' => 2],
]);
$this->assertEquals('agg1', $aggregation->getName());
$this->assertCount(2, $aggregation->getCounters());
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"nesbot/carbon": "^1.22 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^7 | ^9"
"phpunit/phpunit": "^7 | ^9",
"phpspec/prophecy-phpunit": "^1 | ^2"
},
"autoload": {
"psr-4": {
Expand Down
58 changes: 29 additions & 29 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
processIsolation="false"
stopOnFailure="true"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
processIsolation="false"
stopOnFailure="true"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./Tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Tests/</directory>
<directory>./Resources/</directory>
<directory>./DependencyInjection/</directory>
<directory>./vendor/</directory>
</exclude>
</whitelist>
</filter>

</phpunit>
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./Tests/</directory>
<directory>./Resources/</directory>
<directory>./DependencyInjection/</directory>
<directory>./vendor/</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>./Tests</directory>
</testsuite>
</testsuites>
</phpunit>