Skip to content

Commit 3bfc38d

Browse files
authored
Merge pull request #66 from ensi-platform/v7-search-type
V7 add search type and boost
2 parents 2394bff + 199ce33 commit 3bfc38d

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

src/Concerns/InteractsWithIndex.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ protected function settings(): array
2929
/**
3030
* @see SearchIndex::search()
3131
*/
32-
public function search(array $dsl): array
32+
public function search(array $dsl, ?string $searchType = null): array
3333
{
34-
return $this->resolveClient()->search($this->indexName(), $dsl);
34+
return $this->resolveClient()->search($this->indexName(), $dsl, $searchType);
3535
}
3636

3737
/**
3838
* @see SearchIndex::search()
3939
*/
40-
public function searchAsync(array $dsl): FutureArray
40+
public function searchAsync(array $dsl, ?string $searchType = null): FutureArray
4141
{
42-
return $this->resolveClient()->searchAsync($this->indexName(), $dsl);
42+
return $this->resolveClient()->searchAsync($this->indexName(), $dsl, $searchType);
4343
}
4444

4545
/**

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: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,31 @@ 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

29-
return $this->client->search([
30-
'index' => $indexName,
31-
'body' => $dsl,
32-
]);
29+
return $this->client
30+
->search(
31+
array_filter([
32+
'index' => $indexName,
33+
'body' => $dsl,
34+
'search_type' => $searchType,
35+
])
36+
);
3337
}
3438

35-
public function searchAsync(string $indexName, array $dsl): FutureArray
39+
public function searchAsync(string $indexName, array $dsl, ?string $searchType = null): FutureArray
3640
{
3741
$this->queryLog?->log($indexName, $dsl);
3842

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

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

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
@@ -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)