Skip to content

Commit

Permalink
Add function to append ".keyword" to field name
Browse files Browse the repository at this point in the history
Added a static method 'asKeyword' in Query.php. This appends a ".keyword" suffix to the field name, aiding in sorting and aggregating on keyword fields.
It can be used like `$field = Query::asKeyword($field);`.
This makes some querying code easier to read.
  • Loading branch information
Radiergummi committed Aug 30, 2023
1 parent 839c5aa commit db7fabd
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit db7fabd

Please sign in to comment.