2015-04-21 67 views
1

我已經創造了Elasticsearch插件並安裝成功(http://localhost:9200/_nodes/plugins/顯示它安裝的。)但是我似乎無法在我的查詢中使用它 - 我只得到錯誤。 「ScriptException [動態腳本爲[常規]禁用]」。好像我需要一個不同的lang設置。但我試過'lang':'java'。沒有快樂。我試過lang:表達。然後我得到「ExpressionScriptCompilationException [未知變量[的MaxMind在表達」。我如何訪問我創建的插件?還是我需要做更多的事情來註冊它?如何使用Elasticsearch插件定義的過濾器

我一直在關注這個優秀導遊: https://github.com/imotov/elasticsearch-native-script-example

但隻字未提查詢應該怎麼寫。

我AbstractPlugin:

package org.elasticsearch.plugin.maxmind; 

import java.util.Collection; 

import org.elasticsearch.common.collect.Lists; 
import org.elasticsearch.common.inject.Module; 
import org.elasticsearch.plugins.AbstractPlugin; 
import org.elasticsearch.script.ScriptModule; 

import org.elasticsearch.plugin.maxmind.GeoLoc; 

public class MaxMind extends AbstractPlugin { 
    @Override public String name() { 
     return "maxmind"; 
    } 

    @Override public String description() { 
     return "Plugin to annotate ip addresses with maxmind geo data"; 
    } 

    // Thanks https://github.com/imotov/elasticsearch-native-script-example 
    public void onModule(ScriptModule module) { 
     module.registerScript("geoloc", GeoLoc.Factory.class); 
    } 
} 

註名 「geoloc」。這是我在查詢中使用的名稱嗎?

我GeoLoc模塊:

package org.elasticsearch.plugin.maxmind; 

import java.util.HashMap; 
import java.util.Map; 

import org.elasticsearch.script.ScriptException; 
import org.elasticsearch.common.Nullable; 
import org.elasticsearch.common.xcontent.support.XContentMapValues; 
import org.elasticsearch.index.fielddata.ScriptDocValues; 
import org.elasticsearch.script.AbstractSearchScript; 
import org.elasticsearch.script.ExecutableScript; 
import org.elasticsearch.script.NativeScriptFactory; 

public class GeoLoc extends AbstractSearchScript { 

    public static class Factory implements NativeScriptFactory { 

     // called on every search on every shard 
     @Override 
     public ExecutableScript newScript 
      (@Nullable Map<String, Object> params) 
     { 
      String fieldName = params == null ? null: 
       XContentMapValues.nodeStringValue(params.get("field"), null); 
      if (fieldName == null) { 
       throw new ScriptException("Missing field parameter"); 
      } 
      return new GeoLoc(fieldName); 
     } 
    } 

    private final String fieldName; 

    private GeoLoc(String fieldName) { 
     this.fieldName = fieldName; 
    } 

    @Override 
    public Object run() { 
     ScriptDocValues docValue = (ScriptDocValues) doc().get(fieldName); 
     if (docValue != null && !docValue.isEmpty()) { 
      // TODO: real geolocation here 
      HashMap fakeloc = new HashMap<String, String>(); 
      fakeloc.put("lat", "1.123"); 
      fakeloc.put("lon", "44.001"); 
      fakeloc.put("basedon", docValue); 
      return fakeloc; 
     } 
     return false; 
    } 
} 

我的查詢:

{ 
    "_source": [ 
     "uri", 
     "user_agent", 
     "server_ip", 
     "server_port", 
     "client_ip", 
     "client_port" 
    ], 
    "query": { 
     "filtered": { 
      "filter": {} 
     } 
    }, 
    "script_fields": { 
     "test1": { 
      "params": { 
       "field": "client_ip" 
      }, 
      "script": "geoloc" // is this right? 
     } 
    }, 
    "size": 1 
} 
+0

我知道你試過'郎鹹平:java',但是它與'郎工作:native'? –

+1

嗚呼!謝謝@LeeH。讓它成爲答案,我會接受它。謝謝! – Julian

+0

完成,很高興它幫助! –

回答

5

你應該能夠與你的腳本指定lang: "native",用Java編寫,並註冊了registerScript任何腳本是 「原生」類型。

相關問題