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

Unable specify negative values in Range queries #99

Open
ElliottKasoar opened this issue Aug 15, 2023 · 0 comments
Open

Unable specify negative values in Range queries #99

ElliottKasoar opened this issue Aug 15, 2023 · 0 comments

Comments

@ElliottKasoar
Copy link

I am attempting to build ElasticSearch queries, which include ranges that can take negative values, such as {'range': {'values': {'lt': '10', 'gt': '-10'}}}, but there doesn't seem to be a way to do this.

from luqum.parser import parser
from luqum.elasticsearch import ElasticsearchQueryBuilder

tree = parser.parse('values:{-1 TO 5}')

leads to a syntax error, since - is a special character:

File [~/venv/lib/python3.8/site-packages/luqum/parser.py:383](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a224442227d.vscode-resource.vscode-cdn.net/home/ubuntu/notebooks/~/venv/lib/python3.8/site-packages/luqum/parser.py:383), in p_error(p)
    381     error = "unexpected  '%s'" % p.value
    382     pos = "position %d" % p.lexpos
--> 383 raise ParseSyntaxError("Syntax error in input : %s at %s!" % (error, pos))

ParseSyntaxError: Syntax error in input : unexpected  '-' at position 8!

The alternative options I tried were to escape the -:

tree = parser.parse('values:{\-1 TO 5}')
es_builder = ElasticsearchQueryBuilder()

print(es_builder(tree))
{'range': {'values': {'lt': '5', 'gt': '\\-1'}}}

or surround the value in quotes:

tree = parser.parse('values:{"-1" TO 5}')
es_builder = ElasticsearchQueryBuilder()

print(es_builder(tree))
{'range': {'values': {'lt': '5', 'gt': '"-1"'}}}

but in either case, the ElasticSearch query is wrong.

My temporary solution has been to use .strip('\"') in the ERange class when setting self.gt etc, but I'm not sure if that's the best solution, and I may well be missing something that can avoid changing the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant