pox.xml文件添加以下内容

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>

新建ESHighLevelRestUtil.java

package com;

import java.util.HashMap;
import java.util.Map; import org.apache.http.HttpHost;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus; public class ESHighLevelRestUtil { static RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("172.19.12.249", 9200, "http"))); /**
* 验证索引是否存在
*
* @param index
* 索引名称
* @return
* @throws Exception
*/
public boolean indexExists(String index) throws Exception {
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
request.local(false);
request.humanReadable(true); boolean exists = client.indices().exists(request);
return exists;
} /**
*
* @param index
* @param indexType
* @param properties
* 结构: {name:{type:text}} {age:{type:integer}}
* @return
* @throws Exception
*/
public boolean indexCreate(String index, String indexType,
Map<String, Object> properties) throws Exception { if (indexExists(index)) {
return true;
}
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder().put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)); Map<String, Object> jsonMap = new HashMap<>();
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
jsonMap.put(indexType, mapping);
request.mapping(indexType, jsonMap); CreateIndexResponse createIndexResponse = client.indices().create(
request);
boolean acknowledged = createIndexResponse.isAcknowledged();
return acknowledged;
} /**
* 删除索引
*
* @param index
* @return
* @throws Exception
*/
public boolean indexDelete(String index) throws Exception {
try {
DeleteIndexRequest request = new DeleteIndexRequest(index);
DeleteIndexResponse deleteIndexResponse = client.indices().delete(
request);
return deleteIndexResponse.isAcknowledged();
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) {
return true;
} else {
return false;
}
}
} /**
* 创建更新文档
*
* @param index
* @param indexType
* @param documentId
* @param josonStr
* @return
* @throws Exception
*/
public boolean documentCreate(String index, String indexType,
String documentId, String josonStr) throws Exception {
IndexRequest request = new IndexRequest(index, indexType, documentId); request.source(josonStr, XContentType.JSON);
IndexResponse indexResponse = client.index(request); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED
|| indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
return true;
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
return true;
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo
.getFailures()) {
throw new Exception(failure.reason());
}
}
return false;
} /**
* 创建更新索引
*
* @param index
* @param indexType
* @param documentId
* @param map
* @return
* @throws Exception
*/
public boolean documentCreate(String index, String indexType,
String documentId, Map<String, Object> map) throws Exception {
IndexRequest request = new IndexRequest(index, indexType, documentId); request.source(map);
IndexResponse indexResponse = client.index(request); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED
|| indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
return true;
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
return true;
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo
.getFailures()) {
throw new Exception(failure.reason());
}
}
return false;
} /**
* 创建索引
*
* @param index
* @param indexType
* @param josonStr
* @return
* @throws Exception
*/
public String documentCreate(String index, String indexType, String josonStr)
throws Exception {
IndexRequest request = new IndexRequest(index, indexType); request.source(josonStr, XContentType.JSON);
IndexResponse indexResponse = client.index(request); String id = indexResponse.getId();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED
|| indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
return id;
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
return id;
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo
.getFailures()) {
throw new Exception(failure.reason());
}
}
return null;
} /**
* 创建索引
*
* @param index
* @param indexType
* @param map
* @return
* @throws Exception
*/
public String documentCreate(String index, String indexType,
Map<String, Object> map) throws Exception {
IndexRequest request = new IndexRequest(index, indexType); request.source(map);
IndexResponse indexResponse = client.index(request); String id = indexResponse.getId();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED
|| indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
return id;
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
return id;
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo
.getFailures()) {
throw new Exception(failure.reason());
}
}
return null;
} public boolean documentDelete(String index, String indexType,
String documentId) throws Exception {
DeleteRequest request = new DeleteRequest(index, indexType, documentId);
DeleteResponse deleteResponse = client.delete(request);
if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
return true;
}
ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
return true;
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo
.getFailures()) {
throw new Exception(failure.reason());
}
}
return false;
} }

新建ESHighLevelRestTest.java

package com;

import java.util.HashMap;
import java.util.Map; import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient; public class ESHighLevelRestTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ESHighLevelRestUtil util = new ESHighLevelRestUtil(); System.out.println(util.indexExists("indextest001")); Map<String, Object> map = new HashMap<String, Object>();
map.put("name", new HashMap() {
{
put("type", "text");
}
});
map.put("age", new HashMap() {
{
put("type", "double");
}
});
map.put("sex", new HashMap() {
{
put("type", "double");
}
});
map.put("address", new HashMap() {
{
put("type", "text");
}
}); // 创建主题
util.indexCreate("indextest005", "sx", map); //创建文档1
System.out.println(util.documentCreate("indextest005", "sx",
new HashMap<String,Object>() {
{
put("name", "名称1");
put("age", 18);
put("sex", 10);
put("address", "地址1");
}
})); //创建更新文档2
System.out.println(util.documentCreate("indextest005", "sx", "1",
new HashMap<String,Object>() {
{
put("name", "名称2");
put("age", 18);
put("sex", 10);
put("address", "地址2");
}
})); //删除文档
System.out.println(util.documentDelete("indextest005", "sx", "1"));
}
}

参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-overview.html

相互学习,如有不正确的地方欢迎指正

最新文章

  1. Android raw to bmp
  2. 自己动手写计算器v1.1
  3. Linux学习之一--VI编辑器的基本使用
  4. LCS
  5. MyBatis学习总结(八)&mdash;&mdash;Mybatis3.x与Spring4.x整合
  6. Android项目实战(二十二):启动另一个APP or 重启本APP
  7. WPF学习之路(一) 初识WPF
  8. Android GUI系统
  9. 百练_2945 拦截导弹(DP)
  10. Peaceful Commission
  11. HDU3389 Game
  12. vue中路由跳转的底层原理
  13. FJOI2019 划水记
  14. aaronyang的百度地图API之LBS云与.NET开发 Javascript API 2.0【把数据存到LBS云2/2】
  15. 【Android】amr播放
  16. (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)
  17. 20162314 《Program Design &amp; Data Structures》Learning Summary Of The Fifth Week
  18. php后台“爬虫”模拟登录第三方系统
  19. Web设计的速查卡(转)
  20. ETL 工具下载全集 包括 Informatica Datastage Cognos( 持续更新)

热门文章

  1. 初学Java 求最大公约数
  2. prometheus 笔记
  3. 记录每个action执行时间
  4. Ehcahe独立使用
  5. jsonp跨域获取数据实现百度搜索
  6. 真的,移动端尺寸自适应与dpr无关
  7. 2025年VR虚拟现实技术将渗透人们日常生活
  8. 基于dokcer的zoookeeper集群安装
  9. postman-Runner
  10. operator函数操作符