1

I'm getting following error while escaping special character '&' after running http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:"Toys \& Games " this query in Solr

{
"responseHeader": {
    "zkConnected": true,
    "status": 400,
    "QTime": 0,
    "params": {
        "q": "*:*",
        "Games \"": "",
        "fq": "Category:\"Toys \\",
        "rows": "70"
    }
},
"error": {
    "metadata": [
        "error-class", "org.apache.solr.common.SolrException",
        "root-error-class", "org.apache.solr.parser.TokenMgrError"
    ],
    "msg": "org.apache.solr.search.SyntaxError: Cannot parse 'Category:\"Toys \\': Lexical error at line 1, column 17.  Encountered: <EOF> after : \"\\\"Toys \\\\\"",
    "code": 400
}}

Category field contains values like below

"Category":["Toys & Games "," Learning & Education "," Science Kits & Toys"]
"Category":["Home & Kitchen "," Home Décor "," Window Treatments "," Window Stickers & Films ", " Window Films"],

And category field is of type string with multivalued=true

      {
    "name":"Category",
    "type":"string",
    "multiValued":true,
    "stored":true},

How to search properly for Category:"Toys & Games "

NOTE: I tried http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:Toys* AND *"Games "&rows=70 this query and it worked fine, but If I excatly want to serach for string 'Toys & Games ' how to do that by properly escaping special character '&'

Swastik
  • 144
  • 2
  • 19
  • queries that contain double quotation marks, use triple slashes \\\: For query syntax: One slash \ to escape the " For the JSON string syntax: Two slashes \\ to escape the \ Triple slashes \\\ escape both characters in \" to produce \\ (an escaped escape) and \" (an escaped double quote). – Abhijit Bashetti Nov 03 '21 at 13:51
  • Getting error because of special character '&' . Double quotation is not causing any problem. – Swastik Nov 03 '21 at 14:00
  • 1
    You don't need to escape `&`. https://solr.apache.org/guide/8_10/the-standard-query-parser.html#escaping-special-characters – EricLavault Nov 03 '21 at 14:19

1 Answers1

1

You'll need to encode some of the characters. For example the following command:

$ curl 'http://localhost:8983/solr/puldata/select?fq=title_t%3A%22Woody%20Herman%20%26%20His%20Orchestra%22&q=*&start=0'

will query fq=title_t:"Woody Herman & His Orchestra". Notice how the :, ", spaces, and the & characters are encoded.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73
  • As suggested by @EricLavault no need to escape '&', escaping is needed for '&&'. So considering @Hector Correa answer, solved error by encoding '&' with '%26'. Instead of `http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:"Toys & Games "` using this `http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:"Toys %26 Games "` worked for me – Swastik Nov 03 '21 at 15:08