2015-05-09 55 views
5

我有以下查詢當前使用動態腳本。之後我發現我的主機不支持這個,因爲它具有更廣泛的安全影響。我將如何重寫這個腳本,以便它不使用動態腳本?不使用動態腳本查詢

{ 
    "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": {} 
    } 
} 
+0

你可以[將腳本移動到文件](https: //www.elastic.co/blog/running-groovy-scripts-without-dynamic-scripting)?或者,更好地說,您的主機是否允許您在ES安裝目錄中放置.groovy文件? –

回答

2

您可以store your scripts on the file system並從您的查詢/聚合中引用它們。

,內容如下

doc['loc'].lat 

創建一個名爲config/scripts/lat.groovy文件創建一個名爲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": {} 
    } 
} 
+0

如果禁用動態腳本,則「索引腳本」將不起作用;-)。 –

1

除了放置實際的腳本一個.groovy文件,就像我提到的(更多詳細信息here),您可以定義一個native腳本。比groovy-on-file方法更多參與,但更靈活。在你的情況下,腳本非常簡單:-)但你不需要靈活性,但是存在這個選項(真正的實現示例):ElasticSearch: aggregation on _score field w/ Groovy disabled