引入依赖
引入的依赖版本必须和正在运行的Elasticsearch版本一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
|
实现
连接Elasticsearch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| import com.google.gson.JsonObject; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test;
import java.net.InetAddress;
public class MyTest {
private static String host = "192.168.0.116"; private static int port = 9300; private TransportClient client = null;
@Before public void getClient() throws Exception { client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port)); }
@After public void closeClient() { if (client != null) { client.close(); } } }
|
增加索引并且在这个索引下添加文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Test public void addIndex() { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", "Java编程思想"); jsonObject.addProperty("publishDate", "2012-11-23"); jsonObject.addProperty("price", 88); IndexResponse indexResponse = client .prepareIndex("book", "java", "1") .setSource(jsonObject.toString(), XContentType.JSON) .get(); System.out.println("索引名称:" + indexResponse.getIndex()); System.out.println("索引类型:" + indexResponse.getType()); System.out.println("文档id:" + indexResponse.getId()); System.out.println("当前状态:" + indexResponse.status()); }
|
删除索引
1 2 3 4 5 6 7 8 9 10
| @Test public void deleteIndex() { DeleteResponse deleteResponse = client .prepareDelete("book", "java", "1") .get(); System.out.println("索引名称:" + deleteResponse.getIndex()); System.out.println("索引类型:" + deleteResponse.getType()); System.out.println("文档id:" + deleteResponse.getId()); System.out.println("当前状态:" + deleteResponse.status()); }
|
修改索引的文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Test public void updateIndex() { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", "Java编程思想2"); jsonObject.addProperty("publishDate", "2012-11-22"); jsonObject.addProperty("price", 77);
UpdateResponse updateResponse = client .prepareUpdate("book", "java", "1") .setDoc(jsonObject.toString(), XContentType.JSON).get(); System.out.println("索引名称:" + updateResponse.getIndex()); System.out.println("索引类型:" + updateResponse.getType()); System.out.println("文档id:" + updateResponse.getId()); System.out.println("当前状态:" + updateResponse.status()); }
|
查询索引文档
1 2 3 4 5 6 7 8
|
@Test public void getIndex() { GetResponse getResponse = client.prepareGet("book", "java", "1").get(); System.out.println(getResponse.getSourceAsString()); }
|