diff --git a/README.md b/README.md index d0901ab..b579bfe 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/Documents/DocumentManager.php b/src/Documents/DocumentManager.php index f2c1f89..b9ef5eb 100644 --- a/src/Documents/DocumentManager.php +++ b/src/Documents/DocumentManager.php @@ -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); } } diff --git a/src/Search/SearchRequest.php b/src/Search/SearchRequest.php index 0beb07b..c0945bc 100644 --- a/src/Search/SearchRequest.php +++ b/src/Search/SearchRequest.php @@ -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; } @@ -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; } @@ -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; } diff --git a/tests/Unit/Search/SearchRequestTest.php b/tests/Unit/Search/SearchRequestTest.php index aeb8fb2..b52c5b8 100644 --- a/tests/Unit/Search/SearchRequestTest.php +++ b/tests/Unit/Search/SearchRequestTest.php @@ -1,4 +1,6 @@ -assertSame([ - 'query' => [ - 'term' => [ - 'user' => 'foo', + 'body' => [ + 'query' => [ + 'term' => [ + 'user' => 'foo', + ], ], ], ], $request->toArray()); @@ -43,14 +47,16 @@ public function test_array_casting_with_query_and_highlight(): void ]); $this->assertEquals([ - 'query' => [ - 'match' => [ - 'content' => 'foo', + 'body' => [ + 'query' => [ + 'match' => [ + 'content' => 'foo', + ], ], - ], - 'highlight' => [ - 'fields' => [ - 'content' => new stdClass(), + 'highlight' => [ + 'fields' => [ + 'content' => new stdClass(), + ], ], ], ], $request->toArray()); @@ -68,12 +74,14 @@ public function test_array_casting_with_query_and_sort(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'sort' => [ - ['title' => 'asc'], - '_score', + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'sort' => [ + ['title' => 'asc'], + '_score', + ], ], ], $request->toArray()); } @@ -101,22 +109,24 @@ public function test_array_casting_with_query_and_rescore(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'rescore' => [ - 'window_size' => 50, + 'body' => [ 'query' => [ - 'rescore_query' => [ - 'match_phrase' => [ - 'message' => [ - 'query' => 'the quick brown', - 'slop' => 2, + 'match_all' => new stdClass(), + ], + 'rescore' => [ + 'window_size' => 50, + 'query' => [ + 'rescore_query' => [ + 'match_phrase' => [ + 'message' => [ + 'query' => 'the quick brown', + 'slop' => 2, + ], ], ], + 'query_weight' => 0.7, + 'rescore_query_weight' => 1.2, ], - 'query_weight' => 0.7, - 'rescore_query_weight' => 1.2, ], ], ], $request->toArray()); @@ -131,10 +141,12 @@ public function test_array_casting_with_query_and_from(): void $request->from(10); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'from' => 10, ], - 'from' => 10, ], $request->toArray()); } @@ -147,10 +159,12 @@ public function test_array_casting_with_query_and_size(): void $request->size(100); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'size' => 100, ], - 'size' => 100, ], $request->toArray()); } @@ -170,14 +184,16 @@ public function test_array_casting_with_query_and_suggest(): void ]); $this->assertEquals([ - 'query' => [ - 'match_none' => new stdClass(), - ], - 'suggest' => [ - 'color_suggestion' => [ - 'text' => 'red', - 'term' => [ - 'field' => 'color', + 'body' => [ + 'query' => [ + 'match_none' => new stdClass(), + ], + 'suggest' => [ + 'color_suggestion' => [ + 'text' => 'red', + 'term' => [ + 'field' => 'color', + ], ], ], ], @@ -208,10 +224,12 @@ public function test_array_casting_with_source($source): void $request->source($source); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + '_source' => $source, ], - '_source' => $source, ], $request->toArray()); } @@ -226,11 +244,13 @@ public function test_array_casting_with_collapse(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'collapse' => [ - 'field' => 'user', + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'collapse' => [ + 'field' => 'user', + ], ], ], $request->toArray()); } @@ -248,10 +268,12 @@ public function test_array_casting_with_aggregations(): void ]); $this->assertEquals([ - 'aggregations' => [ - 'min_price' => [ - 'min' => [ - 'field' => 'price', + 'body' => [ + 'aggregations' => [ + 'min_price' => [ + 'min' => [ + 'field' => 'price', + ], ], ], ], @@ -271,12 +293,14 @@ public function test_array_casting_with_post_filter(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'post_filter' => [ - 'term' => [ - 'color' => 'red', + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'post_filter' => [ + 'term' => [ + 'color' => 'red', + ], ], ], ], $request->toArray()); @@ -291,10 +315,12 @@ public function test_array_casting_with_track_total_hits(): void $request->trackTotalHits(100); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'track_total_hits' => 100, ], - 'track_total_hits' => 100, ], $request->toArray()); } @@ -310,12 +336,14 @@ public function test_array_casting_with_indices_boost(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'indices_boost' => [ - ['my-alias' => 1.4], - ['my-index' => 1.3], + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'indices_boost' => [ + ['my-alias' => 1.4], + ['my-index' => 1.3], + ], ], ], $request->toArray()); } @@ -329,10 +357,12 @@ public function test_array_casting_with_track_scores(): void $request->trackScores(true); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'track_scores' => true, ], - 'track_scores' => true, ], $request->toArray()); } @@ -356,17 +386,19 @@ public function test_array_casting_with_script_fields(): void ]); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), - ], - 'script_fields' => [ - 'my_doubled_field' => [ - 'script' => [ - 'lang' => 'painless', - 'source' => 'doc[params.field] * params.multiplier', - 'params' => [ - 'field' => 'my_field', - 'multiplier' => 2, + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'script_fields' => [ + 'my_doubled_field' => [ + 'script' => [ + 'lang' => 'painless', + 'source' => 'doc[params.field] * params.multiplier', + 'params' => [ + 'field' => 'my_field', + 'multiplier' => 2, + ], ], ], ], @@ -383,10 +415,48 @@ public function test_array_casting_with_min_score(): void $request->minScore(0.5); $this->assertEquals([ - 'query' => [ - 'match_all' => new stdClass(), + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + 'min_score' => 0.5, + ], + ], $request->toArray()); + } + + public function test_array_casting_with_search_type(): void + { + $request = new SearchRequest([ + 'match_all' => new stdClass(), + ]); + + $request->searchType('query_then_fetch'); + + $this->assertEquals([ + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], + ], + 'search_type' => 'query_then_fetch', + ], $request->toArray()); + } + + public function test_array_casting_with_preference(): void + { + $request = new SearchRequest([ + 'match_all' => new stdClass(), + ]); + + $request->preference('_local'); + + $this->assertEquals([ + 'body' => [ + 'query' => [ + 'match_all' => new stdClass(), + ], ], - 'min_score' => 0.5, + 'preference' => '_local', ], $request->toArray()); } }