diff --git a/README.md b/README.md index 9f6a2be..a99be15 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ The following query types are available: \Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery::create('name', 'john doe', fuzziness: 2, boost: 5.0); ``` +#### `MatchPhraseQuery` +[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html) + +```php +\Spatie\ElasticsearchQueryBuilder\Queries\MatchPhraseQuery::create('name', 'john doe', slop: 2,zeroTermsQuery: "none",analyzer: "my_analyzer"); +``` + #### `MultiMatchQuery` [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html) diff --git a/src/Builder.php b/src/Builder.php index 31f6839..9c74956 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -93,7 +93,7 @@ public function search(): Elasticsearch|Promise $params['from'] = $this->from; } - if($this->trackTotalHits) { + if ($this->trackTotalHits) { $params['track_total_hits'] = true; } diff --git a/src/Queries/MatchPhraseQuery.php b/src/Queries/MatchPhraseQuery.php new file mode 100644 index 0000000..cf7ecfd --- /dev/null +++ b/src/Queries/MatchPhraseQuery.php @@ -0,0 +1,50 @@ + [ + $this->field => [ + 'query' => $this->value, + ], + ], + ]; + + if ($this->slop) { + $match['match_phrase'][$this->field]['slop'] = $this->slop; + } + + if ($this->zeroTermsQuery) { + $match['match_phrase'][$this->field]['zero_terms_query'] = $this->zeroTermsQuery; + } + + if ($this->analyzer) { + $match['match_phrase'][$this->field]['analyzer'] = $this->analyzer; + } + + return $match; + } +}