Skip to content

Commit 199ce33

Browse files
committed
V7 add search type and boost
1 parent b6e8354 commit 199ce33

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

src/Concerns/InteractsWithIndex.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ abstract public function tiebreaker(): string;
2121

2222
abstract protected function indexName(): string;
2323

24-
abstract protected function searchType(): ?string;
25-
2624
protected function settings(): array
2725
{
2826
throw new Exception("Need to redefine the method");
@@ -31,17 +29,17 @@ protected function settings(): array
3129
/**
3230
* @see SearchIndex::search()
3331
*/
34-
public function search(array $dsl): array
32+
public function search(array $dsl, ?string $searchType = null): array
3533
{
36-
return $this->resolveClient()->search($this->indexName(), $dsl, $this->searchType());
34+
return $this->resolveClient()->search($this->indexName(), $dsl, $searchType);
3735
}
3836

3937
/**
4038
* @see SearchIndex::search()
4139
*/
42-
public function searchAsync(array $dsl): FutureArray
40+
public function searchAsync(array $dsl, ?string $searchType = null): FutureArray
4341
{
44-
return $this->resolveClient()->searchAsync($this->indexName(), $dsl);
42+
return $this->resolveClient()->searchAsync($this->indexName(), $dsl, $searchType);
4543
}
4644

4745
/**

src/Contracts/MultiMatchOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function make(
1515
?string $operator = null,
1616
?string $fuzziness = null,
1717
?string $minimumShouldMatch = null,
18-
?float $boost = null
18+
?float $boost = null,
1919
): static {
2020
Assert::nullOrOneOf($type, MatchType::cases());
2121
Assert::nullOrOneOf($operator, ['or', 'and']);

src/Contracts/SearchIndex.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ public function tiebreaker(): string;
1515
* Perform search query.
1616
*
1717
* @param array $dsl
18+
* @param string|null $searchType
1819
* @return array
1920
*/
20-
public function search(array $dsl): array;
21+
public function search(array $dsl, ?string $searchType = null): array;
2122

2223
/**
2324
* Perform delete by query.

src/ElasticClient.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ public function search(string $indexName, array $dsl, ?string $searchType = null
3636
);
3737
}
3838

39-
public function searchAsync(string $indexName, array $dsl): FutureArray
39+
public function searchAsync(string $indexName, array $dsl, ?string $searchType = null): FutureArray
4040
{
4141
$this->queryLog?->log($indexName, $dsl);
4242

43-
return $this->client->search($this->paramsAsync([
44-
'index' => $indexName,
45-
'body' => $dsl,
46-
]));
43+
return $this->client->search($this->paramsAsync(
44+
array_filter([
45+
'index' => $indexName,
46+
'body' => $dsl,
47+
'search_type' => $searchType,
48+
]),
49+
));
4750
}
4851

4952
public function deleteByQuery(string $indexName, array $dsl): array

src/ElasticIndex.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,4 @@ protected function indexName(): string
2626
{
2727
return $this->name;
2828
}
29-
30-
protected function searchType(): ?string
31-
{
32-
return null;
33-
}
3429
}

src/Search/SearchQuery.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SearchQuery implements SortableQuery, CollapsibleQuery
3636
protected array $fields = [];
3737
protected array $include = [];
3838
protected array $exclude = [];
39+
protected ?string $searchType = null;
3940

4041
public function __construct(protected SearchIndex $index)
4142
{
@@ -169,7 +170,7 @@ protected function execute(
169170

170171
$dsl = array_filter($dsl);
171172

172-
return $async ? $this->index->searchAsync($dsl) : $this->index->search($dsl);
173+
return $async ? $this->index->searchAsync($dsl, $this->searchType) : $this->index->search($dsl, $this->searchType);
173174
}
174175

175176
protected function sourceToDSL(bool $source): array | bool
@@ -264,6 +265,15 @@ public function skip(int $count): static
264265
return $this;
265266
}
266267

268+
public function searchType(string $searchType): static
269+
{
270+
Assert::stringNotEmpty($searchType);
271+
272+
$this->searchType = $searchType;
273+
274+
return $this;
275+
}
276+
267277
//endregion
268278

269279
protected function boolQuery(): BoolQueryBuilder

0 commit comments

Comments
 (0)