ElasticSearch Java API提供强大的搜索功能,以下主要介绍Java API方法中,针对数据,进行简单增删改查的使用实例。
1、获取ElasticSearch client对象public Client getEsClient(){
Client client = null;
try{
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true) //自动嗅探整个ES集群节点
.put("client", true)
.put("data",false)
.put("cluster.name","elasticsearch") //设置集群名字
.put("number_of_shards", 2) //2个主分片
.put("index.refresh_interval", "5s") //每5秒提交一次数据,类似oracle中的commit
.build();
client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("hostName", 9200));
}catch(Exception ex){
ex.printStackTrace();
}
return client;
}
2、查询List方法List> dataList = new ArrayList>();
Client client = null;
try{
//获取Elasticsearch client对象
client = getEsClient();
//设置查询条件(类似sql中 eventid = eventid )
QueryBuilder qb = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("eventid", eventid));
// 设置ES中 indexName 和 indexType,以及QueryBuilders 对象
SearchRequestBuilder requestBuilder = client.prepareSearch(EventBean.indexName)
.setTypes(EventBean.indexType)
.setQuery(qb);
//执行查询 (可以设置排序、分页)
SearchResponse actionGet = requestBuilder.addSort(SortBuilders.fieldSort("occurtime").order(SortOrder.DESC))
.setFrom((pageNum-1) * pageSize)
.setSize(pageNum * pageSize)
.execute().actionGet();
//遍历查询结果
if(actionGet != null){
SearchHits hits = actionGet.getHits();
if(hits != null && hits.getHits() != null){
Map hitMap = null;
for (SearchHit hit : hits.getHits()){
hitMap = hit.getSource();
if(hitMap == null || hitMap.size() <= 0){
continue;
}
dataList.add(hitMap );
}
}
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
//关闭client
}
return dataList ;
3、查询总数方法int rowTotal = 0;
Client client = null;
try{
//获取Elasticsearch client对象
client = getEsClient();
//设置查询条件 (类似sql中 title like '%searchtext%')
QueryBuilder qb = QueryBuilders.boolQuery().must(QueryBuilders.wildcardQuery("title", "*"+searchtext+"*"));
// 设置ES中 indexName 和 indexType,以及QueryBuilders 对象
CountRequestBuilder requestBuilder = client.prepareCount(EventBean.indexName)
.setTypes(EventBean.indexType)
.setQuery(qb);
// 查询动作
rowTotal = ConverUtils.Obj2int( requestBuilder.execute()
.actionGet().getCount(), 0);
}catch(Exception ex){
ex.printStackTrace();
}finally{
//关闭client
}
return rowTotal;
4、添加方法boolean optFlag = false;
Client client = null;
try{
//获取Elasticsearch client对象
client = getEsClient();
//构造请求对象
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
IndexRequestBuilder indexRequestBuilder = null;
//根据请求对象bodyList, 组织bulkRequestBuilder对象
String indexId = "";
Map bodyMap = null;
for (int count = 0; count
bodyMap = bodyList.get(count);
if (bodyMap == null) {
break;
}
//组织索引对象id
indexId = ConverUtils.Obj2Str(bodyMap.get("id"), "");
indexRequestBuilder = client.prepareIndex(indexName, indexType).setId(indexId).setSource(bodyMap);
bulkRequestBuilder.add(indexRequestBuilder);
}
//批量更新执行
BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
if (bulkResponse.hasFailures()) {
optFlag = false;
} else {
optFlag = true;
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
//关闭client
}
return optFlag ;
5、更新方法boolean optFlag = false;
Client client = null;
try{
//获取Elasticsearch client对象
client = getEsClient();
//构造请求对象
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
UpdateRequestBuilder updateRequestBuilder = null;
//根据请求对象cvdMap,组织索引对象
String indexId = getEsIndexId(client, ConverUtils.Obj2long(cvdMap.get("id"), 0L));
//获取索引(getEsIndexId)
updateRequestBuilder = client.prepareUpdate(indexName, indexType, indexId).setDoc(cvdMap);
bulkRequestBuilder.add(updateRequestBuilder);
//批量更新执行
BulkResponse bulkResponse = bulkRequestBuilder.setRefresh(true).execute().actionGet();
if (bulkResponse.hasFailures()) {
optFlag = false;
} else {
optFlag = true;
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
//关闭client
}
return optFlag ;
6、删除方法boolean optFlag = false;
Client client = null;
try{
//获取Elasticsearch client对象
client = getEsClient();
//根据id_array数组, 组织索引对象
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
DeleteRequestBuilder deleteRequestBuilder = null;
for(int i=0; i
if(id_array[i] == null || id_array[i].trim().equals("")){
continue;
}
//根据indexName, indexType, indexId进行删除(这里nfa_dictionary索引中ID字段值和ES自有_id值相同, 可以操作)
deleteRequestBuilder = client.prepareDelete(indexName, indexType, id_array[i]);
bulkRequestBuilder.add(deleteRequestBuilder.request());
}
//进行批量删除操作
BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
if (bulkResponse.hasFailures()) {
optFlag = false;
}else {
optFlag = true;
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
//关闭client
}
return optFlag ;