最近在学习elasticsearch,记录下这个过程(该版本是版本6.2.4)。(安装过程不记录)
一、 首先是学习的相应的restful API 我是用kibana进行操作,也可以使用其他工具(比如postman啥的)
添加记录:(添加一个索引itouchtv,类型news,id 为1)
PUT itouchtv/news/1
{
"title":"learning elasticsearch",
"content":"cjh always learn es online",
"type":"news"
}
查找记录:(有很多的查询,遇到再记录)
GET itouchtv/news/1
修改记录:
1 部分修改(这里仅仅是修改了type字段,其他的信息会保留)
POST itouchtv/news/1/_update
{
"doc": {
"type":"video"
}
}
2 全量替换(这里修改了title,同时其他信息没有保留,其实这里如果存在某条记录,它会删除掉然后再从新创建)
PUT itouchtv/news/1
{
"title":"learning rabbitmq"
}
删除记录:会删除指定的记录
DELETE itouchtv/news/1
二、Java API
我使用maven进行管理:
pom.xml文件导入依赖:
org.elasticsearch.client
transport
6.2.4
org.apache.logging.log4j
log4j-api
2.7
org.apache.logging.log4j
log4j-core
2.7
然后获取client客户端:跟jdbc的学习是一样的,先获取连接。
public class ClientUtils {
public static TransportClient getClient() throws UnknownHostException {
//客户端的设置,集群的名称为 elasticsearch
Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(
new TransportAddress(InetAddress.getByName("localhost"),9300));
return client;
}
}
测试连接是否成功:
public class Test {
public static void main(String[] args) throws UnknownHostException {
TransportClient client = ClientUtils.getClient();
System.out.println(client);
client.close();
}
}
运行结果:org.elasticsearch.transport.client.PreBuiltTransportClient@3005db4a 表示连接成功。
添加记录:
在ClientUtils中加入两个方法:
public static void addRecordJsonBuilder(String index,String type,String id) throws IOException {
TransportClient client = getClient();
IndexResponse indexResponse = client.prepareIndex(index, type, id).setSource(XContentFactory.jsonBuilder()
.startObject()
.field("title", "cjh always learn es online")
.field("content","it is too difficult to learn")
.field("type","news")
.endObject()).get();
System.out.println(indexResponse);
}
//使用map进行构造
public static void addRecordMap(String index,String type,String id) throws UnknownHostException {
TransportClient client = getClient();
Map map = new HashMap();
map.put("title", "cjh always learn rabbitmq online");
map.put("content", "rabbitmq is good");
map.put("type", "news");
IndexResponse indexResponse = client.prepareIndex(index, type, id).setSource(map).get();
System.out.println(indexResponse);
}
执行以下函数:
public class Test1 {
public static void main(String[] args) throws IOException {
ClientUtils.addRecordJsonBuilder("itouchtv", "news", "1");
ClientUtils.addRecordMap("itouchtv", "news", "2");
}
}
结果如下:表示添加成功。
IndexResponse[index=itouchtv,type=news,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
IndexResponse[index=itouchtv,type=news,id=2,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
获取记录:在工具类中加入这个方法。(其实那个client可以作为一个成员变量,这样就不用每次都获取,可以直接用,我这就不改了)
public static void getRecordById(String index,String type,String id) throws UnknownHostException {
TransportClient client = getClient();
GetResponse getResponse = client.prepareGet(index,type,id).get();
System.out.println(getResponse);
}
执行方法:
ClientUtils.getRecordById("itouchtv", "news", "1");
结果:
{"_index":"itouchtv","_type":"news","_id":"1","_version":1,"found":true,"_source":{"title":"cjh always learn es online","content":"it is too difficult to learn","type":"news"}}
修改记录:
在工具类中加入:
public static void updateRecord(String index,String type,String id) throws IOException, InterruptedException, ExecutionException {
TransportClient client = getClient();
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index);
updateRequest.type(type);
updateRequest.id(id);
updateRequest.doc(
XContentFactory.jsonBuilder()
.startObject()
.field("content","update")
.endObject());
UpdateResponse updateResponse = client.update(updateRequest).get();
System.out.println(updateResponse);
}
然后调用:
ClientUtils.updateRecord("itouchtv", "news", "1");
执行结果:
UpdateResponse[index=itouchtv,type=news,id=1,version=2,seqNo=1,primaryTerm=2,result=updated,shards=ShardInfo{total=2, successful=1, failures=[]}]
删除记录:
在工具类中加入:
public static void deleteRecord(String index,String type,String id) throws UnknownHostException {
TransportClient client = getClient();
DeleteResponse deleteResponse = client.prepareDelete(index,type,id).get();
System.out.println(deleteResponse);
}
然后执行:
ClientUtils.deleteRecord("itouchtv", "news", "1");
执行结果:
DeleteResponse[index=itouchtv,type=news,id=1,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]
总结:以上的最基本的一些操作,代码可能不太好,但是这里是以操作为主,就不考虑这个问题了。