ElasticSearch(二)
下面说一下es的API 此前针对5.6.3版本
MAVEN依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.3</version>
</dependency>
1.连接集群:
//设置集群的属性
Settings settings = Settings.builder()
.put("cluster.name","hr_es").build();
//创建集群客户端
TransportClient client = new PreBuiltTransportClient(settings);
//连接集群的各个节点
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.80.11",9300)));
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.80.12",9300)));
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.80.13",9300)));
//测试是否连接成功
List<DiscoveryNode> discoveryNodes = client.listedNodes();
for (DiscoveryNode discoveryNode : discoveryNodes) {
System.out.println(discoveryNode.getAddress());
}
}
2.创建索引库
//设置索引库属性
Settings settings = Settings.builder()
.put("number_of_shards",5)//指定分片数
.put("number_of_replicas",1)//指定副本数
.build();
//创建索引库
CreateIndexResponse response = client.admin().indices().prepareCreate("myesindex")
.setSettings(settings).get();
//测验是否成功
System.out.println(response.isAcknowledged());
3.修改测试库副本数
//设置属性
Settings settings = Settings.builder()
.put("number_of_replicas",2)//指定副本数
.build();
//执行更改
UpdateSettingsResponse response = client.admin().indices().prepareUpdateSettings("myesindex")
.setSettings(settings).get();
//测试是否成功
System.out.println(response.isAcknowledged());
4.获取集群和索引库信息
//获得集群客户端
ClusterAdminClient cluster = client.admin().cluster();
ClusterHealthResponse healths =cluster.prepareHealth().get();
//获取集群名字
String clusterName = healths.getClusterName();
//获取节点数量
int numberOfNodes = healths.getNumberOfNodes();
// 获取数据节点数量
int numberOfDataNodes = healths.getNumberOfDataNodes();
//获取所有的索引库
Collection<ClusterIndexHealth> values = healths.getIndices().values();
for (ClusterIndexHealth value : values) {
String index = value.getIndex();
//分片数
int numberOfReplicas = value.getNumberOfReplicas();
//副本数
int numberOfShards = value.getNumberOfShards();
//索引库状态
ClusterHealthStatus status = value.getStatus();
}
增删改查
1.index
/**
* 添加索引
*/
private String index = "myesindex";//全局变量 索引库名称
private String type = "emp";//全局变量 类型名称
@Test
public void index() throws Exception{
//以下三种 取一即可 作为serSource()参数
//1.以XcontentBuilder形式
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.field("name", "何睿")
.field("age", 24)
.field("sex", "男")
.endObject();
//2.Map形式
Map map = new HashMap<String,Object>();
map.put("name","何睿");map.put("age",24);map.put("sex","男");
//3.Json形式 setSource需要参数XContentType.JSON
// 4.javabean 形式 -->转换成json
IndexResponse response = client.prepareIndex(index, type, "1")
.setSource(builder).get();
//测试是否成功
System.out.println(response.getId());
}
2.get
GetResponse response = client.prepareGet(index, type, "1").get();
System.out.println(response.getSourceAsString());
mget
MultiGetResponse responses = client.prepareMultiGet().add(index, type, "1", "2", "3").get();
for (MultiGetItemResponse respons : responses) {
GetResponse response = respons.getResponse();
System.out.println(respons);
}
delete
DeleteResponse response = client.prepareDelete(index, type, "1").get();
System.out.println(response.getId());
deletebyquery
//termQuery 不会分词 matchQuery会分词
BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.source(index).filter(QueryBuilders.matchQuery("sex", "女人"))
.get();
System.out.println(response.getDeleted());
update1
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.field("name", "何睿").field("age", 24)
.field("sex", "男人").endObject();
UpdateResponse response = client.prepareUpdate(index, type, "2")
.setDoc(builder).get();
System.out.println(response.getId());
update2
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.field("name", "何睿").field("age", 24)
.field("sex", "男人啊").endObject();
UpdateRequest updateRequest = new UpdateRequest(index,type,"2");
updateRequest.doc(builder);
UpdateResponse response = client.update(updateRequest).get();
System.out.println(response.getId());
upsert
XContentBuilder xContentBuilder1 = XContentFactory.jsonBuilder()
.startObject().field("name","帅帅啊").endObject();
XContentBuilder xContentBuilder2 = XContentFactory.jsonBuilder()
.startObject().field("name","帅不帅").endObject();
UpdateResponse response = client.prepareUpdate(index, type, "1").setDoc(xContentBuilder1)
.setUpsert(xContentBuilder2).get();
System.out.println(response.getId());