Java API
ES对Java提供一套操作索引库的工具包,即Java API。所有的ES操作都使用Client对象执行。
1.导包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
2.连接ES获取Client对象
#链接ES服务器 两个端口PC Java程序通过9300链接ES, 9200是WEB链接端口
public static TransportClient getClient() throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
return client;
}
3.索引API
创建文档索引
/**
* 存值
* @throws Exception
*/
@Test
public void getCreated() throws Exception{
TransportClient client = getClient();
IndexRequestBuilder indexRequestBuilder = client.prepareIndex("crm", "user", "1");
Map<String,Object> mp = new HashMap();
mp.put("id",2);
mp.put("name","李 四");
mp.put("age",18);
//存值
IndexResponse indexResponse = indexRequestBuilder.setSource(mp).get();
System.out.println(indexResponse);
client.close();
}
或使用ES自动ID,不提供ID值1也OK。
返回的response对象可以获取到_index,_type,_version,_id等元数据。
获取文档
/**
* 获取存入的值
* @throws Exception
*/
@Test
public void getIndex() throws Exception{
TransportClient client = getClient();
GetResponse getFields = client.prepareGet("crm", "user", "1").get();
System.out.println(getFields.getSource());
client.close();
}
更新文档
/**
* 修改指定的值
* @throws Exception
*/
@Test
public void updateIndex() throws Exception{
TransportClient client = getClient();
Map<String,Object> map=new HashMap<String, Object>();
map.put("id",1);
map.put("name","张三");
map.put("age",19);
UpdateResponse updateResponse = client.prepareUpdate("crm", "user", "1")
.setDoc(map).get();
System.out.println(updateResponse);
client.close();
}
删除文档
/**
* 删除指定的值
* @throws Exception
*/
@Test
public void delete() throws Exception{
TransportClient client = getClient();
DeleteResponse deleteResponse = client.prepareDelete("crm", "user", "1").get();
System.out.println(deleteResponse);
client.close();
}
批量操作
/**
* 批量添加对象
*/
@Test
public void getbulk() throws Exception {
// 获取客户端的es对象
TransportClient client = getClient();
// 批量请求的对象
BulkRequestBuilder bulkRequest = client.prepareBulk();
for (int i = 0; i < 20; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", i);
map.put("name", "kd" + i);
map.put("age", 18 + i);
map.put("class", 1);
bulkRequest.add(client.prepareIndex("crm", "user", i + "")
.setSource(map));
}
// 提交请求
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
//处理错误
System.out.println("error");
}
// 关闭资源
client.close();
}
获取所有文档
/**
* 获取所有数据
* @throws Exception
*/
@Test
public void getAll() throws Exception{
TransportClient client = getClient();
SearchResponse crm = client.prepareSearch("crm").get();
//获取crm已存在数据的条数
System.out.println(crm.getHits().totalHits());
//循环遍历
SearchHit[] hits = crm.getHits().getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSource());
}
client.close();
}
搜索(重点)
//DSL查询
// 搜索
@Test
public void getsearch() throws Exception {
// 获取客户端的es对象
TransportClient client = getClient();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 匹配 class =1
List<QueryBuilder> must = boolQueryBuilder.must();
must.add(QueryBuilders.termQuery("class","1"));
// 搜索 1班的人 过滤 age 20-25 分页 0, 3 sort id desc
// 过滤
List<QueryBuilder> filter = boolQueryBuilder.filter();
filter.add(QueryBuilders.rangeQuery("age").gte(20).lte(25));
// 设置分页 排序 order by id desc
SearchResponse searchResponse = client.prepareSearch("crm")
.setFrom(0).setSize(3)
.setQuery(boolQueryBuilder)
.addSort("id", SortOrder.DESC).get();
System.out.println("总条数:"+searchResponse.getHits().getTotalHits());
SearchHit[] hits = searchResponse.getHits().getHits();
// 循环数据结构
for (SearchHit hit : hits) {
System.out.println(hit.getSource());
}
// 关闭资源
client.close();
}