Elasticsearch 5.1.1 Embedded Mode

来源:互联网 发布:哥白尼式革命 知乎 编辑:程序博客网 时间:2024/06/07 22:29

Elasticsearch 5.1.1 与2.X的Embedded Mode有了较大不同。搭梯子查了一下,在此做个记录:

Node节点要做一下扩充

public class EmbedNode extends org.elasticsearch.node.Node{    public EmbedNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins) {        super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null), classpathPlugins);    }}

建立服务类,提供查询Client,用到了Netty4,请导入相应的包

public class EmbedSearchServer{    private Node node;    private EmbedSearchServer(){};    public EmbedSearchServer(String dataPath){        node = new EmbedNode(            Settings.builder()            .put("transport.type", "netty4")            .put("http.type", "netty4")            .put("http.enabled", "true")             .put("path.home", "classpath")             .put("path.data", dataPath)            .build(),             Arrays.asList(Netty4Plugin.class)        );    }    public void start() throws NodeValidationException{        node.start();    }    public void stop() throws IOException{        node.close();    }    public Client getClient() {        return node.client();    }}

一个简单使用例子:

new EmbedSearchServer(/*your data path*/).getClient().search(/*SearchRequest*/)
0 0