11

Is the forward slash "/" a reserved character in solr field names?

I'm having trouble writing a solr sort query which will parse for fields containing a forward slash "/"

When making an http query to my solr server:

q=*&sort=normal+desc

Will work but

q=*&sort=with/slash+desc
q=*&sort=with%2Fslash+desc

Both fail say "can not use FieldCache on multivalued field: with"

Each solr document contains two int fields "normal", and "with/slash". With my solr schema indexing the fields as so

...
<field name="normal" type="int"   indexed="true" stored="true" required="false" />
<field name="with/slash" type="int"   indexed="true" stored="true" required="false" />
...

Is there any special way I need to encode forward slashes in solr? Or are there any other delimiter characters I can use? I'm already using '-' and "." for other purposes.

Akusete
  • 10,704
  • 7
  • 57
  • 73
  • 2
    For escaping characters, please check out http://stackoverflow.com/questions/1133573/solr-sanitizing-query for escaping Solr characters and scroll to the bottom of http://wiki.apache.org/solr/SolrQuerySyntax for how to compose URLs . Hope this helps – Jesvin Jose Aug 29 '11 at 07:31
  • Thanks for the links, however the "/" character is not in the list of suspect solr characters, and my URL encoding is not a problem. FYI, I've decided to work around this problem by replacing / with _ in my field names. – Akusete Aug 29 '11 at 08:47
  • Doh! I just ran into the same problem. `/` isn't in the list I checked for special characters either. I've tried encoding, quotes, etc. No luck for me either =( – Justin Mar 15 '12 at 21:40
  • 1
    Can it be that you are using a tokenizer wich splits the index on a / try to use the solr.StrField in the schema.xml – sandrozbinden Apr 24 '12 at 12:44
  • The problem seems to be that Solr is treating your string as a regex when a forward slash is found: http://stackoverflow.com/questions/17798300/lucene-queryparser-with-in-query-criteria ; escaping with slashes, as @tw123 suggested, should solve your problem. – Haroldo_OK Sep 29 '14 at 17:23
  • https://stackoverflow.com/questions/16657152/matching-a-forward-slash-with-a-regex – Krzysztof Trekiel Nov 30 '18 at 12:35

4 Answers4

6

I just came across the same problem, and after some experimentation found that if you have a forward-slash in the field name, you must escape it with a backslash in the Solr query (but note that you do not have to do this in the field list parameter, so a search looking for /my/field/name containing my_value is entered in the "q" field as:

\/my\/field\/name:my_value

I haven't tried the sort field, but try this and let us know :)

This is on Solr 4.0.0 alpha.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
tw123
  • 61
  • 1
  • 2
3

From the solr wiki at https://wiki.apache.org/solr/SolrQuerySyntax :

Solr 4.0 added regular expression support, which means that '/' is now a special character and must be escaped if searching for literal forward slash.

Xavier Portebois
  • 3,354
  • 6
  • 33
  • 53
  • Nice, thanks for the info. Prior to 4.0, the characters that needed escaping were ``+ - && || ! ( ) { } [ ] ^ " ~ * ? : \``. The forward slash wasn't one of them. It is now, though. – Priidu Neemre Oct 29 '21 at 11:49
0

In my case I needed to search for forward slash / with wild card *, e.g.:

+(*/*)
+(*2016/17*)

I Tried to escape it like so:

+(*2016\/*)
+(*2016\/17*)

but that didn't work also.

the solution was to wrap the text with double quote " like do:

+("*\/*")
+("*/*")

+("*2016\/17*")
+("*2016/17*")

both returned the same result with and without escaping the forward slash

Waqleh
  • 9,741
  • 8
  • 65
  • 103
0

In my case I was using Solr Admin UI to run queries I had to search for a URL with a forward slash.

text:www.myurl.com/test => returned unwanted results text:"www.myurl.com/test" => returned desired results.

For your implementation of Solr Search indexing, enclosing the search string in quotes if you're using Solr Admin UI would be sufficient.

Onkar Singh
  • 109
  • 2
  • 10