@@ -284,8 +284,11 @@ var ElasticsearchManager = (function(){
284284        //(grab +1 so we know if there are more pages) 
285285        pagination  =  "LIMIT "  +  rowsToPull 
286286     } 
287+      var  queryReplacer  =  function ( )  { 
288+        return  userQuery 
289+      } 
287290     var  fullSqlQuery  =  tableConfig . sql_table . query 
288-         . replace ( / [ $ ] [ $ ] q u e r y / g,  userQuery ) 
291+         . replace ( / [ $ ] [ $ ] q u e r y / g,  queryReplacer ) 
289292        . replace ( "$$pagination" ,  pagination ) 
290293        . replace ( / [ $ ] [ $ ] i n d e x / g,  indices ) 
291294
@@ -374,12 +377,7 @@ var ElasticsearchManager = (function(){
374377  /** Find/replace lookups */ 
375378  function  incorporateLookupsAndScriptFields_ ( path ,  tableConfig ,  lookups ,  otherReplacements )  { 
376379    var  tableConfigStr  =  JSON . stringify ( tableConfig ) 
377-     Object . entries ( lookups ) . forEach ( function ( kv )  { 
378-       var  lookupStr  =  kv [ 0 ] 
379-       var  lookupRegex  =  new  RegExp ( escapeRegExp_ ( lookupStr ) ,  "g" ) 
380-       var  lookupJsonStr  =  JSON . stringify ( kv [ 1 ] ) 
381-       tableConfigStr  =  tableConfigStr . replace ( lookupRegex ,  lookupJsonStr ) 
382-     } ) 
380+     // Script fields first, since they may contain lookups: 
383381    var  scriptFields  =  Util . getJson ( tableConfig ,  [  path ,  "script_fields"  ] )  ||  [ ] 
384382    scriptFields . forEach ( function ( scriptField )  { 
385383      var  lookupStr  =  `"$$script_field(${ scriptField . name }  
@@ -389,11 +387,28 @@ var ElasticsearchManager = (function(){
389387        source : scriptField . script  ||  "" , 
390388        params : scriptField . params  ||  { } 
391389      } ) 
392-       tableConfigStr  =  tableConfigStr . replace ( lookupRegex ,  lookupJsonStr ) 
390+       var  replacer  =  function ( )  { 
391+         return  lookupJsonStr  //(so don't need to worry about special chars in the replacement) 
392+       } 
393+       tableConfigStr  =  tableConfigStr . replace ( lookupRegex ,  replacer ) 
393394    } ) 
395+     // Lookups: 
396+     Object . entries ( lookups ) . forEach ( function ( kv )  { 
397+       var  lookupStr  =  kv [ 0 ] 
398+       var  lookupRegex  =  new  RegExp ( escapeRegExp_ ( lookupStr ) ,  "g" ) 
399+       var  lookupJsonStr  =  JSON . stringify ( kv [ 1 ] ) 
400+       var  replacer  =  function ( )  { 
401+         return  lookupJsonStr  //(so don't need to worry about special chars in the replacement) 
402+       } 
403+       tableConfigStr  =  tableConfigStr . replace ( lookupRegex ,  replacer ) 
404+     } ) 
405+     // Other: 
394406    if  ( otherReplacements )  { 
395407      Object . entries ( otherReplacements ) . forEach ( function ( kv )  { 
396-         tableConfigStr  =  tableConfigStr . replace ( new  RegExp ( kv [ 0 ] ,  "g" ) ,  kv [ 1 ] ) 
408+         var  replacer  =  function ( )  { 
409+           return  kv [ 1 ]  //(so don't need to worry about special chars in the replacement) 
410+         } 
411+         tableConfigStr  =  tableConfigStr . replace ( new  RegExp ( kv [ 0 ] ,  "g" ) ,  replacer ) 
397412      } ) 
398413    } 
399414    return  JSON . parse ( tableConfigStr ) 
0 commit comments