sspat / es-query-sanitizer
Simple helper class to sanitize ElasticSearch reserved characters from query strings
Installs: 112 042
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ~5.7
This package is auto-updated.
Last update: 2024-11-12 05:30:21 UTC
README
Inspired by node-elasticsearch-sanitize
Features
Accepts an arbitrary string as input and escapes the ElasticSearch reserved characters:
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / AND OR NOT space
Returns a sanitized string which can be safely used in an ElasticSearch query_string query.
Installation
The preferred way to install this extension is through composer.
Either run
composer require sspat/es-query-sanitizer
or add
"sspat/es-query-sanitizer": "~1.0"
to the require section of your composer.json
file.
Usage
To use pass in a string:
$unescapedQueryString = 'AND there! are? (lots of) char*cters 2 ^escape!' $escapedQueryString = \sspat\ESQuerySanitizer\Sanitizer::escape($unescapedQueryString); echo $escapedQueryString; // \A\N\D\ there\!\ are\?\ \(lots\ of\)\ char\*cters\ 2\ \^escape\!
You can also pass an array as the second argument, if you want to prevent some characters from being escaped:
$unescapedQueryString = 'AND there! are? (lots of) char*cters 2 ^escape!' $excludeCharacters = ['!', '^']; $escapedQueryString = \sspat\ESQuerySanitizer\Sanitizer::escape($unescapedQueryString, $excludeCharacters); echo $escapedQueryString; // \A\N\D\ there!\ are\?\ \(lots\ of\)\ char\*cters\ 2\ ^escape!