Using subqueries with variables

Hi, I’m trying to write a query that has a subquery so that I can check 2 different fields for the same value but only if it is provided.

In my grafana dashboard I have a lucene query like this:

environment:$environment AND (firstname:${name} OR lastname:${name}) AND currency:$currency

The generated query looks like this:
“query”:“environment:Dev AND (firstname: OR lastname:)”

i.e. it is including the subquery even when the $name variable is not populated. The currency variable is excluded from the query as it is not populated which is what I want. Is what I am trying to do possible?