Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search query escape function #23

Closed
ergo opened this issue Jun 30, 2011 · 5 comments
Closed

search query escape function #23

ergo opened this issue Jun 30, 2011 · 5 comments
Labels

Comments

@ergo
Copy link

ergo commented Jun 30, 2011

would it make sense to make an escape function for raw data used in searches and include it within lib itself?

something like this

def solr_escape(input):
    special_chars = ["\\", '+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']',
                  '^', '"', '~', '*', '?', ':']
    for char in special_chars:
        if char in input:
            input = input.replace(char, '\\' + char)
    return input
@thomasbilk
Copy link

Yes. The search query ought to be escaped, though I would prefer a regexp like this one:

import re
def solr_escape(value):
    ESCAPE_CHARS_RE = re.compile(r'(?<!\\)(?P<char>[&|+\-!(){}[\]^"~*?:])')
    return ESCAPE_CHARS_RE.sub(r'\\\g<char>', value)

@thomasbilk
Copy link

So.

@swistakm
Copy link
Contributor

@thomasbilk, @ergo: take a look at solrq. It escapes queries for you and also has some other nice features. It can be used with pysolr or any other python solr client.

@torfsen
Copy link

torfsen commented Mar 26, 2018

Please note that / is also a reserved character since Lucene 4.

@stale
Copy link

stale bot commented Jun 24, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 24, 2018
@stale stale bot closed this as completed Jul 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants