初学ElasticSearch,记录下代码
package com.lucas;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.IOException;
import java.net.InetAddress;
public class ElasticSearchJavaApi {
public static void main(String[] args) throws IOException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
// 查
GetResponse response = client.prepareGet("aaa", "vvv", "5").execute().actionGet();
System.out.println(response.getSourceAsString());
// 增
String json = "{" +
"\"id\":\"1\"," +
"\"title\":\"Java设计模式之装饰模式\"," +
"\"content\":\"在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。\"," +
"\"postdate\":\"2018-02-03 14:38:00\"," +
"\"url\":\"csdn.net/79239072\"" +
"}";
IndexResponse response1 = client.prepareIndex("first_api", "java_api", "1")
.setSource(json, XContentType.JSON).get();
System.out.println(response1.status());
// 改
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("first_api").type("java_api").id("1")
.doc(jsonBuilder.startObject().
field("url").value("https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html")
.endObject());
client.update(updateRequest).actionGet();
// 删
DeleteResponse deleteResponse = client
.prepareDelete("first_api", "java_api", "1")
.execute().actionGet();
System.out.println(deleteResponse.status());
client.close();
}
}
原文链接:https://blog.csdn.net/gavin5033/article/details/82775868
package com.lucas;
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.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ElasticSeearchUtil {
private static Settings settings = Settings.builder().put("cluster.name", "my-application").build();
private static TransportClient transportClient;
static {
try {
transportClient = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 新增 document
*
* @param indexName index
* @param indexType type
* @param data json data
* @return RestStatus
*/
public static RestStatus addDocument(String indexName, String indexType, Map<String, Object> data) {
IndexResponse actionGet = transportClient
.prepareIndex(indexName, indexType)
.setSource(data)
//.setId("1") //自己设置了id,也可以使用ES自带的,但是看文档说,ES的会因为删除id发生变动。
.execute().actionGet();
return actionGet.status();
}
/**
* 修改 document
*
* @param indexName index
* @param indexType type
* @param id id
* @param data 修改的字段map集合
*/
public static void update(String indexName, String indexType, String id, Map<String, Object> data) {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName)
.type(indexType)
.id(id)
.doc(data);
transportClient.update(updateRequest).actionGet();
//或者使用下面方式(效果一样)
/*XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName)
.type(indexType)
.id(id)
.doc(jsonBuilder
.startObject()
.field("title").value("XContentBuilder")
.endObject());
transportClient.update(updateRequest).actionGet();*/
}
/**
* 修改 document
*
* @param indexName index
* @param indexType type
* @param id id
* @param data 修改的字段map集合
*/
public static void update1(String indexName, String indexType, String id, Map<String, Object> data) {
/* UpdateRequestBuilder prepareUpdate = transportClient.prepareUpdate(indexName, indexType, id);
prepareUpdate
.setDoc(data)
//.setId("")
//.setScript("")
.get();*/
//或者
try {
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
UpdateRequestBuilder prepareUpdate1 = transportClient.prepareUpdate(indexName, indexType, id);
prepareUpdate1.setDoc(jsonBuilder.startObject()
.field("title").value("XContentBuilder")
.endObject())
.get();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void delDocument(String indexName, String indexType, String id) {
DeleteResponse deleteResponse = transportClient
.prepareDelete(indexName, indexType, id)
.execute().actionGet();
deleteResponse.status();
}
/**
* 批量操作
*
* @param indexName index
* @param indexType type
* @param data json data
* @return boolean
*/
public static boolean bulk(String indexName, String indexType, List<Map<String, Object>> data) {
BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
bulkRequestBuilder.add(transportClient.prepareIndex(indexName, indexType).setSource(data.get(0)));
bulkRequestBuilder.add(transportClient.prepareDelete(indexName, indexType, "id"));
bulkRequestBuilder.add(transportClient.prepareUpdate(indexName, indexType, "id").setDoc(data.get(1)));
BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
return bulkResponse.hasFailures();
}
/**
* get 操作
*
* @param indexName index
* @param indexType type
* @param id id
* @return map
*/
public static Map<String, Object> get(String indexName, String indexType, String id) {
GetResponse getResponse = transportClient
.prepareGet(indexName, indexType, id)
.execute().actionGet();
return getResponse.getSource();
}
/**
* MultiGet
*
* @param indexName index
* @param indexType type
* @param ids id
* @return list
*/
public static List<Map<String, Object>> multiGet(String indexName, String indexType, String... ids) {
List<Map<String, Object>> list = new ArrayList<>();
MultiGetResponse response = transportClient.prepareMultiGet()
.add(indexName, indexType, ids)
//另一个index、indextype和ids
.add("liu1", "liu1_type", "AV8-YHAr6f3B-qEudDab")
.execute().actionGet();
for (MultiGetItemResponse itemResponse : response) {
GetResponse getResponse = itemResponse.getResponse();
if (getResponse.isExists()) {
list.add(getResponse.getSource());
System.out.println(getResponse.getSourceAsString());
}
}
return list;
}
}