2

I have a elastic query script that meant to calculate the ground distance with the parameters I gave it and I have to use it in multiple queries Is there a way to avoid this duplication , for-example a way to calculate a global variable and use it in all the scripts in this example I want to calculate distance and use it in other queries and not calculate it every time.

   "query":{
      "bool":{
         "filter":[
            {
               "script":{
                  "script":{
                     "lang":"painless",
                     "params":{
                        "9183":4896.4,
                        "9238":7487.3,
                        "9239":7491.2,
                        "9402":5150.7,
                        "9618":2069.5,
                        "9656":4028.6,
                        "9660":3612.1,
                        "10049":2823.4,
                        "10175":3679.2,
                        "10204":4975.8,
                        "10479":5167.8,
                        "10486":2762.7,
                        "10775":3193.2,
                        "10801":6596.8,
                        "11063":2814.7,
                        "11470":7309.8,
                        "11596":2818.4,
                        "11780":6254.9,
                        "11954":3907.4,
                        "12001":6377.1,
                        "12071":3065.1,
                        "12258":2264.4,
                        "12333":6048.1,
                        "13088":1844.1,
                        "13566":2266.9,
                        "13576":3946.4,
                        "13636":3620.8,
                        "13696":5970.7,
                        "13700":1648.4,
                        "13810":4451.7,
                        "13884":2935,
                        "13902":2193.5,
                        "14271":3963.1,
                        "14345":2979,
                        "14360":2260,
                        "14363":2533,
                        "14375":4024.7,
                        "14685":1849,
                        "14703":3769.1,
                        "14913":3943,
                        "14973":6767.3,
                        "14976":5951.1,
                        "15009":3894.1,
                        "15270":4590.9,
                        "15348":4954.9,
                        "15360":4540.2,
                        "15447":5774.2,
                        "15534":6915.4,
                        "15612":5732.6,
                        "15789":1556.5,
                        "15991":3018.4,
                        "16051":6406.7,
                        "16201":2814.5,
                        "16247":3254.8,
                        "16525":2193.2,
                        "16547":4422.9,
                        "16561":3540.8,
                        "16613":6754.8,
                        "16727":3264.6,
                        "16731":1956.7,
                        "17063":4275.2,
                        "17181":4354.4,
                        "17657":2913.3,
                        "17757":1762.8,
                        "17761":3522.6,
                        "17777":2029,
                        "17895":3989,
                        "17965":1972.1,
                        "18553":3753.8,
                        "18586":2186.5,
                        "18631":1959.4,
                        "19006":7236,
                        "19022":3970.5,
                        "19134":3753.7,
                        "19150":7410.3,
                        "19180":4115,
                        "19196":5071.1,
                        "19276":1780.9,
                        "19387":3530.3,
                        "19393":2040.8,
                        "19792":4689.2,
                        "19903":5166.4,
                        "19981":6781.6,
                        "20132":2498.1,
                        "20372":6799.3,
                        "20387":5453.5,
                        "20826":3829.1,
                        "20838":2317.9,
                        "20856":2399.4,
                        "21134":1339.1,
                        "21233":3963.6,
                        "21266":4757,
                        "21269":3583.9,
                        "21320":3926.3,
                        "21490":5100.5,
                        "21910":4792.5,
                        "23104":7408.4,
                        "23125":4892.9,
                        "23167":3526.2,
                        "24625":6983.8,
                        "24985":6782.1,
                        "25090":3500.3,
                        "25129":1451.9,
                        "25375":4687,
                        "25376":4050.8,
                        "25458":2138.1,
                        "25478":4776.8,
                        "25652":6463,
                        "26224":6259.5,
                        "26567":2313.6,
                        "26594":6465.6,
                        "26651":2068,
                        "26824":3592,
                        "26826":6396.7,
                        "26890":3790.7,
                        "26926":5943.1,
                        "26932":5018.9,
                        "26956":3626.5,
                        "27012":4201.9,
                        "27016":4209.1,
                        "27017":4212.2,
                        "27019":4203.1,
                        "27020":4202.2,
                        "27021":4211.1,
                        "27026":4190.1,
                        "27053":1834.6,
                        "27055":5564.1,
                        "27135":6467.4,
                        "27232":3588.8,
                        "27242":5898.2,
                        "27257":4061.2,
                        "27266":6913.4,
                        "27318":3917.6,
                        "27346":2122.8,
                        "27365":7021,
                        "27368":7619.7,
                        "27396":5375.8,
                        "27460":7504.4,
                        "27492":3885.6,
                        "27557":2989.4,
                        "27594":2830.4,
                        "27683":3882.7,
                        "27828":3980.8,
                        "27865":6066.5,
                        "28018":6863.6,
                        "28057":2569.9,
                        "28060":2569.7,
                        "minDistance":0
                     },
                     "source":"def a=doc['id'].getValue();if (!params.containsKey(a.toString())) {return false;}double distance=params[a.toString()]; return distance <= doc['maxClassDeliveryDistance'].getValue() && distance >= params['minDistance']"
                  }
               }
            },
            {
               "script":{
                  "script":{
                     "lang":"painless",
                     "params":{
                        "9183":4896.4,
                        "9238":7487.3,
                        "9239":7491.2,
                        "9402":5150.7,
                        "9618":2069.5,
                        "9656":4028.6,
                        "9660":3612.1,
                        "10049":2823.4,
                        "10175":3679.2,
                        "10204":4975.8,
                        "10479":5167.8,
                        "10486":2762.7,
                        "10775":3193.2,
                        "10801":6596.8,
                        "11063":2814.7,
                        "11470":7309.8,
                        "11596":2818.4,
                        "11780":6254.9,
                        "11954":3907.4,
                        "12001":6377.1,
                        "12071":3065.1,
                        "12258":2264.4,
                        "12333":6048.1,
                        "13088":1844.1,
                        "13566":2266.9,
                        "13576":3946.4,
                        "13636":3620.8,
                        "13696":5970.7,
                        "13700":1648.4,
                        "13810":4451.7,
                        "13884":2935,
                        "13902":2193.5,
                        "14271":3963.1,
                        "14345":2979,
                        "14360":2260,
                        "14363":2533,
                        "14375":4024.7,
                        "14685":1849,
                        "14703":3769.1,
                        "14913":3943,
                        "14973":6767.3,
                        "14976":5951.1,
                        "15009":3894.1,
                        "15270":4590.9,
                        "15348":4954.9,
                        "15360":4540.2,
                        "15447":5774.2,
                        "15534":6915.4,
                        "15612":5732.6,
                        "15789":1556.5,
                        "15991":3018.4,
                        "16051":6406.7,
                        "16201":2814.5,
                        "16247":3254.8,
                        "16525":2193.2,
                        "16547":4422.9,
                        "16561":3540.8,
                        "16613":6754.8,
                        "16727":3264.6,
                        "16731":1956.7,
                        "17063":4275.2,
                        "17181":4354.4,
                        "17657":2913.3,
                        "17757":1762.8,
                        "17761":3522.6,
                        "17777":2029,
                        "17895":3989,
                        "17965":1972.1,
                        "18553":3753.8,
                        "18586":2186.5,
                        "18631":1959.4,
                        "19006":7236,
                        "19022":3970.5,
                        "19134":3753.7,
                        "19150":7410.3,
                        "19180":4115,
                        "19196":5071.1,
                        "19276":1780.9,
                        "19387":3530.3,
                        "19393":2040.8,
                        "19792":4689.2,
                        "19903":5166.4,
                        "19981":6781.6,
                        "20132":2498.1,
                        "20372":6799.3,
                        "20387":5453.5,
                        "20826":3829.1,
                        "20838":2317.9,
                        "20856":2399.4,
                        "21134":1339.1,
                        "21233":3963.6,
                        "21266":4757,
                        "21269":3583.9,
                        "21320":3926.3,
                        "21490":5100.5,
                        "21910":4792.5,
                        "23104":7408.4,
                        "23125":4892.9,
                        "23167":3526.2,
                        "24625":6983.8,
                        "24985":6782.1,
                        "25090":3500.3,
                        "25129":1451.9,
                        "25375":4687,
                        "25376":4050.8,
                        "25458":2138.1,
                        "25478":4776.8,
                        "25652":6463,
                        "26224":6259.5,
                        "26567":2313.6,
                        "26594":6465.6,
                        "26651":2068,
                        "26824":3592,
                        "26826":6396.7,
                        "26890":3790.7,
                        "26926":5943.1,
                        "26932":5018.9,
                        "26956":3626.5,
                        "27012":4201.9,
                        "27016":4209.1,
                        "27017":4212.2,
                        "27019":4203.1,
                        "27020":4202.2,
                        "27021":4211.1,
                        "27026":4190.1,
                        "27053":1834.6,
                        "27055":5564.1,
                        "27135":6467.4,
                        "27232":3588.8,
                        "27242":5898.2,
                        "27257":4061.2,
                        "27266":6913.4,
                        "27318":3917.6,
                        "27346":2122.8,
                        "27365":7021,
                        "27368":7619.7,
                        "27396":5375.8,
                        "27460":7504.4,
                        "27492":3885.6,
                        "27557":2989.4,
                        "27594":2830.4,
                        "27683":3882.7,
                        "27828":3980.8,
                        "27865":6066.5,
                        "28018":6863.6,
                        "28057":2569.9,
                        "28060":2569.7
                     },
                     "source":"def a=doc['id'].getValue();if (!params.containsKey(a.toString())) {return false;}double distance=params[a.toString()];return distance <= doc['maxDeliveryRadius'].getValue()"
                  }
               }
            }
         ]
      }
   }
}

Mapping :

{
   "id":{
      "type":"long"
   },
   "maxClassDeliveryDistance":{
      "type":"long"
   },
   "maxDeliveryRadius":{
      "type":"long"
   }
}
slifer2015
  • 682
  • 6
  • 12

1 Answers1

2

You should use a stored script instead so you can reference it by id:

POST _scripts/distance-script
{
  "script": {
    "lang": "painless",
    "source":"def a=doc['id'].getValue();if (!params.containsKey(a.toString())) {return false;}double distance=params[a.toString()];return distance <= doc['maxDeliveryRadius'].getValue()"
  }
}

You can then reference the above script in your query:

{
  "query": {
    "bool": {
      "filter": [
        {
          "script": {
            "script": {
              "lang": "painless",
              "params": { ... },
              "id": "distance-script"
            }
          }
        },
        {
          "script": {
            "script": {
              "lang": "painless",
              "params": { ... },
              "id": "distance-script"
            }
          }
        }
      ]
    }
  }
}

UPDATE

I think we can solve this differently. You have two script queries in a bool/filter which means AND. Also, you have the same parameters in both scripts. Hence, you could combine both scripts and get away with a single script, like this one:

POST _scripts/distance-script
{
  "script": {
    "lang": "painless",
    "source":"""
    def a = doc.id.value.toString();
    if (!params.containsKey(a) {
        return false;
    }

    double distance = params[a];
    return distance <= doc.maxClassDeliveryDistance.value && distance >= params['minDistance'] && distance <= doc.maxDeliveryRadius.value;
    """
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • I think you misunderstood the question I don't want to calculate `double distance=params[a.toString()];` every time because the value is the same for every id – slifer2015 Aug 18 '19 at 03:20
  • Then, if distance is constant for each ID, you should store the value as a new field in each document, so you don't need all those params... but it's also a good idea to use store scripts ;-) – Val Aug 18 '19 at 04:43
  • actually I'ts the same per every request thus I can't save it in my document And It will change in the user next request – slifer2015 Aug 18 '19 at 05:03
  • I really appreciate your endeavor, but as I said you are calculating the below phrase over and over ` double distance = params[a];` while this is constant per every search query (per user request) Is there a way that I calculate the specified phrase just once in the scope of query and use the value in multiple script queries – slifer2015 Aug 18 '19 at 08:52
  • you're not really calculating anything since you're just dereferencing whatever is in the params hash... I'm curious why you think this is a problem. – Val Aug 18 '19 at 08:54
  • I thought repeating `double distance = params[a];` in every query will be an over-head for my query , thanks a lot :) – slifer2015 Aug 19 '19 at 06:17
  • I don't see why. Look towards what I suggested, i.e. to combine both script into a single one. – Val Aug 19 '19 at 06:27
  • Is there a way to calculate global variable `double distance = params[a];` once in the scope of query and use the result in other inner queries ? That's my real question – slifer2015 Aug 24 '19 at 13:43