es 客户端
pom
---------------------------------------------
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.4.3</version>
</dependency>
---------------------------------------------
java接口
1.连接
public void initClint(String clustername, String host, int port) {
Settings settings = ImmutableSettings.settingsBuilder().put(
"cluster.name", clustername).build();
client = new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(host, port));
}
2, 插入(在存在时会全部覆盖)
public static int insertData(String indexname, String type, String id,
String json) {
try {
IndexResponse response = client.prepareIndex(indexname, type, id)
.setSource(json).setRefresh(false).setCreate(false)
.execute().actionGet();
// XContentFactory
// .jsonBuilder()
// .startObject()
// .field("body", "kimchy")
// .field("date", "20150302")
// .field("count", 1)
// .field("url", " http://asdas.com")
// .field("title", "trying out Elastic Search")
// .endObject()).execute().actionGet();
if (response.getHeaders().isEmpty()) {
return (int) response.getVersion();
} else {
logger.info("error IndexResponse=" + response.getVersion());
return 0;
}
} catch (Exception e) {
logger.error("insertData error", e);
return 0;
}
}
String json) {
try {
IndexResponse response = client.prepareIndex(indexname, type, id)
.setSource(json).setRefresh(false).setCreate(false)
.execute().actionGet();
// XContentFactory
// .jsonBuilder()
// .startObject()
// .field("body", "kimchy")
// .field("date", "20150302")
// .field("count", 1)
// .field("url", " http://asdas.com")
// .field("title", "trying out Elastic Search")
// .endObject()).execute().actionGet();
if (response.getHeaders().isEmpty()) {
return (int) response.getVersion();
} else {
logger.info("error IndexResponse=" + response.getVersion());
return 0;
}
} catch (Exception e) {
logger.error("insertData error", e);
return 0;
}
}
3.创建索引
public static Boolean creatIndex(String index, String type) {
Boolean isok = true;
try {
Boolean isexists = false;
if (client.admin().indices().prepareExists(index).get().isExists()) {
GetMappingsResponse res = client.admin().indices().getMappings(
new GetMappingsRequest().indices(index)).get();
ImmutableOpenMap<String, MappingMetaData> mapping = res
.mappings().get(index);
if (mapping.containsKey(type)) {
isexists = true;
}
}
if (!isexists) {
CreateIndexResponse response = client.admin().indices()
.prepareCreate(index).setSettings(
ImmutableSettings.settingsBuilder().put(
"number_of_shards", 5)).addMapping(
type,
XContentFactory.jsonBuilder().startObject()
.startObject(type).startObject(
"properties").startObject(
"title")
.field("type", "string").endObject()
.startObject("body").field("type",
"string").endObject()
.startObject("releasedate").field(
"type", "string").endObject()
.startObject("url").field("type",
"string").endObject()
.startObject("count").field("type",
"long").endObject().endObject()
.endObject().endObject()).execute()
.actionGet();
if (response.getHeaders().isEmpty()) {
return true;
} else {
logger.info("CreateIndexResponse error="
+ response.getHeaders());
}
}
} catch (Exception e) {
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
} else if (ExceptionsHelper.unwrapCause(e) instanceof ClusterBlockException) {
} else {
logger.error("failed to create index [{}], disabling river...",
e, index);
isok = false;
}
}
return isok;
}
4,更新(不存在时插入,存在时执行更新脚本)
public static int update(String index, String type, String id,String json) {
UpdateRequest updateRequest = new UpdateRequest(index, type, id).upsert(json)
.script("if(ctx._source.count >= 1) ctx._source.count += 1 ")
.refresh(true)
.retryOnConflict(5);
int rint = 1;
try {
UpdateResponse rs = client.update(updateRequest).get();
if (rs.getHeaders().isEmpty()) {
if(rs.isCreated())
{
rint = 1;
}
rint = 2;
} else {
logger.error("update rs error" + rs.getHeaders());
rint = 0;
}
} catch (InterruptedException e) {
logger.error("update error", e);
rint = 0;
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rint;
}
5, 删除一个索引
public Boolean deleteIndex(String indexname) {
try {
DeleteIndexResponse response = client.admin().indices()
.prepareDelete(indexname).execute().actionGet();
if (response.getHeaders().isEmpty()) {
return true;
} else {
return false;
}
} catch (Exception e) {
logger.error("deleteIndex error", e);
return false;
}
}
6, 删除一个索引下的类型
public Boolean deleteType(String indexname,String typename)
{
Boolean isok = false;
try{
DeleteMappingRequest deleteMapping=new DeleteMappingRequest(indexname).types(typename);
DeleteMappingResponse res = client.admin().indices().deleteMapping(deleteMapping).actionGet();
if(res.getHeaders().isEmpty())
{
isok = true;
}else{
isok =false;
}
}catch(Exception e)
{
logger.error("",e);
}
logger.info("delete index="+indexname+" type="+typename+" isok="+isok);
return isok;
}