前面十节课我们已经把ES的基本概念和使用讲的差不多了,现在我们就用基于java来实际开发一个操作ES的小项目,带大家来一起练练手。
1、我们用IDEA创建一个maven项目
项目结构如上图所示,然后我们就引入操作ES需要用到的jar和JUNIT,pom.xml最终配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kakatadage</groupId>
<artifactId>esdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.5.1</version>
</dependency>
</dependencies>
<build>
<finalName>esdemo</finalName>
<plugins>
<plugin>
<!-- 设置javac编译器的版本和编码字符 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf8</encoding><!-- 编译器编码 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
项目创建完成后,我们就可以针对ES实现数据的增删改查了。
2、实现ES实现查询
新建一个测试类,如下图:
最后代码如下所示:
package com.kakatadage.esdemo.test; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.net.InetAddress; import java.net.UnknownHostException; public class EsDemo { //从ES中查询数据 @Test public void test1() throws UnknownHostException { //指定ES集群 Settings setting = Settings.builder().put("cluster.name", "my-application").build(); //创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress( new TransportAddress( InetAddress.getByName("127.0.0.1"),9300)); //get方式数据查询 ,参数为Index,type和id GetResponse response = client.prepareGet("lib4","items","5").get(); System.out.println(response.getSourceAsString()); client.close(); } }
返回结果就是一个JSON字符串
{ "price" : null, "itemID" : "ID1005" }
3、java 插入ES一条数据
//插入数据 @Test public void test2() throws IOException { //指定ES集群 Settings setting = Settings.builder().put("cluster.name", "my-application").build(); //创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress( new TransportAddress( InetAddress.getByName("127.0.0.1"),9300)); XContentBuilder doc = XContentFactory.jsonBuilder() .startObject() .field("id","1") .field("title","我在学习es插入操作") .field("content","好好学习,天天向上") .endObject(); //添加一个doc IndexResponse response = client.prepareIndex("lib5","testadd",null)//id为null,由ES自己生成 .setSource(doc).get(); System.out.println(response.status());//打印添加是否成功 client.close(); }
返回结果:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "lib5", "_type": "testadd", "_id": "dympT2cBfvwZhJjhaFQ3", "_score": 1, "_source": { "id": "1", "title": "我在学习es插入操作", "content": "好好学习,天天向上" } } ] } }
4、java 删除ES一个文档
//删除文档 @Test public void test3() throws IOException { //指定ES集群 Settings setting = Settings.builder().put("cluster.name", "my-application").build(); //创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress( new TransportAddress( InetAddress.getByName("127.0.0.1"),9300)); DeleteResponse response = client.prepareDelete("lib5","testadd","dympT2cBfvwZhJjhaFQ3") .get(); System.out.println(response.status());//打印添加是否成功 client.close(); }
5、java 修改ES一个文档
我们先新增一个,然后根据它的ID来更新
//更新文档 @Test public void test4() throws IOException, ExecutionException, InterruptedException { //指定ES集群 Settings setting = Settings.builder().put("cluster.name", "my-application").build(); //创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress( new TransportAddress( InetAddress.getByName("127.0.0.1"),9300)); UpdateRequest request = new UpdateRequest(); request.index("lib5") .type("testadd") .id("eCm1T2cBfvwZhJjhF1SM") .doc( XContentFactory.jsonBuilder().startObject() .field("title","我在学习ES的修改操作") .field("newadd","新增字段") .endObject() ); UpdateResponse response = client.update(request).get(); System.out.println(response.status());//打印是否成功 client.close(); }