ElasticSearch Java 增删改查
package com.earclouds.elasticsearch;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
public class ElasticSearch {
/*
* 创建索引
*/
public static void CreateIndex(Client client){
String json = "{" +
"\"user\":\"daiyutage\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
IndexResponse response = client.prepareIndex("twitter", "tweet","2")
.setSource(json)
.get();
// Index name
String _index = response.getIndex();
System.out.println("index:"+_index);
// Type name
String _type = response.getType();
System.out.println("_type:"+_type);
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println("_version:"+_version);
// isCreated() is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();
}
/*
* 根据索引获取信息
*/
public static void GetResponse(Client client){
GetResponse reponse = client.prepareGet("twitter","tweet","AVbFEOOdufxgYZWc3N1p").get();
String _source = reponse.getSourceAsString();
System.out.println(_source);
}
/*
* 删除index
*/
public static void DeleteResponse(Client client){
DeleteResponse reponse = client.prepareDelete("twitter","tweet","AVbFEOOdufxgYZWc3N1p").get();
System.out.println(reponse.isFound());
}
/*
* update
*/
private static void UpdateRequest(Client client) throws IOException, InterruptedException, ExecutionException {
//当更新的内容不存在的时候会添加
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("twitter");
updateRequest.type("tweet");
updateRequest.id("3");
XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();
updateRequest.doc(jsonBuilder
.startObject()
.field("message","我是林志颖啊")
.field("user","lin")
.endObject());
UpdateResponse response= client.update(updateRequest).get();
/*
* //当更新的内容不存在时候,不会添加
UpdateResponse response = client.prepareUpdate("twitter","tweet","1")
.setDoc(jsonBuilder()
.startObject()
.field("users4","xue")
.endObject())
.get();*/
System.out.println(response.getVersion());
}
/*
* 多行查询
*/
private static void MultiGet(Client client) {
MultiGetResponse response = client.prepareMultiGet()
.add("twitter","tweet","2","1")
.get();
for(MultiGetItemResponse itemResponse:response){
GetResponse getResponse = itemResponse.getResponse();
if(getResponse.isExists()){
System.out.println(getResponse.getSourceAsString());
}
}
}
/*
* BulkIndex
*/
private static void BulkIndex(Client client) throws IOException {
BulkRequestBuilder requestBuilder = client.prepareBulk();
requestBuilder.add(client.prepareIndex("twitter","tweet","4")
.setSource(jsonBuilder()
.startObject()
.field("user","niekaijie")
.field("school","beiyou")
.field("address","haidianqu")
.endObject())
);
requestBuilder.add(client.prepareIndex("twitter","tweet","3")
.setSource(jsonBuilder()
.startObject()
.field("user","林志颖aa")
.field("school","台湾大学")
.field("address","台湾")
.endObject())
);
BulkResponse response = requestBuilder.get();
}
/*
* search
*/
private static void SearchIndex(Client client) {
SearchResponse response = client.prepareSearch("twitter")
.setTypes("tweet")
.setQuery(QueryBuilders.termQuery("user", "lin"))
.execute()
.actionGet();
SearchHits hits = response.getHits();
for(SearchHit hit: hits.getHits()){
System.out.println(hit.getSource().get("message"));
}
}
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
String str = "115.29.109.166";
String[] ipStr = str.split("\\.");
byte[] ipBuf = new byte[4];
for(int i = 0; i < 4; i++){
ipBuf[i] = (byte)(Integer.parseInt(ipStr[i])&0xff);
}
Client client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByAddress(ipBuf),9300));
// CreateIndex(client);
// GetResponse(client);
// DeleteResponse(client);;
// UpdateRequest(client);
//client.close();
// MultiGet(client);
// BulkIndex(client);
SearchIndex(client);
}
}