Skip to content

Commit

Permalink
Merge pull request #39 from MilanLamote/main
Browse files Browse the repository at this point in the history
Add highlighting
  • Loading branch information
freekmurze authored Apr 17, 2024
2 parents 4a239a0 + 3ce4ac7 commit 77d6b15
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ The `fields()` method can be used to request specific fields from the resulting
$builder->fields('user.id', 'http.*.status');
```

## Highlighting

The `highlight()` method can be used to add a highlight section to your query along the rules in [the ElasticSearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html).

```php
$highlightSettings = [
'pre_tags' => ['<em>'],
'post_tags' => ['</em>'],
'fields' => [
'*' => (object) []
]
];

$builder->highlight($highlightSettings);
```

## Pagination

Finally the `Builder` also features a `size()` and `from()` method for the corresponding ElasticSearch search parameters. These can be used to build a paginated search. Take a look the following example to get a rough idea:
Expand Down
13 changes: 13 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Builder

protected bool $trackTotalHits = false;

protected ?array $highlight = null;

public function __construct(protected Client $client)
{
}
Expand Down Expand Up @@ -145,6 +147,13 @@ public function withoutAggregations(): static
return $this;
}

public function highlight(array $highlight): static
{
$this->highlight = $highlight;

return $this;
}

public function getPayload(): array
{
$payload = [];
Expand All @@ -169,6 +178,10 @@ public function getPayload(): array
$payload['search_after'] = $this->searchAfter;
}

if ($this->highlight) {
$payload['highlight'] = $this->highlight;
}

return $payload;
}
}

0 comments on commit 77d6b15

Please sign in to comment.