Discussion:
Solr Json API How to escape space in search string
Iana Bondarska
2016-03-07 22:49:34 UTC
Permalink
Hi All,
could you please tell me if escaping special characters in search keywords
works in json api.
e.g. I have document
{
"string_s":"new value"
}
And I want to query "string_s" field with keyword "new value".
In path params api I can escape spaces in keyword as well as other special
characters with \ .
following query finds document:
http://localhsot:8983/solr/dynamic_fields_qa/select?q=string_s:new\
value&wt=json&indent=true
But if I try to run same search using json api, nothing is found:

http://localhsot:8983/solr/dynamic_fields_qa/select?q=*:*&json=
{"query":"string_s:new\ value"}

Best Regards,
Iana Bondarska
Yonik Seeley
2016-03-07 22:59:41 UTC
Permalink
Post by Iana Bondarska
Hi All,
could you please tell me if escaping special characters in search keywords
works in json api.
e.g. I have document
{
"string_s":"new value"
}
And I want to query "string_s" field with keyword "new value".
In path params api I can escape spaces in keyword as well as other special
characters with \ .
http://localhsot:8983/solr/dynamic_fields_qa/select?q=string_s:new\
value&wt=json&indent=true
http://localhsot:8983/solr/dynamic_fields_qa/select?q=*:*&json=
{"query":"string_s:new\ value"}
So the issue here is probably double-decoding... the JSON parser will
see the backslash-space and replace it with space only, and then the
lucene query parser will not see the backslash at all.

Either
1) use a double backslash
2) enclose the string in quotes (which will need to be backslash
escaped at the JSON level, or you can use the json single-quote
support.)

-Yonik
Jack Krupansky
2016-03-07 23:03:14 UTC
Permalink
Backslash in JSON just tells JSON to escape the next character, while what
you really want is to pass a backslash through to the Solr query parser,
which you can do with a double backslash.

Alternatively, you could use quotes around the string in Solr, which would
require you to escape the quotes in JSON, with a single backslash.

-- Jack Krupansky
Post by Iana Bondarska
Hi All,
could you please tell me if escaping special characters in search keywords
works in json api.
e.g. I have document
{
"string_s":"new value"
}
And I want to query "string_s" field with keyword "new value".
In path params api I can escape spaces in keyword as well as other special
characters with \ .
http://localhsot:8983/solr/dynamic_fields_qa/select?q=string_s:new\
value&wt=json&indent=true
http://localhsot:8983/solr/dynamic_fields_qa/select?q=*:*&json=
{"query":"string_s:new\ value"}
Best Regards,
Iana Bondarska
Loading...