Skip to content

Commit 16179d0

Browse files
committed
Add search type and preference support to SearchRequest
1 parent e4ec1f0 commit 16179d0

File tree

4 files changed

+192
-109
lines changed

4 files changed

+192
-109
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ $request->indicesBoost([
418418
['my-index' => 1.3],
419419
]);
420420

421+
// define the search type
422+
$request->searchType('query_then_fetch');
423+
424+
// set the preference
425+
$request->preference('_local');
426+
421427
// use pagination
422428
$request->from(0)->size(20);
423429

src/Documents/DocumentManager.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,8 @@ public function deleteByQuery(string $indexName, array $query, bool $refresh = f
104104

105105
public function search(string $indexName, SearchRequest $request): SearchResponse
106106
{
107-
$params = [
108-
'index' => $indexName,
109-
'body' => $request->toArray(),
110-
];
111-
107+
$params = array_merge($request->toArray(), ['index' => $indexName]);
112108
$response = $this->client->search($params);
113-
114109
return new SearchResponse($response);
115110
}
116111
}

src/Search/SearchRequest.php

+28-16
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,43 @@ final class SearchRequest implements Arrayable
1414
public function __construct(?array $query = null)
1515
{
1616
if (isset($query)) {
17-
$this->request['query'] = $query;
17+
$this->request['body']['query'] = $query;
1818
}
1919
}
2020

2121
public function highlight(array $highlight): self
2222
{
23-
$this->request['highlight'] = $highlight;
23+
$this->request['body']['highlight'] = $highlight;
2424
return $this;
2525
}
2626

2727
public function sort(array $sort): self
2828
{
29-
$this->request['sort'] = $sort;
29+
$this->request['body']['sort'] = $sort;
3030
return $this;
3131
}
3232

3333
public function rescore(array $rescore): self
3434
{
35-
$this->request['rescore'] = $rescore;
35+
$this->request['body']['rescore'] = $rescore;
3636
return $this;
3737
}
3838

3939
public function from(int $from): self
4040
{
41-
$this->request['from'] = $from;
41+
$this->request['body']['from'] = $from;
4242
return $this;
4343
}
4444

4545
public function size(int $size): self
4646
{
47-
$this->request['size'] = $size;
47+
$this->request['body']['size'] = $size;
4848
return $this;
4949
}
5050

5151
public function suggest(array $suggest): self
5252
{
53-
$this->request['suggest'] = $suggest;
53+
$this->request['body']['suggest'] = $suggest;
5454
return $this;
5555
}
5656

@@ -59,25 +59,25 @@ public function suggest(array $suggest): self
5959
*/
6060
public function source($source): self
6161
{
62-
$this->request['_source'] = $source;
62+
$this->request['body']['_source'] = $source;
6363
return $this;
6464
}
6565

6666
public function collapse(array $collapse): self
6767
{
68-
$this->request['collapse'] = $collapse;
68+
$this->request['body']['collapse'] = $collapse;
6969
return $this;
7070
}
7171

7272
public function aggregations(array $aggregations): self
7373
{
74-
$this->request['aggregations'] = $aggregations;
74+
$this->request['body']['aggregations'] = $aggregations;
7575
return $this;
7676
}
7777

7878
public function postFilter(array $postFilter): self
7979
{
80-
$this->request['post_filter'] = $postFilter;
80+
$this->request['body']['post_filter'] = $postFilter;
8181
return $this;
8282
}
8383

@@ -86,31 +86,43 @@ public function postFilter(array $postFilter): self
8686
*/
8787
public function trackTotalHits($trackTotalHits): self
8888
{
89-
$this->request['track_total_hits'] = $trackTotalHits;
89+
$this->request['body']['track_total_hits'] = $trackTotalHits;
9090
return $this;
9191
}
9292

9393
public function indicesBoost(array $indicesBoost): self
9494
{
95-
$this->request['indices_boost'] = $indicesBoost;
95+
$this->request['body']['indices_boost'] = $indicesBoost;
9696
return $this;
9797
}
9898

9999
public function trackScores(bool $trackScores): self
100100
{
101-
$this->request['track_scores'] = $trackScores;
101+
$this->request['body']['track_scores'] = $trackScores;
102102
return $this;
103103
}
104104

105105
public function minScore(float $minScore): self
106106
{
107-
$this->request['min_score'] = $minScore;
107+
$this->request['body']['min_score'] = $minScore;
108108
return $this;
109109
}
110110

111111
public function scriptFields(array $scriptFields): self
112112
{
113-
$this->request['script_fields'] = $scriptFields;
113+
$this->request['body']['script_fields'] = $scriptFields;
114+
return $this;
115+
}
116+
117+
public function searchType(string $searchType): self
118+
{
119+
$this->request['search_type'] = $searchType;
120+
return $this;
121+
}
122+
123+
public function preference(string $preference): self
124+
{
125+
$this->request['preference'] = $preference;
114126
return $this;
115127
}
116128

0 commit comments

Comments
 (0)