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.
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.leads to a syntax error, since
-
is a special character:The alternative options I tried were to escape the
-
:or surround the value in quotes:
but in either case, the ElasticSearch query is wrong.
My temporary solution has been to use
.strip('\"')
in theERange
class when settingself.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.