Search code examples
solrlucenesolrcloudfaceted-searchsolr-query-syntax

Escape Special Character In Solr


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 '&'


Solution

  • 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.