Skip to content

Commit

Permalink
Merge pull request #795 from topikito/escape-new-symbols
Browse files Browse the repository at this point in the history
[RFR] Escape new symbols in Util::escapeTerm (fixes #794)
  • Loading branch information
ruflin committed Mar 5, 2015
2 parents 637b706 + ab75e5b commit 36165fb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/Elastica/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Nicolas Ruflin <[email protected]>
* @author Thibault Duplessis <[email protected]>
* @author Oleg Zinchenko <[email protected]>
* @author Roberto Nygaard <[email protected]>
*/
class Util
{
Expand All @@ -35,9 +36,10 @@ public static function replaceBooleanWordsAndEscapeTerm($term)

/**
* Escapes the following terms (because part of the query language)
* + - && || ! ( ) { } [ ] ^ " ~ * ? : \
* + - && || ! ( ) { } [ ] ^ " ~ * ? : \ < >
*
* @link http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Escaping%20Special%20Characters
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
*
* @param string $term Query term to escape
* @return string Escaped query term
Expand All @@ -46,7 +48,7 @@ public static function escapeTerm($term)
{
$result = $term;
// \ escaping has to be first, otherwise escaped later once again
$chars = array('\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/');
$chars = array('\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/', '<', '>');

foreach ($chars as $char) {
$result = str_replace($char, '\\'.$char, $result);
Expand Down
4 changes: 2 additions & 2 deletions test/lib/Elastica/Test/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function getReplaceBooleanWordsPairs()

public function testEscapeTermSpecialCharacters()
{
$before = '\\+-&&||!(){}[]^"~*?:/';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/';
$before = '\\+-&&||!(){}[]^"~*?:/<>';
$after = '\\\\\\+\\-\\&&\\||\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:\\/\<\>';

$this->assertEquals(Util::escapeTerm($before), $after);
}
Expand Down

0 comments on commit 36165fb

Please sign in to comment.