Skip to content

Commit 4f6bf49

Browse files
authored
Merge pull request #65 from ensi-platform/v8-search-type
V8 add search type and boost
2 parents f3c249a + c5808fb commit 4f6bf49

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

src/Concerns/InteractsWithIndex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ protected function settings(): array
2828
/**
2929
* @see SearchIndex::search()
3030
*/
31-
public function search(array $dsl): array
31+
public function search(array $dsl, ?string $searchType = null): array
3232
{
33-
return $this->resolveClient()->search($this->indexName(), $dsl);
33+
return $this->resolveClient()->search($this->indexName(), $dsl, $searchType);
3434
}
3535

3636
/**

src/Contracts/MultiMatchOptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public static function make(
1414
?string $type = null,
1515
?string $operator = null,
1616
?string $fuzziness = null,
17-
?string $minimumShouldMatch = null
17+
?string $minimumShouldMatch = null,
18+
?float $boost = null,
1819
): static {
1920
Assert::nullOrOneOf($type, MatchType::cases());
2021
Assert::nullOrOneOf($operator, ['or', 'and']);
@@ -24,6 +25,7 @@ public static function make(
2425
'operator' => $operator,
2526
'fuzziness' => $fuzziness,
2627
'minimum_should_match' => $minimumShouldMatch,
28+
'boost' => $boost,
2729
]));
2830
}
2931

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ public function getClient(): Client
2222
return $this->client;
2323
}
2424

25-
public function search(string $indexName, array $dsl): array
25+
public function search(string $indexName, array $dsl, ?string $searchType = null): array
2626
{
2727
$this->queryLog?->log($indexName, $dsl);
2828

2929
return $this->client
30-
->search(['index' => $indexName, 'body' => $dsl])
30+
->search(
31+
array_filter([
32+
'index' => $indexName,
33+
'body' => $dsl,
34+
'search_type' => $searchType,
35+
])
36+
)
3137
->asArray();
3238
}
3339

src/Search/Enums/SearchType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Search\Enums;
4+
5+
class SearchType
6+
{
7+
public const QUERY_THEN_FETCH = 'query_then_fetch';
8+
public const DFS_QUERY_THEN_FETCH = 'dfs_query_then_fetch';
9+
10+
public static function cases(): array
11+
{
12+
return [
13+
self::QUERY_THEN_FETCH,
14+
self::DFS_QUERY_THEN_FETCH,
15+
];
16+
}
17+
}

src/Search/SearchQuery.php

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

3940
public function __construct(protected SearchIndex $index)
4041
{
@@ -150,7 +151,7 @@ protected function execute(
150151
$dsl['search_after'] = $cursor->toDSL();
151152
}
152153

153-
return $this->index->search(array_filter($dsl));
154+
return $this->index->search(array_filter($dsl), $this->searchType);
154155
}
155156

156157
protected function sourceToDSL(bool $source): array | bool
@@ -245,6 +246,15 @@ public function skip(int $count): static
245246
return $this;
246247
}
247248

249+
public function searchType(string $searchType): static
250+
{
251+
Assert::stringNotEmpty($searchType);
252+
253+
$this->searchType = $searchType;
254+
255+
return $this;
256+
}
257+
248258
//endregion
249259

250260
protected function boolQuery(): BoolQueryBuilder

0 commit comments

Comments
 (0)