Skip to content
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

search query must be escaped #3

Open
CarstenLeue opened this issue Jan 16, 2017 · 0 comments
Open

search query must be escaped #3

CarstenLeue opened this issue Jan 16, 2017 · 0 comments

Comments

@CarstenLeue
Copy link

In var searchParams = "q=*:*&fl=name,id&wt=json&fq=classification:content-type&fq=name:" + contentTypeName;

the value of the fq parameter needs to be escaped:

  1. the name of the content type represents a Solr query, so we need to apply Solr escaping
  2. the result must be escaped using encodeURIComponent since it is a query parameter

For Solr escaping the is no standard javascript method afaik, the equivalent java code is:

private static final String escapeQueryChars(final String s) {
	final StringBuilder sb = new StringBuilder();
	for (int i = 0; i < s.length(); i++) {
		final char c = s.charAt(i);
		// These characters are part of the query syntax and must be escaped
		if ((c == '\\') || (c == '+') || (c == '-') || (c == '!') || (c == '(') || (c == ')') || (c == ':')
				|| (c == '^') || (c == '[') || (c == ']') || (c == '\"') || (c == '{') || (c == '}') || (c == '~')
				|| (c == '*') || (c == '?') || (c == '|') || (c == '&') || (c == ';') || (c == '/')
				|| Character.isWhitespace(c)) {
			sb.append('\\');
		}
		sb.append(c);
	}
	return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant