当前位置: 首页 > 知识库问答 >
问题:

如何在弹性搜索中避免重复无痛脚本

梁丘缪文
2023-03-14

我有一个弹性查询脚本,用于使用我给出的参数计算地面距离,我必须在多个查询中使用它。是否有一种方法可以避免这种重复,例如,有一种计算全局变量并在所有脚本中使用它的方法。在本例中,我希望计算<code>距离

   "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()"
                  }
               }
            }
         ]
      }
   }
}

映射:

{
   "id":{
      "type":"long"
   },
   "maxClassDeliveryDistance":{
      "type":"long"
   },
   "maxDeliveryRadius":{
      "type":"long"
   }
}

共有1个答案

高山
2023-03-14

您应该使用存储的脚本,以便您可以通过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()"
  }
}

然后,您可以在查询中引用上述脚本:

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

更新

我认为我们可以用不同的方式解决这个问题。在<code>bool/filter<code>中有两个脚本查询,这意味着和。此外,两个脚本中的参数相同。因此,您可以将这两个脚本组合起来,只使用一个脚本,如下所示:

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;
    """
  }
}
 类似资料:
  • 我是ES的新手,我正在尝试使用聚合编写搜索查询。 在写同样的东西时,我面临着无痛脚本的问题。 哪里可以得到完整的无痛脚本文档,用于弹性搜索?

  • 你能建议如何更新基于条件字段的文档(使用脚本-我猜是无痛的)吗? 其目的是添加/删除文档中的值 所以如果我有这些输入文档: 我想表演这样的东西: 使用以下命令更新具有“flag=true”的所有文档: 添加标签:"me","one" 已删除标签:“goo”、“foo” 所以预期结果应该是这样的:

  • 我想将地理信息的旧数据重新索引到地理点。 以前的数据包含这种格式的位置。 我想像这样在数组中的地理点中重新索引位置 这是映射 这是我执行重新索引的请求。 问题它没有覆盖位置:{} 到位置:[]

  • 我在elasticsearch中创建了一个观察器,当我们在索引中10分钟没有新条目或事件时,它会报告,这可以通过查看条目中的源字段来进一步划分。 我只得到了索引的最后10分钟,并查看了桶中不存在哪个源。 为此,我首先创建我们收到的所有源类型的列表,然后从返回的存储桶键创建一个列表。然后,我想比较列表以查看缺少哪个列表,然后将其传递到消息中。 我在for循环中遇到了一个通用错误。任何反馈对弹性和无痛

  • 我目前正在使用Elastic search 5.2,并尝试使用以下rest api执行upsert操作: > http://ip:9200/indexname/typename/id/_update Json有效载荷: } 此api执行以下操作: 1.)如果在弹性搜索中没有找到索引,则使用upsert字段中提供的json创建索引。< br> 2。)如果索引存在,那么它通过运行文档中提供的简单脚本来

  • 想知道是否有人知道为什么这个查询更新运行良好,但即使任务运行到完成并声称所有记录都已更新,也不会删除任何内容?我可以使用相同的语法删除所有人员对象,而不会出现任何问题。为什么 ES 不删除对象属性? 现在,如果我使用从上述调用返回的ID观看任务 GET/_ tasks/qle uj 8 jqqgopfgse ZL 7 u 9 q:1776664 我得到了这个(简短的版本),声称所有记录都已更新。然