glance-project / search-service
Search Service
Package info
gitlab.cern.ch/fence/common/search-service.git
pkg:composer/glance-project/search-service
Requires
- cache/filesystem-adapter: ^3.0
- doctrine/dbal: ^4.2
- php-di/php-di: ^7.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-develop
- v2.4.0
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- dev-story/ATGLANCE-8495
- dev-master
- dev-bug/ATGLANCE-8334
- dev-local-fylesystem-adapter-typo-hotfix
- dev-hotfix/2.0.7
- dev-atlas-version
- dev-master-lhcb
- dev-feature/ddd
This package is auto-updated.
Last update: 2026-11-26 12:33:05 UTC
README
Search Service created to translate query strings generated from a boolean search into SQL filters
For the full documentation please refer to these links: Entity search guide, Search service guide.
Getting started
Before we start, make sure you have run the migrations on your database. They can be found on the `/example/migrationsfolder. Don't forget to change{{ HISTORY_SCHEMA_NAME }}to your history schema name and{{ SCHEMA_NAME }}to your main schema name. On the example we are also inserting a search type calledsurveys`. This goes in line with the example SQL queries we are using.
Installing
To install the search service as one of your application's dependencies you can run the following command:
composer require glance-project/search-service
Setting up
You need to setup your SQL query and the .json configuration file for your search. The configuration file will be used to define the properties, cache location, lookup table and the location of your SQL query, which will be used to retrieve the data from the database. An example can be found on the `/example/resources/survey-search.sqlfolder for the query and on the/example/resources/surveys.json` folder for the configuration file.
When we define the properties on the configuration file, we include, alongside with the column name from the database, their type (e.g. "date") and the compatible operators (e.g. "contain", "not-contain", "=", "!=").
You also need to create the controller and setup the routes for the search service. An example can be found on the `/example/Search/SearchController.phpand/example/Search/SearchRoutes.php` files. After they are set up, an user will be able to save, delete and find their saved searches (that's the reason for the database migrations).
With all this set up you are able to use the search service. For the client, please visit this link.
Using the search service
On the `/example/Survey/Web/SurveyController.php` file you can find an example of how a controller communicates with the search service. The controller passes the request's query params ($request->getQueryParams()) to SearchInputDTO. PHP has already URL-decoded them at that point, so queryString is used as received and is not decoded again.
The queryString parameter must therefore be URL-encoded exactly once, as any other query parameter (e.g. passing it in axios params, or with encodeURIComponent). Do not encode it yourself before handing it to an HTTP client that encodes params again, otherwise the service receives the encoded text instead of the query.
For the query ("description" = "test"), an example GET request would look like this:
https://localhost:8080/rp-survey/api/surveys/search?offset=0&limit=50&queryString=(%22description%22%20%3D%20%22test%22)
Would result in a response like this:
{
"results": [
{
"id": "1389",
"description": "test",
"comments": null,
"magnetOn": "N",
"magnetOnAsString": "No",
"startedOn": "04-AUG-22 01.52.00.000000 PM",
"startedOnAsString": "2022-08-04 13:52",
"beamOffAt": "04-AUG-22 01.52.00.000000 PM",
"beamOffAtAsString": "2022-08-04 13:52",
"ramsesLogLink": null,
"responsiblePersonsNames": "Carlos Brito"
}
],
"numberOfResults": "1"
}
Rewriting or rejecting filter values
runSearch takes an optional third argument, a callable the Filter class applies to every statement's value
before compiling it into SQL. Without it a value reaches the SQL as the caller wrote it, which is right as long
as callers write values the way the query selects them. When they do not, the callable either converts the
value or refuses it.
Converting, for a column that holds 2018-11-28 while callers write 28/11/2018:
use Glance\Search\Domain\Value;
$results = $this->searchProvider->runSearch(
$searchParameters,
$searchConfigurationPath,
function (string $field, string $operator, string $value) use ($dateFields): string {
// Fields this application does not treat as dates are left alone
if (!in_array($field, $dateFields, true) || $value === Value::EMPTY) {
return $value;
}
$date = DateTimeImmutable::createFromFormat("!d/m/Y", $value);
if ($date === false) {
throw new InvalidArgumentException("'$value' is not a date written as DD/MM/YYYY");
}
// The query selects this column as TO_CHAR(..., 'yyyy-mm-dd')
return $date->format("Y-m-d");
}
);
Refusing instead, where callers already write dates the way the query selects them, so that a value which cannot match is reported as an error rather than read back as an empty result list:
// same guard as above
$date = DateTimeImmutable::createFromFormat("!Y-m-d", $value);
if ($date === false || $date->format("Y-m-d") !== $value) {
throw new InvalidArgumentException("'$value' is not a valid date for '$field'");
}
return $value;
The re-formatting check is what makes this strict: on its own createFromFormat accepts 2018-1-28 and rolls
an impossible 2018-02-30 over into 2018-03-02, so comparing the parsed date back against the input is what
rejects a filter that could only ever match the wrong day. Converting accepts more shapes than the results
report; refusing keeps the shape read from a result and the shape allowed in a filter identical.
The callable receives:
$field, the property name as the configuration file spells it, so it can be looked up in the samepropertiesobject that defines the field's column and operators.$operator, which matters becausecontainandnot-containcompile toLIKE '%value%'and match part of the stored text, so a value of reduced precision is meaningful for them and not for=.$value, as it was written, with the spaces of a quoted value decoded. The__EMPTY__token used for null comparisons is passed through untouched, so a callable that would otherwise reject it must return it as is.
Its return value is used as given, with no further encoding, and throwing rejects the filter: the exception
propagates out of runSearch for the caller to handle. It runs once per statement, only when a queryString
is given; omitting the argument keeps values reaching the SQL exactly as received.
More info
This project was the subject of a presentation at the 26th International Conference On Computing In High Energy & Nuclear Physics (CHEP 2023). You can find the event here.