ElasticSearch(名称太长,后面简称ES)作为一个搜索引擎,目前可谓是如日中天,几乎和solr齐驾并驱。关于他能做什么,跟云计算有什么关系,在此不再描述。但是ES的官方文档,特别是关于java的客户端文档,真是少的可怜,甚至连个完整的增删改的示例都没有。在此,我就献丑了。
在开始讲解之前,还是先做个铺垫,为了能够有一个可以索引的模型,我们自定义了一个模型,暂时起个名称叫LogModel吧,这个模型有各种数据类型,int,long,String,list,但千万不要认为这是跟记录日志有关的一个模型。作为索引的一个最简单模型。代码如下:
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
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 catIds;
public LogModel(){
Random random = new Random();
this.id = Math.abs(random.nextLong());
int subId = Math.abs(random.nextInt());
this.subId = subId;
List list = new ArrayList(5);
for(int i&#61;0;i<5;i&#43;&#43;){
list.add(Math.abs(random.nextInt()));
}
this.catIds &#61; list;
this.systemName &#61; subId%1 &#61;&#61; 0?"oa":"cms";
this.host &#61; subId%1 &#61;&#61; 0?"10.0.0.1":"10.2.0.1";
this.desc &#61; "中文" &#43; UUID.randomUUID().toString();
}
public LogModel(long id,int subId,String sysName,String host,String desc,List catIds){
this.id &#61; id;
this.subId &#61; subId;
this.systemName &#61; sysName;
this.host &#61; host;
this.desc &#61; desc;
this.catIds &#61; catIds;
}
...//省去get,set方法
}
同时&#xff0c;因为ES在索引的时候&#xff0c;一般都用json格式&#xff0c;因此&#xff0c;使用jackson定义了一个将对象转化成json的工具类&#xff0c;也很简单&#xff0c;代码&#xff1a;
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
public class ESUtils {
private static ObjectMapper objectMapper &#61; new ObjectMapper();
public static String toJson(Object o){
try {
return objectMapper.writeValueAsString(o);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}
}
在开始进行操作ES服务器之前&#xff0c;我们必须得获得ES的API&#xff0c;简单介绍一下ES操作服务器的两种方式&#xff0c;一种是使用Node方式&#xff0c;即本机也启动一个ES&#xff0c;然后跟服务器的ES进行通信&#xff0c;这个node甚至还能存储(奇怪&#xff0c;一般需要这样的方式吗&#xff1f;)&#xff0c;另一种&#xff0c;就是下面我介绍的这一种&#xff0c;通过一个对象使用http协议跟服务器进行交互。
获得一个ES客户端API的代码如下&#xff1a;
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
Settings settings &#61; ImmutableSettings.settingsBuilder()
//指定集群名称
.put("cluster.name", "elasticsearch")
//探测集群中机器状态
.put("client.transport.sniff", true).build();
/*
* 创建客户端&#xff0c;所有的操作都由客户端开始&#xff0c;这个就好像是JDBC的Connection对象
* 用完记得要关闭
*/
Client client &#61; new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
Client对象&#xff0c;可以理解为数据库的Connection对象。好了&#xff0c;准备工作完成&#xff0c;下面就开始增删改查。
Index(增加)
ES里面的增加对象不叫什么add,save等&#xff0c;叫index。但无论叫什么名称&#xff0c;反正就是向ES服务器里面加数据。上面说过一个对象转json的工具类&#xff0c;其实ES的API中&#xff0c;是自带构建json的工具类的。
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
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添加索引对象
* &#64;author donlian
*/
public class IndexTest {
public static void main(String[] argv){
Settings settings &#61; ImmutableSettings.settingsBuilder()
//指定集群名称
.put("cluster.name", "elasticsearch")
//探测集群中机器状态
.put("client.transport.sniff", true).build();
/*
* 创建客户端&#xff0c;所有的操作都由客户端开始&#xff0c;这个就好像是JDBC的Connection对象
* 用完记得要关闭
*/
Client client &#61; new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
String json &#61; ESUtils.toJson(new LogModel());
//在这里创建我们要索引的对象
IndexResponse response &#61; client.prepareIndex("twitter", "tweet")
//必须为对象单独指定ID
.setId("1")
.setSource(json)
.execute()
.actionGet();
//多次index这个版本号会变
System.out.println("response.version():"&#43;response.version());
client.close();
}
}
运行这个代码&#xff0c;就向ES插入了一条数据&#xff0c;你运行两遍&#xff0c;还是一条。ES根据你设置的ID来设置对象&#xff0c;如果没有则插入&#xff0c;有则更新。每更新一次&#xff0c;对应的version加1.
好了&#xff0c;在次&#xff0c;使用以下命令&#xff0c;应该能够查询到一条记录了。
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
curl -XGET &#39;http://localhost:9200/twitter/tweet/1&#39;
delete(删除)
有了增加的例子&#xff0c;删除的例子也就好写了。增加是prepareIndex&#xff0c;删除是prepareDelete,查询就是PrepareGet。
代码如下&#xff1a;
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
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 &#61; ImmutableSettings.settingsBuilder()
//指定集群名称
.put("cluster.name", "elasticsearch")
//探测集群中机器状态
.put("client.transport.sniff", true).build();
/*
* 创建客户端&#xff0c;所有的操作都由客户端开始&#xff0c;这个就好像是JDBC的Connection对象
* 用完记得要关闭
*/
Client client &#61; new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
//在这里创建我们要索引的对象
DeleteResponse response &#61; client.prepareDelete("twitter", "tweet", "1")
.execute().actionGet();
System.out.println(response.getId());
System.out.println(ESUtils.toJson(response.getHeaders()));
}
}
GET(查询)
Java代码 ![3939d3710641708a5c8bd42373efe525.png](https://img8.php1.cn/3cdc5/ff78/bdf/3fbb92a2cd2f0b26.png)
import org.elasticsearch.action.get.GetResponse;
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;
public class GetTest {
public static void main(String[] argv){
Settings settings &#61; ImmutableSettings.settingsBuilder()
//指定集群名称
.put("cluster.name", "elasticsearch")
//探测集群中机器状态
.put("client.transport.sniff", true).build();
/*
* 创建客户端&#xff0c;所有的操作都由客户端开始&#xff0c;这个就好像是JDBC的Connection对象
* 用完记得要关闭
*/
Client client &#61; new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("192.168.1.106", 9300));
//在这里创建我们要索引的对象
GetResponse response &#61; client.prepareGet("twitter", "tweet", "1")
.execute().actionGet();
System.out.println("response.getId():"&#43;response.getId());
System.out.println("response.getSourceAsString():"&#43;response.getSourceAsString());
}
}
好了&#xff0c;增删改查的代码写完。至于搜索&#xff0c;那是一个比较深入的话题&#xff0c;我也在慢慢探索。我时间我会继续写下去。