Skip to content

Commit

Permalink
Merge pull request #50 from summerKK/match-phrase-feat-20240527
Browse files Browse the repository at this point in the history
feat: add MatchPhraseQuery
  • Loading branch information
AlexVanderbist authored Oct 3, 2024
2 parents d7b27fc + b8237f8 commit fbe30a8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function search(): Elasticsearch|Promise
$params['from'] = $this->from;
}

if($this->trackTotalHits) {
if ($this->trackTotalHits) {
$params['track_total_hits'] = true;
}

Expand Down
50 changes: 50 additions & 0 deletions src/Queries/MatchPhraseQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Spatie\ElasticsearchQueryBuilder\Queries;

class MatchPhraseQuery implements Query
{
public static function create(
string $field,
string $value,
int|null $slop = null,
string|null $zeroTermsQuery = null,
string|null $analyzer = null
): self {
return new self($field, $value, $slop, $zeroTermsQuery, $analyzer);
}

public function __construct(
protected string $field,
protected string $value,
protected int | null $slop = null,
protected string | null $zeroTermsQuery = null,
protected string | null $analyzer = null
) {
}

public function toArray(): array
{
$match = [
'match_phrase' => [
$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;
}
}

0 comments on commit fbe30a8

Please sign in to comment.