diff --git a/src/Query.php b/src/Query.php index d17a5cd..2474830 100755 --- a/src/Query.php +++ b/src/Query.php @@ -22,6 +22,7 @@ use Matchory\Elasticsearch\Interfaces\ConnectionInterface; use function count; use function json_encode; +use function rtrim; use const JSON_THROW_ON_ERROR; /** @@ -203,6 +204,18 @@ public function __construct( $this->model = $model ?? new Model(); } + /** + * Adds a ".keyword" suffix to the given field name. This is useful for + * sorting and aggregating on keyword fields. + * + * @param string $field + * @return string + */ + public static function asKeyword(string $field): string + { + return rtrim($field, '.') . '.keyword'; + } + /** * Retrieves the underlying Elasticsearch connection. *