【问题标题】:Query without using dynamic scripting不使用动态脚本进行查询
【发布时间】:2015-07-20 12:36:29
【问题描述】:

我有以下当前使用动态脚本的查询。从那以后,我发现我的主机不支持这一点,因为它具有更广泛的安全影响。我将如何重写此脚本使其不使用动态脚本?

{
  "size": 0,
  "aggs": {
    "filtered_cells": {
      "filter": {
        "geo_bounding_box": {
          "loc": {
            "top_left": "58.645976, -13.515625",
            "bottom_right": "50.524473, 2.436523"
          }
        }
      },
      "aggs": {
        "cells": {
          "geohash_grid": {
            "field": "loc",
            "precision": 2
          },
          "aggs": {
            "center_lat": {
              "avg": {
                "script": "doc['loc'].lat"
              }
            },
            "center_lon": {
              "avg": {
                "script": "doc['loc'].lon"
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

【问题讨论】:

  • 你能move the script to file吗?或者,更好的说法是,您的主机是否允许您将 .groovy 文件放在 ES 安装目录中?

标签: elasticsearch elasticsearch-query


【解决方案1】:

您可以store your scripts on the file system 并在您的查询/聚合中引用它们。

创建一个名为config/scripts/lat.groovy的文件,内容如下

 doc['loc'].lat

创建另一个名为config/scripts/lon.groovy 的文件,内容如下

 doc['loc'].lon

然后将您的查询更改为:

{
  "size": 0,
  "aggs": {
    "filtered_cells": {
      "filter": {
        "geo_bounding_box": {
          "loc": {
            "top_left": "58.645976, -13.515625",
            "bottom_right": "50.524473, 2.436523"
          }
        }
      },
      "aggs": {
        "cells": {
          "geohash_grid": {
            "field": "loc",
            "precision": 2
          },
          "aggs": {
            "center_lat": {
              "avg": {
                "script_file": "lat"
              }
            },
            "center_lon": {
              "avg": {
                "script_file": "lon"
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "match_all": {}
  }
}

【讨论】:

  • 如果动态脚本被禁用,“索引你的脚本”将不起作用;-)。
【解决方案2】:

除了将实际脚本放在.groovy 文件上,就像我提到的(更多细节here),您还可以定义native 脚本。比 groovy-on-file 方法涉及更多,但更灵活。在您的情况下,脚本非常简单 :-) 并且您不需要灵活性,但是存在该选项(确实来自您的实现示例):ElasticSearch: aggregation on _score field w/ Groovy disabled

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2021-10-09
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多