ElasticSearch简介:
ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,其第一个版本于2010年2月出现在GitHub上并迅速成为最受欢迎的项目之一。
ES也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
ElasticSearch新旧版本之间的语法以及API变化比较大,使用时请参考官方文档。
添加pom.xml依赖:
<!-- ElasticSearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
配置文件
#es
elasticsearch_ip = 127.0.0.1
elasticsearch_port = 9200
cluster_name = ecommerce
JFinal项目启动加载
public class DemoConfig extends JFinalConfig {
public void configConstant(Constants me) {}
public void configRoute(Routes me) {}
public void configEngine(Engine me) {}
public void configPlugin(Plugins me) {
//配置es
ElasticSearchPlugin searchPlugin = new ElasticSearchPlugin(p.get("elasticsearch_ip"), p.getInt("elasticsearch_port"), p.get("cluster_name"));
me.add(searchPlugin);
}
public void configInterceptor(Interceptors me) {}
public void configHandler(Handlers me) {}
}
自定义ElasticSearchPlugin
package com.flmall.api.common.plugin;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpHost;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.IPlugin;
public class ElasticSearchPlugin implements IPlugin {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchPlugin.class);
private String ip;
private int port;
private String clusterName;
private static RestClientBuilder builder;
private static RestClient restClient;
private static RestHighLevelClient restHighLevelClient;
private static RequestOptions options = RequestOptions.DEFAULT;
public ElasticSearchPlugin(String ip,int port,String clusterName){
this.ip = ip;
this.port = port;
this.clusterName = clusterName;
}
public static RestClient getClient() {
return restClient;
}
public static RestHighLevelClient getHighLevelClient(){
return restHighLevelClient;
}
public static RequestOptions getRequestOptions(){
options = RequestOptions.DEFAULT;
return options;
}
@Override
public boolean start() {
/**
* 这里的连接方式指的是没有安装x-pack插件,如果安装了x-pack则参考{@link ElasticsearchXPackClient}
* 1. java客户端的方式是以tcp协议在9300端口上进行通信
* 2. http客户端的方式是以http协议在9200端口上进行通信
*/
try {
builder = RestClient.builder(new HttpHost(this.ip, this.port));
//restClient = builder.build();
restHighLevelClient = new RestHighLevelClient(builder);
List<Node> nodes = restClient.getNodes();
if (nodes.isEmpty()) {
logger.info("No NODES Connected");
}else {
for (Node node : nodes){
logger.info("节点信息:"+node.getName()+node.getName()+node.getHost());
}
}
logger.info("ElasticsearchClient 连接成功,节点包括:"+ JSON.toJSON(restClient.getNodes()));
} catch (Exception e) {
logger.info(e.getMessage());
}
return true;
}
@Override
public boolean stop() {
if (restHighLevelClient != null){
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (restClient != null){
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}