New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "<" and ">" as they cannot be escaped #1415

Merged
merged 4 commits into from Nov 30, 2017
Jump to file or symbol
Failed to load files and symbols.
+16 −7
Diff settings

Always

Just for now

View
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file based on the
### Backward Compatibility Breaks
### Bugfixes
- Characters "<" and ">" will be removed when a query term is passed to [`Util::escapeTerm`](https://github.com/ruflin/Elastica/pull/1415/files). Since v5.1 the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters) states that these symbols cannot be escaped ever.
### Added
View
@@ -72,12 +72,12 @@ public static function escapeDateMath($requestUri)
}
/**
* Replace the following reserved words: AND OR NOT
* Replace known reserved words (e.g. AND OR NOT)
* and
* escapes the following terms: + - && || ! ( ) { } [ ] ^ " ~ * ? : \.
* escape known special characters (e.g. + - && || ! ( ) { } [ ] ^ " ~ * ? : etc.)
*
* @link http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Boolean%20operators
* @link http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Escaping%20Special%20Characters
* @link https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_boolean_operators
* @link https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters
*
* @param string $term Query term to replace and escape
*
@@ -106,11 +106,19 @@ public static function escapeTerm($term)
{
$result = $term;
// \ escaping has to be first, otherwise escaped later once again
$chars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/', '<', '>'];
$escapableChars = ['\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/'];
foreach ($chars as $char) {
foreach ($escapableChars as $char) {
$result = str_replace($char, '\\'.$char, $result);
}
// < and > cannot be escaped, so they should be removed
// @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
$nonEscapableChars = ['<', '>'];
foreach ($nonEscapableChars as $char) {

This comment has been minimized.

@ruflin

ruflin Nov 26, 2017

Owner

I wonder if we should update line

* escapes the following terms: + - && || ! ( ) { } [ ] ^ " ~ * ? : \.
in some way? Interestingly it already doesn't list < and >.

This comment has been minimized.

@martin-georgiev

martin-georgiev Nov 27, 2017

Contributor

I would drop the list of characters in the description of replaceBooleanWordsAndEscapeTerm. My reasoning is that it exposes the internal behaviour of the used submethods. The character list creates maintenance backlog as it is not something that the wrapper method replaceBooleanWordsAndEscapeTerm has control over at present.
Yet if you believe it adds value then it needs updating too. Are you looking at any particular wording?

This comment has been minimized.

@ruflin

ruflin Nov 27, 2017

Owner

+1 on removing it. I'm good with most wordings :-)

This comment has been minimized.

@martin-georgiev

martin-georgiev Nov 29, 2017

Contributor

The method description is updated. I believe we can move this PR forward now.

$result = str_replace($char, '', $result);
}
return $result;
}
@@ -109,7 +109,7 @@ public function getReplaceBooleanWordsPairs()
public function testEscapeTermSpecialCharacters()
{
$before = '\\+-&&||!(){}[]^"~*?:/<>';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/\<\>';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/';
$this->assertEquals(Util::escapeTerm($before), $after);
}
ProTip! Use n and p to navigate between commits in a pull request.