code: public Query parse(String textsearch) throws ParseException
     {
        // replace escaped ' with just '
        StringBuilder rewritten = new StringBuilder();
        // the default lucene query parser recognizes 'AND' and 'NOT' as
        // keywords.
        textsearch = textsearch.replaceAll("AND", "and");
        textsearch = textsearch.replaceAll("NOT", "not");
        boolean escaped = false;
        for (int i = 0; i < textsearch.length(); i++)
        {
           if (textsearch.charAt(i) == '\\')
           {
              if (escaped)
              {
                 rewritten.append("\\\\");
                 escaped = false;
              }
              else
              {
                 escaped = true;
              }
           }
           else if (textsearch.charAt(i) == '\'')
           {
              if (escaped)
              {
                 escaped = false;
              }
              rewritten.append(textsearch.charAt(i));
           }
           else if (textsearch.charAt(i) == '~')
           {
              if (i == 0 || Character.isWhitespace(textsearch.charAt(i - 1)))
              {
                 // escape tilde so we can use it for similarity query
                 rewritten.append("\\");
              }
              rewritten.append('~');
           }
           else
           {
              if (escaped)
              {
                 rewritten.append('\\');
                 escaped = false;
              }
              rewritten.append(textsearch.charAt(i));
           }
        }
        return super.parse(rewritten.toString());
     }
docs: {@inheritDoc}