Search code examples
solrlucene

Escape special characters in Solr query while searching on fields?


Solr Query: fooField:foovalue OR barField:barValue

This query can be query override from browser which risks potential injection attacks. But escaping special characters like : and whitespace doesn't returns result as expected.

Is there a way to escape the query and search on solr fields to get desired results?


Solution

  • The edismax query handler is made for situations like this - if you don't want the user to be able to attach field names themselves, use qf to tell Solr which fields it should search, then escape the query properly and send it in the q parameter as raw text.

    If you do want to search different values in different fields, you'll have to escape each value and prefix it with the field name as you've shown.