Skip to content
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
10 changes: 2 additions & 8 deletions src/Concerns/DecoratesBoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use Closure;
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreItem;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreOptions;
use Ensi\LaravelElasticQuery\Contracts\MatchOptions;
use Ensi\LaravelElasticQuery\Contracts\MoreLikeOptions;
use Ensi\LaravelElasticQuery\Contracts\MoreLikeThis;
use Ensi\LaravelElasticQuery\Contracts\MultiMatchOptions;
use Ensi\LaravelElasticQuery\Contracts\WildcardOptions;
use Ensi\LaravelElasticQuery\Filtering\BoolQueryBuilder;
use Ensi\LaravelElasticQuery\Filtering\Criterias\FunctionScore;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Traits\ForwardsCalls;

Expand Down Expand Up @@ -161,12 +160,7 @@ public function whereBetween(string $field, mixed $from, mixed $to): static
return $this;
}

/**
* @param array<FunctionScoreItem> $functions
* @param ?DSLAware $query
* @param ?FunctionScoreOptions $options
*/
public function addFunctionScore(array $functions, ?DSLAware $query = null, ?FunctionScoreOptions $options = null): static
public function orFunctionScore(FunctionScore $functionScore): static
{
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());

Expand Down
8 changes: 2 additions & 6 deletions src/Contracts/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ensi\LaravelElasticQuery\Contracts;

use Closure;
use Ensi\LaravelElasticQuery\Filtering\Criterias\FunctionScore;
use Illuminate\Contracts\Support\Arrayable;

interface BoolQuery
Expand Down Expand Up @@ -47,12 +48,7 @@ public function whereMoreLikeThis(array $fields, MoreLikeThis $likeThis, ?MoreLi

public function whereBetween(string $field, mixed $from, mixed $to): static;

/**
* @param array<FunctionScoreItem> $functions
* @param ?DSLAware $query
* @param ?FunctionScoreOptions $options
*/
public function addFunctionScore(array $functions, ?DSLAware $query = null, ?FunctionScoreOptions $options = null): static;
public function orFunctionScore(FunctionScore $functionScore): static;

public function pinned(array $ids, ?DSLAware $query = null): static;
}
6 changes: 3 additions & 3 deletions src/Contracts/FunctionScoreItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
class FunctionScoreItem implements Arrayable
{
public function __construct(
private int $weight,
private Criteria $filter,
protected int $weight,
protected Criteria $filter,
) {
}

public function toArray(): array
{
return [
'weight' => $this->weight,
'filter' => $this->filter->toDSL(),
'weight' => $this->weight,
];
}
}
2 changes: 1 addition & 1 deletion src/Contracts/FunctionScoreOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class FunctionScoreOptions implements Arrayable
{
public function __construct(private array $options = [])
public function __construct(protected array $options = [])
{
}

Expand Down
20 changes: 20 additions & 0 deletions src/Contracts/FunctionScoreScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Ensi\LaravelElasticQuery\Contracts;

class FunctionScoreScript implements DSLAware
{
public function __construct(
protected string $source,
protected array $params = [],
) {
}

public function toDSL(): array
{
return ['script' => array_filter([
'source' => $this->source,
'params' => $this->params,
])];
}
}
11 changes: 2 additions & 9 deletions src/Filtering/BoolQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Ensi\LaravelElasticQuery\Contracts\BoolQuery;
use Ensi\LaravelElasticQuery\Contracts\Criteria;
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreItem;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreOptions;
use Ensi\LaravelElasticQuery\Contracts\MatchOptions;
use Ensi\LaravelElasticQuery\Contracts\MoreLikeOptions;
use Ensi\LaravelElasticQuery\Contracts\MoreLikeThis;
Expand Down Expand Up @@ -268,14 +266,9 @@ public function whereBetween(string $field, mixed $from, mixed $to): static
return $this;
}

/**
* @param array<FunctionScoreItem> $functions
* @param ?DSLAware $query
* @param ?FunctionScoreOptions $options
*/
public function addFunctionScore(array $functions, ?DSLAware $query = null, ?FunctionScoreOptions $options = null): static
public function orFunctionScore(FunctionScore $functionScore): static
{
$this->should->add(new FunctionScore($functions, $query, $options));
$this->should->add($functionScore);

return $this;
}
Expand Down
25 changes: 19 additions & 6 deletions src/Filtering/Criterias/FunctionScore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,43 @@
use Ensi\LaravelElasticQuery\Contracts\DSLAware;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreItem;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreOptions;
use Ensi\LaravelElasticQuery\Contracts\FunctionScoreScript;
use stdClass;
use Webmozart\Assert\Assert;

class FunctionScore implements Criteria
{
/**
* @param array<FunctionScoreItem> $functions
* @param FunctionScoreOptions|null $options
*/
public function __construct(
private array $functions,
private ?DSLAware $query = null,
private ?FunctionScoreOptions $options = null,
protected ?DSLAware $query = null,
protected ?FunctionScoreOptions $options = null,
protected array $functions = [],
protected ?FunctionScoreScript $scriptScore = null,
protected ?float $weight = null,
) {
array_map(fn ($function) => Assert::isInstanceOfAny($function, [FunctionScoreItem::class]), $functions);
Assert::allIsInstanceOfAny($functions, [FunctionScoreItem::class]);
}

public function toDSL(): array
{
$body = [
'query' => $this->query?->toDSL() ?? ['match_all' => new stdClass()],
'functions' => array_map(fn (FunctionScoreItem $function) => $function->toArray(), $this->functions),
];

if ($this->functions) {
$body['functions'] = array_map(fn (FunctionScoreItem $function) => $function->toArray(), $this->functions);
}

if ($this->scriptScore) {
$body['script_score'] = $this->scriptScore->toDSL();
}

if (!is_null($this->weight)) {
$body['weight'] = $this->weight;
}

if ($this->options) {
$body = array_merge($this->options->toArray(), $body);
}
Expand Down
23 changes: 11 additions & 12 deletions tests/IntegrationTests/Search/SearchQueryIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Ensi\LaravelElasticQuery\Contracts\MoreLikeThis;
use Ensi\LaravelElasticQuery\Contracts\ScoreMode;
use Ensi\LaravelElasticQuery\Contracts\SortableQuery;
use Ensi\LaravelElasticQuery\Filtering\Criterias\FunctionScore;
use Ensi\LaravelElasticQuery\Filtering\Criterias\Terms;
use Ensi\LaravelElasticQuery\Tests\Data\Models\ProductsIndex;
use Ensi\LaravelElasticQuery\Tests\IntegrationTestCase;
Expand Down Expand Up @@ -148,21 +149,19 @@
/** @var SearchIntegrationTestCase $this */

$result = ProductsIndex::query()
->addFunctionScore(
[
new FunctionScoreItem(
weight: 10,
filter: new Terms(
field: "tags",
values: ["drinks"],
),
),
],
->orFunctionScore(new FunctionScore(
options: FunctionScoreOptions::make(
scoreMode: ScoreMode::SUM,
boostMode: BoostMode::SUM
)
)
),
functions: [new FunctionScoreItem(
weight: 10,
filter: new Terms(
field: "tags",
values: ["drinks"],
),
)],
))
->get();

assertCount(6, $result);
Expand Down