From 3eda298ba57b7514a6239651618a93781faab6ba Mon Sep 17 00:00:00 2001 From: Summer Date: Mon, 27 May 2024 18:39:53 +0800 Subject: [PATCH 1/2] feat: add MatchPhraseQuery --- README.md | 7 +++++ src/Queries/MatchPhraseQuery.php | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/Queries/MatchPhraseQuery.php 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/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; + } +} From b8237f8c535f3f53e7e471c041ae906ca75e629c Mon Sep 17 00:00:00 2001 From: Summer Date: Mon, 27 May 2024 19:03:32 +0800 Subject: [PATCH 2/2] chore: cs-fix --- src/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; }