package com.loginsigh;

import org.elasticsearch.common.inject.internal.Nullable;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;
import org.elasticsearch.script.ScriptException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by kira on 16/11/24.
 */
public class GrokNativeScriptFactory implements NativeScriptFactory {
    public ExecutableScript newScript(@Nullable Map<String, Object> params) {

        String pattern = params == null ? null :
                XContentMapValues.nodeStringValue(params.get("pattern"), null);

        String fieldName = params == null ? null :
                XContentMapValues.nodeStringValue(params.get("fieldName"), null);

        String groupkeys = params == null ? null :
                XContentMapValues.nodeStringValue(params.get("groupkeys"), null);


        if (fieldName == null || fieldName.equals("")) {
            throw new ScriptException("Missing field parameter");
        }
        if (pattern == null || pattern.equals("")) {
            throw new ScriptException("Missing field parameter");
        }

        List<String> groupkeyList = new ArrayList<>();

        if (groupkeys != null && !groupkeys.equals("")) {
            groupkeyList = Arrays.asList(groupkeys.split(","));
        }

        return new GrokNativeScript(pattern, fieldName, groupkeyList);
    }

    public boolean needsScores() {
        return false;
    }
}
@@ -1020,27 +1020,27 @@
  %7C%7C 
-fieldName.equals(%22%22
+%22%22.equals(fieldName
 )) %7B
@@ -1151,23 +1151,18 @@
  %7C%7C 
-pattern
+%22%22
 .equals(
 %22%22))
@@ -1149,34 +1149,39 @@
 ll %7C%7C %22%22.equals(
-%22%22
+pattern
 )) %7B%0A           
@@ -1340,27 +1340,27 @@
 && !
-groupkeys.equals(%22%22
+%22%22.equals(groupkeys
 )) %7B

clean code