Skip to content

Commit

Permalink
Add search type and preference support to SearchRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Feb 20, 2022
1 parent e4ec1f0 commit 16179d0
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 109 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@ $request->indicesBoost([
['my-index' => 1.3],
]);

// define the search type
$request->searchType('query_then_fetch');

// set the preference
$request->preference('_local');

// use pagination
$request->from(0)->size(20);

Expand Down
7 changes: 1 addition & 6 deletions src/Documents/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,8 @@ public function deleteByQuery(string $indexName, array $query, bool $refresh = f

public function search(string $indexName, SearchRequest $request): SearchResponse
{
$params = [
'index' => $indexName,
'body' => $request->toArray(),
];

$params = array_merge($request->toArray(), ['index' => $indexName]);
$response = $this->client->search($params);

return new SearchResponse($response);
}
}
44 changes: 28 additions & 16 deletions src/Search/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@ final class SearchRequest implements Arrayable
public function __construct(?array $query = null)
{
if (isset($query)) {
$this->request['query'] = $query;
$this->request['body']['query'] = $query;
}
}

public function highlight(array $highlight): self
{
$this->request['highlight'] = $highlight;
$this->request['body']['highlight'] = $highlight;
return $this;
}

public function sort(array $sort): self
{
$this->request['sort'] = $sort;
$this->request['body']['sort'] = $sort;
return $this;
}

public function rescore(array $rescore): self
{
$this->request['rescore'] = $rescore;
$this->request['body']['rescore'] = $rescore;
return $this;
}

public function from(int $from): self
{
$this->request['from'] = $from;
$this->request['body']['from'] = $from;
return $this;
}

public function size(int $size): self
{
$this->request['size'] = $size;
$this->request['body']['size'] = $size;
return $this;
}

public function suggest(array $suggest): self
{
$this->request['suggest'] = $suggest;
$this->request['body']['suggest'] = $suggest;
return $this;
}

Expand All @@ -59,25 +59,25 @@ public function suggest(array $suggest): self
*/
public function source($source): self
{
$this->request['_source'] = $source;
$this->request['body']['_source'] = $source;
return $this;
}

public function collapse(array $collapse): self
{
$this->request['collapse'] = $collapse;
$this->request['body']['collapse'] = $collapse;
return $this;
}

public function aggregations(array $aggregations): self
{
$this->request['aggregations'] = $aggregations;
$this->request['body']['aggregations'] = $aggregations;
return $this;
}

public function postFilter(array $postFilter): self
{
$this->request['post_filter'] = $postFilter;
$this->request['body']['post_filter'] = $postFilter;
return $this;
}

Expand All @@ -86,31 +86,43 @@ public function postFilter(array $postFilter): self
*/
public function trackTotalHits($trackTotalHits): self
{
$this->request['track_total_hits'] = $trackTotalHits;
$this->request['body']['track_total_hits'] = $trackTotalHits;
return $this;
}

public function indicesBoost(array $indicesBoost): self
{
$this->request['indices_boost'] = $indicesBoost;
$this->request['body']['indices_boost'] = $indicesBoost;
return $this;
}

public function trackScores(bool $trackScores): self
{
$this->request['track_scores'] = $trackScores;
$this->request['body']['track_scores'] = $trackScores;
return $this;
}

public function minScore(float $minScore): self
{
$this->request['min_score'] = $minScore;
$this->request['body']['min_score'] = $minScore;
return $this;
}

public function scriptFields(array $scriptFields): self
{
$this->request['script_fields'] = $scriptFields;
$this->request['body']['script_fields'] = $scriptFields;
return $this;
}

public function searchType(string $searchType): self
{
$this->request['search_type'] = $searchType;
return $this;
}

public function preference(string $preference): self
{
$this->request['preference'] = $preference;
return $this;
}

Expand Down
Loading

0 comments on commit 16179d0

Please sign in to comment.