You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Paolo Castagna <ca...@googlemail.com> on 2010/06/18 09:37:18 UTC
SolrQuery and escaping special characters
Hi,
I am using Solr v1.4 and SolrJ on the client side.
I am not sure how SolrJ behaves regarding "escaping" special characters
[1] in a query string.
SolrJ does URL encoding of the query string it sends to Solr.
Do I need to escape special characters [1] when I construct a SolrQuery
object or not?
For example, if I want to search for "http://example.com#foo" in a
"uri" field, should I use:
(a) SolrQuery query = new SolrQuery("uri:http://example.com#foo");
(b) SolrQuery query = new SolrQuery("uri:http\\://example.com#foo");
which become respectively:
(a') q=uri%3Ahttp%3A%2F%2Fexample.com%23foo
(b') q=uri%3Ahttp%5C%3A%2F%2Fexample.com%23foo
My understanding is that SolrJ users are supposed to escape special
characters, therefore (b) is the correct way.
If this is the case, what's the best way to escape a query string which
might contain field names and URIs in their field values?
Thanks,
Paolo
[1]
http://lucene.apache.org/java/2_9_1/queryparsersyntax.html#Escaping%20Special%20Characters
Re: SolrQuery and escaping special characters
Posted by Lance Norskog <go...@gmail.com>.
Thank you for the example, Ahmet!
Paolo- what you did in choice 'b' does what you want - it escapes the
colon in the URI. But Ahmet's example is a better way because it does
not have the 'double-escaping' problem that us old Unix types are so
familiar with.
On 6/18/10, Chris Hostetter <ho...@fucit.org> wrote:
>
> : I am not sure how SolrJ behaves regarding "escaping" special characters
> : [1] in a query string.
>
> SolrJ encodes your strings for "transport" -- ie: it handles URL escaping
> if it's sending hte query in a GET URL -- but id doesn't do "query parser
> escaping" ... mainly because it has no way of knowing which query parser
> you are using.
>
>
> -Hoss
>
>
--
Lance Norskog
goksron@gmail.com
Re: SolrQuery and escaping special characters
Posted by Chris Hostetter <ho...@fucit.org>.
: I am not sure how SolrJ behaves regarding "escaping" special characters
: [1] in a query string.
SolrJ encodes your strings for "transport" -- ie: it handles URL escaping
if it's sending hte query in a GET URL -- but id doesn't do "query parser
escaping" ... mainly because it has no way of knowing which query parser
you are using.
-Hoss
Re: SolrQuery and escaping special characters
Posted by Ahmet Arslan <io...@yahoo.com>.
> My understanding is that SolrJ users are supposed to escape
> special characters, therefore (b) is the correct way.
> If this is the case, what's the best way to escape a query
> string which
> might contain field names and URIs in their field values?
Easiest thing is to use RawQParserPlugin or FieldQParserPlugin.
e.g. SolrQuery.setQuery("{!field f=uri}http://www.apache.org")