网上找了很久没找到2.4.X 想要的java api 工具
自己写了一个,分享一下
导入所需的jar
com.xinyi.search
elasticsearch
2.4.6
com.xinyi.search
lucene-core
5.5.4
com.xinyi.search
lucene-analyzers-common
5.5.4
joda-time
joda-time
2.9.4
com.google.guava
guava
18.0
com.twitter
jsr166e
1.1.0
com.tdunning
t-digest
3.0
com.xinyi.search
netty
3.10.6.Final
com.xinyi.search
hppc
0.7.1
com.xinyi.search
jackson-core
2.8.1
com.xinyi.search
jackson-dataformat-smile
2.8.1
com.xinyi.search
compress-lzf
1.0.2
2 java工具类
package com.xinyi.es.service.impl;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author: liang.he
* @Desc: ES-2.4.6相关测试
* @Date: Created in 15:58 2018/5/18
*/
@Service
public class EsServiceImpl {
@Value("${application.es.clusterName}")
private String clusterName;
@Value("${application.es.ip}")
private String esIp;
@Value("${application.es.port}")
private Integer esPort;
private TransportClient client;
@PostConstruct
public TransportClient getClient() {
if(client!=null){
return client;
}
Settings settings = Settings.settingsBuilder()
.put("cluster.name", clusterName).build();
try {
client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esIp), esPort));
} catch (Exception e) {
System.out.println("clusterName or ip or port is Error!");
e.printStackTrace();
}
return client;
}
public List> query(String indexName,String typeName,String filedName,String value,Integer pageIndex,Integer pageSize) {
QueryBuilder qb = QueryBuilders.matchPhraseQuery(filedName, value);
SearchResponse response = client.prepareSearch(indexName)
.setTypes(typeName).setQuery(qb).setFrom(pageIndex).setSize(pageSize).execute().actionGet();
List> result= new ArrayList>();
for (SearchHit hit : response.getHits().getHits()) {
result.add(hit.getSource());
}
return result;
}
public int insertEs(String indexName,String typeName,Map source) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex(indexName,typeName).setSource(source));
BulkResponse response = bulkRequest.get();
if(response.hasFailures()){
System.out.println("数据插入失败"+source);
return 0;
}
return 1;
}
public int deleteById(String indexName,String typeName,String id){
try {
DeleteRequestBuilder deleteRequest = client.prepareDelete(indexName,typeName,id);
DeleteResponse response = deleteRequest.execute().actionGet();
response.getId();
}catch (Exception e){
e.printStackTrace();
return 0;
}
return 1;
}
public void close(){
if (client!=null){
client.close();
}
}
}