优缺点
优点:
使用Transport 接口进行通信,能够使用ES集群中的一些特性,性能最好。
缺点:
JAR包版本需与ES集群版本一致,ES集群升级,客户端也跟着升级到相同版本。
ES 7.0 之后要逐步去掉。
使用准备
maven依赖
<!-- ES --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.4</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.4</version> </dependency><!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.0</version> </dependency>
config.proerties
#es
es_ip=x.x.10.183
es_port=9300
es_cluster_name=my-application
初始化
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
public enum EsClientEnum {
INSTANCE;
private TransportClient client;
private EsClientEnum(){
Prop prop = PropKit.use(new File("./conf/config.properties"),"gbk");
String es_cluster_name = prop.get("es_cluster_name");
String es_ip = prop.get("es_ip");
Integer es_port = prop.getInt("es_port");
System.out.println("es_cluster_name = " + es_cluster_name);
//设置集群名称
try {
Settings settings = Settings.builder()
.put("cluster.name", es_cluster_name)
.put("client.transport.sniff", false) //启动嗅探功能,自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
// .put("client.transport.ignore_cluster_name", true)//忽略集群名字验证, 打开后集群名字不对也能连接上
// .put("client.transport.nodes_sampler_interval", 5)//报错
// .put("client.transport.ping_timeout", 5) //报错, ping等待时间
.build();// 集群名
//创建client
client= new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(es_ip), es_port));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public TransportClient getClient() {
return client;
}
}
crud使用封装
import org.apache.log4j.Logger;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ElasticSearchUtil {
private static final Logger logger = Logger.getLogger(ElasticSearchUtil.class);
private static TransportClient client =EsClientEnum.INSTANCE.getClient();
/**
* 通过prepareIndex增加文档,参数为json字符串
* @param index 索引名
* @param type 类型
* @param _id 数据id
* @param json 数据
*/
@SuppressWarnings("deprecation")
public static void insertData(String index, String type, String _id, String json) {
IndexResponse indexResponse = client.prepareIndex(index, type).setId(_id)
.setSource(json)
.get();
System.out.println(indexResponse.getVersion());
logger.info("数据插入ES成功!");
}
/**
* 功能描述:更新数据
* @param index 索引名
* @param type 类型
* @param _id 数据id
* @param json 数据
*/
@SuppressWarnings("deprecation")
public static void updateData(String index, String type, String _id, String json){
try {
UpdateRequest updateRequest = new UpdateRequest(index, type, _id).doc(json);
// client.prepareUpdate(index, type, _id).setDoc(json).get();
client.update(updateRequest).get();
} catch (Exception e) {
logger.error("update data failed." + e.getMessage());
}
}
/**
* 功能描述:删除指定数据
* @param index 索引名
* @param type 类型
* @param _id 数据id
*/
public static void deleteData(String index, String type, String _id) {
try {
DeleteResponse response = client.prepareDelete(index, type, _id).get();
System.out.println(response.isFragment());
logger.info("删除指定数据成功!");
} catch (Exception e) {
logger.error("删除指定数据失败!" + e);
}
}
/**
* 功能描述:批量插入数据
* @param index 索引名
* @param type 类型
* @param jsonList 批量数据
*/
@SuppressWarnings("deprecation")
public void bulkInsertData(String

本文详细介绍了使用Elasticsearch的Java TransportClient进行通信的方法,包括初始化配置、CRUD操作以及查询优化。内容涵盖精确查询、模糊查询、范围查询、短语查询、布尔查询和分页查询等,还展示了如何处理查询结果和执行高亮显示。此外,文中还讨论了深度分页scroll和聚合查询(Agggregation),并提供了相关工具类的封装示例。
最低0.47元/天 解锁文章
4306

被折叠的 条评论
为什么被折叠?



