2
votes

Dynamic value or a variable doesn't work inside a elasticsearch "range" query.

For explaining more this is a elasticsearch range query which find productId from 1000 to 11100, which is working perfectly ---

$json = '{
   "query" : {
       "range" : {
           "productId" : {
               "from" : '1000',
               "to" : '11100'
           }
       }
   }
}';

On the other hand using the same query with a variable with the same value it returns me error like ---

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures

$a =1000;
$b = 11100;

$json = '{
       "query" : {
           "range" : {
               "productId" : {
                   "from" : '$a',
                   "to" : '$b'
               }
           }
       }
    }';

Do anyone knows where i am making the mistake.

Any suggestion will be great help. Thanks in advanced.

1
what happened when you remove the quote around $a and $b (exemple : "from" : $a instead of "from" : '$a')vincent
@vincent it also returns the same error, i have tried it in many ways -- $a and '.$a.'Istiak Mahmood
could you paste the full error?Marco Bonzanini

1 Answers

1
votes

If this is PHP, there's a problem with string concatenation:

$a = 1000;
$b = 11100;

$json = '{
   "query" : {
       "range" : {
           "productId" : {
               "from" : '.$a.',
               "to" : '.$b.'
           }
       }
   }
}';

see the dots around the variables.

If you run the original piece of code by itself, the PHP parser should give you a parse error.