ElasticSearch(名称太长,后面简称ES)作为一个搜索引擎,目前可谓是如日中天,几乎和solr齐驾并驱。关于他能做什么,跟云计算有什么关系,在此不再描述。但是ES的官方文档,特别是关于java的客户端文档,真是少的可怜,甚至连个完整的增删改的示例都没有。在此,我就献丑了。
在开始讲解之前,还是先做个铺垫,为了能够有一个可以索引的模型,我们自定义了一个模型,暂时起个名称叫LogModel吧,这个模型有各种数据类型,int,long,String,list,但千万不要认为这是跟记录日志有关的一个模型。作为索引的一个最简单模型。代码如下:
-
import java.util.ArrayList;
-
import java.util.List;
-
import java.util.Random;
-
import java.util.UUID;
-
/**
-
* 瞎编的一个模型,跟日志基本没有关系
-
* @author donlian
-
*/
-
public class LogModel {
-
//主ID
-
private long id;
-
//次ID
-
private int subId;
-
/**
-
* 系统名称
-
*/
-
private String systemName;
-
private String host;
-
-
//日志描述
-
private String desc;
-
private List<Integer> catIds;
-
public LogModel(){
-
Random random = new Random();
-
this.id = Math.abs(random.nextLong());
-
int subId = Math.abs(random.nextInt());
-
this.subId = subId;
-
List<Integer> list = new ArrayList<Integer>(5);
-
for(int i=0;i<5;i++){
-
list.add(Math.abs(random.nextInt()));
-
}
-
this.catIds = list;
-
this.systemName = subId%1 == 0?"oa":"cms";
-
this.host = subId%1 == 0?"10.0.0.1":"10.2.0.1";
-
this.desc = "中文" + UUID.randomUUID().toString();
-
}
-
public LogModel(long id,int subId,String sysName,String host,String desc,List<Integer> catIds){
-
this.id = id;
-
this.subId = subId;
-
this.systemName = sysName;
-
this.host = host;
-
this.desc = desc;
-
this.catIds = catIds;
-
}
-
...//省去get,set方法
-
}
同时,因为ES在索引的时候,一般都用json格式,因此,使用jackson定义了一个将对象转化成json的工具类,也很简单,代码:
-
public class ESUtils {
-
private static ObjectMapper objectMapper = new ObjectMapper();
-
public static String toJson(Object o){
-
try {
-
return objectMapper.writeValueAsString(o);
-
} catch (JsonProcessingException e) {
-
e.printStackTrace();
-
}
-
return "";
-
}
-
}
在开始进行操作ES服务器之前,我们必须得获得ES的API,简单介绍一下ES操作服务器的两种方式,一种是使用Node方式,即本机也启动一个ES,然后跟服务器的ES进行通信,这个node甚至还能存储(奇怪,一般需要这样的方式吗?),另一种,就是下面我介绍的这一种,通过一个对象使用http协议跟服务器进行交互。
获得一个ES客户端API的代码如下:
-
Settings settings = ImmutableSettings.settingsBuilder()
-
//指定集群名称
-
.put("cluster.name", "elasticsearch")
-
//探测集群中机器状态
-
.put("client.transport.sniff", true).build();
-
/*
-
* 创建客户端,所有的操作都由客户端开始,这个就好像是JDBC的Connection对象
-
* 用完记得要关闭
-
*/
-
Client client = new TransportClient(settings)
-
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
Client对象,可以理解为数据库的Connection对象。好了,准备工作完成,下面就开始增删改查。
Index(增加)
ES里面的增加对象不叫什么add,save等,叫index。但无论叫什么名称,反正就是向ES服务器里面加数据。上面说过一个对象转json的工具类,其实ES的API中,是自带构建json的工具类的。
-
import org.elasticsearch.action.index.IndexResponse;
-
import org.elasticsearch.client.Client;
-
import org.elasticsearch.client.transport.TransportClient;
-
import org.elasticsearch.common.settings.ImmutableSettings;
-
import org.elasticsearch.common.settings.Settings;
-
import org.elasticsearch.common.transport.InetSocketTransportAddress;
-
-
import com.donlianli.es.ESUtils;
-
import com.donlianli.es.model.LogModel;
-
/**
-
* 向ES添加索引对象
-
* @author donlian
-
*/
-
public class IndexTest {
-
public static void main(String[] argv){
-
Settings settings = ImmutableSettings.settingsBuilder()
-
//指定集群名称
-
.put("cluster.name", "elasticsearch")
-
//探测集群中机器状态
-
.put("client.transport.sniff", true).build();
-
/*
-
* 创建客户端,所有的操作都由客户端开始,这个就好像是JDBC的Connection对象
-
* 用完记得要关闭
-
*/
-
Client client = new TransportClient(settings)
-
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
-
String json = ESUtils.toJson(new LogModel());
-
//在这里创建我们要索引的对象
-
IndexResponse response = client.prepareIndex("twitter", "tweet")
-
//必须为对象单独指定ID
-
.setId("1")
-
.setSource(json)
-
.execute()
-
.actionGet();
-
//多次index这个版本号会变
-
System.out.println("response.version():"+response.version());
-
client.close();
-
}
-
}
运行这个代码,就向ES插入了一条数据,你运行两遍,还是一条。ES根据你设置的ID来设置对象,如果没有则插入,有则更新。每更新一次,对应的version加1.
好了,在次,使用以下命令,应该能够查询到一条记录了。
curl -XGET 'http://localhost:9200/twitter/tweet/1'
delete(删除)
有了增加的例子,删除的例子也就好写了。增加是prepareIndex,删除是prepareDelete,查询就是PrepareGet。
代码如下:
-
import org.elasticsearch.action.delete.DeleteResponse;
-
import org.elasticsearch.client.Client;
-
import org.elasticsearch.client.transport.TransportClient;
-
import org.elasticsearch.common.settings.ImmutableSettings;
-
import org.elasticsearch.common.settings.Settings;
-
import org.elasticsearch.common.transport.InetSocketTransportAddress;
-
-
import com.donlianli.es.ESUtils;
-
-
public class DeleteTest {
-
public static void main(String[] argv){
-
Settings settings = ImmutableSettings.settingsBuilder()
-
//指定集群名称
-
.put("cluster.name", "elasticsearch")
-
//探测集群中机器状态
-
.put("client.transport.sniff", true).build();
-
/*
-
* 创建客户端,所有的操作都由客户端开始,这个就好像是JDBC的Connection对象
-
* 用完记得要关闭
-
*/
-
Client client = new TransportClient(settings)
-
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
-
//在这里创建我们要索引的对象
-
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
-
.execute().actionGet();
-
System.out.println(response.getId());
-
System.out.println(ESUtils.toJson(response.getHeaders()));
-
}
-
}
GET(查询)
<div class="dp-highlighter" style="font-family: monaco, 'dejavu sans mono', 'bitstream vera sans mono', consolas, 'courier new', monospace; font-size: 12px; width: 679px; overflow: auto; word-break: break-all; word-wrap: break-word; line-height: 25px; padding: 1px; margin-left: 9px;"><div class="bar"><div class="tools" style="font-weight: bold; padding: 3px; margin: 0px;">Java代码</div></div><ol start="1" class="dp-j" style="margin: 0px 0px 1px; padding: 2px 0px; font-size: 1em; line-height: 1.4em; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.action.get.GetResponse; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.client.Client; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.client.transport.TransportClient; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.common.settings.ImmutableSettings; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.common.settings.Settings; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> org.elasticsearch.common.transport.InetSocketTransportAddress; </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> GetTest { </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">static</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span> main(String[] argv){ </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> Settings settings = ImmutableSettings.settingsBuilder() </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> <span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;">//指定集群名称</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> .put(<span class="string" style="color: blue;">"cluster.name"</span>, <span class="string" style="color: blue;">"elasticsearch"</span>) </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> <span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;">//探测集群中机器状态</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> .put(<span class="string" style="color: blue;">"client.transport.sniff"</span>, <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">true</span>).build(); </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> <span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;">/*</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;"> * 创建客户端,所有的操作都由客户端开始,这个就好像是JDBC的Connection对象</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;"> * 用完记得要关闭</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;"> */</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> Client client = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span> TransportClient(settings) </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> .addTransportAddress(<span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span> InetSocketTransportAddress(<span class="string" style="color: blue;">"192.168.1.106"</span>, <span class="number" style="color: rgb(192, 0, 0);">9300</span>)); </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> <span class="comment" style="color: rgb(0, 130, 0); width: auto; border-width: 0px; padding: 0px; margin: 0px;">//在这里创建我们要索引的对象</span> </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> GetResponse response = client.prepareGet(<span class="string" style="color: blue;">"twitter"</span>, <span class="string" style="color: blue;">"tweet"</span>, <span class="string" style="color: blue;">"1"</span>) </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> .execute().actionGet(); </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> System.out.println(<span class="string" style="color: blue;">"response.getId():"</span>+response.getId()); </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> System.out.println(<span class="string" style="color: blue;">"response.getSourceAsString():"</span>+response.getSourceAsString()); </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;"> } </span></p></li><li style="margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; list-style: none outside none; font-size: 1em; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="color: black;">} </span></p></li></ol></div>
好了,增删改查的代码写完。至于搜索,那是一个比较深入的话题,我也在慢慢探索。我时间我会继续写下去。