欢迎投稿

今日深度:

elasticsearch5.2.2 插件开发(三)ScriptPlugin 的实现,

elasticsearch5.2.2 插件开发(三)ScriptPlugin 的实现,


本文地址  http://blog.csdn.net/makefriend7/article/details/60770564
这个插件实现的功能如下 定义一个“feature"的字段,而该字段的打分规则是由我们自己制定。即,如果查询的字段长度和他一样打99.9分,比它小则打66.6分,大则是33.3分。功能本身没啥意义,但将打分函数修改后,就可以实现图片,视频,音频等数据的比对。
代码如下
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.plugins.ScriptPlugin;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.script.AbstractDoubleSearchScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.NativeScriptFactory;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class MyFirstPlugin extends Plugin implements ActionPlugin, ScriptPlugin {
    private final static Logger LOGGER = LogManager.getLogger(MyFirstPlugin.class);

    public MyFirstPlugin() {
        super();
        LOGGER.warn("Create the Basic Plugin and installed it into elasticsearch");
    }

    @Override
    public List<Class<? extends RestHandler>> getRestHandlers() {
        return Collections.singletonList(MyRestAction.class);
    }

    @Override
    public List<NativeScriptFactory> getNativeScripts() {
        return Collections.singletonList(new MyNativeScriptFactory());
    }

    public static class MyNativeScriptFactory implements NativeScriptFactory {
        private final static Logger LOGGER = LogManager.getLogger(MyNativeScriptFactory.class);

        @Override
        public ExecutableScript newScript(@Nullable Map<String, Object> params) {
            LOGGER.info("MyNativeScriptFactory  run new Script ");
            String featureStr = params == null ? null : XContentMapValues.nodeStringValue(params.get("feature"), null);
            if (featureStr == null) {
                LOGGER.error("Missing the field parameter ");
            }
            return new MyScript(featureStr);
        }

        @Override
        public boolean needsScores() {
            return false;
        }

        @Override
        public String getName() {
            return "my_native_script";
        }
    }

    public static class MyScript extends AbstractDoubleSearchScript {

        private final static Logger LOGGER = LogManager.getLogger(MyScript.class);
        private final String featureStr;

        public MyScript(String featureStr) {
            this.featureStr = featureStr;
        }

        @Override
        public double runAsDouble() {
            LOGGER.info("my run As begining ");
            String strSrcFeature = (String) source().get("feature");
            int nLen1 = featureStr.length();
            int nLen2 = strSrcFeature.length();
            if (nLen1 == nLen2) {
                return 99.9;
            }
            if (nLen1 < nLen2) {
                return 66.6;
            } else {
                return 33.3;
            }
        }
    }
}

是的。就这么一点点代码。简单吧。剩下的就是测试 新建索引文件
PUT my_index1
{
  "mappings": {
    "my_type": {
      "properties": {
         "feature": {
          "type":  "keyword",
           "index": "not_analyzed"
        },
        "tag": {
          "type":  "keyword"
        },
        "testname": {
          "type":  "text"
        }
      }
    }
  }
}
然后放点数据
PUT /my_index1/my_type/1?pretty
{
  "feature": "abc",
  "tag": "mytagabc",
  "testname": "Hello world"
}

PUT /my_index1/my_type/2?pretty
{
  "feature": "123456",
  "tag": "2mytagabc",
  "testname": "2Hello world"
}

PUT /my_index1/my_type/3?pretty
{
  "feature": "def789kkk",
  "tag": "3mytagabc",
  "testname": "3Hello world"
}

最后就是测试代码了
POST my_index1/my_type/_search
{
  "query": {
    "function_score": {
      "query": {
       "match_all" : { }

      },
      "functions": [
        {
          "script_score": {
            "script": {
                "inline": "my_native_script",
                "lang" : "native",
                "params": 
                {
                  "feature": "aaaaaa"
                }
            }
          }
        }
      ]
    }
  }
}

结果就是成功啦
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 99.9,
    "hits": [
      {
        "_index": "my_index1",
        "_type": "my_type",
        "_id": "2",
        "_score": 99.9,
        "_source": {
          "feature": "123456",
          "tag": "2mytagabc",
          "testname": "2Hello world"
        }
      },
      {
        "_index": "my_index1",
        "_type": "my_type",
        "_id": "3",
        "_score": 66.6,
        "_source": {
          "feature": "def789kkk",
          "tag": "3mytagabc",
          "testname": "3Hello world"
        }
      },
      {
        "_index": "my_index1",
        "_type": "my_type",
        "_id": "1",
        "_score": 33.3,
        "_source": {
          "feature": "abc",
          "tag": "mytagabc",
          "testname": "Hello world"
        }
      }
    ]
  }
}





www.htsjk.Com true http://www.htsjk.com/Elasticsearch/29127.html NewsArticle elasticsearch5.2.2 插件开发(三)ScriptPlugin 的实现, 本文地址  http://blog.csdn.net/makefriend7/article/details/60770564 这个插件实现的功能如下 定义一个“feature"的字段,而该字段的打分规则是由我...
相关文章
    暂无相关文章
评论暂时关闭