ElasticSearch(2)--使用Java客户端创建文档

创建maven工程:

引入依赖:

 
  1. <dependencies>

  2. <dependency>

  3. <groupId>org.elasticsearch</groupId>

  4. <artifactId>elasticsearch</artifactId>

  5. <version>2.4.0</version>

  6. </dependency>

  7. <dependency>

  8. <groupId>junit</groupId>

  9. <artifactId>junit</artifactId>

  10. <version>4.12</version>

  11. </dependency>

  12. <!-- Jackson Json处理工具包 -->

  13. <dependency>

  14. <groupId>com.fasterxml.jackson.core</groupId>

  15. <artifactId>jackson-databind</artifactId>

  16. <version>2.4.2</version>

  17. </dependency>

  18. </dependencies>

创建文档(没有映射创建,自动创建索引 和 映射):

 
  1. package com.es.demo;

  2.  
  3. import java.net.InetAddress;

  4. import java.util.HashMap;

  5. import java.util.Map;

  6.  
  7. import org.elasticsearch.action.index.IndexResponse;

  8. import org.elasticsearch.client.transport.TransportClient;

  9. import org.elasticsearch.common.transport.InetSocketTransportAddress;

  10. import org.junit.After;

  11. import org.junit.Before;

  12. import org.junit.Test;

  13.  
  14. import com.fasterxml.jackson.databind.ObjectMapper;

  15.  
  16. public class TestES {

  17. private static final String HOST = "127.0.0.1";

  18. private static final int PORT = 9300;

  19.  
  20. private static final ObjectMapper MAPPER = new ObjectMapper();

  21. private TransportClient client = null;

  22.  
  23. // 创建文档,数据源使用的是json

  24. @Test

  25. public void createDocumentByJson() throws Exception{

  26. Map<String, Object> source = new HashMap<String,Object>();

  27. source.put("name", "APPLE手机");

  28. source.put("price", 13999);

  29.  
  30. // 也可以转化java的bean

  31. String json = MAPPER.writeValueAsString(source);

  32. IndexResponse response = this.client.prepareIndex("eshop", "product")

  33. .setSource(json)

  34. .execute()

  35. .actionGet();

  36.  
  37. // 获取结果

  38. String index = response.getIndex();

  39. String type = response.getType();

  40. String id = response.getId();

  41. long version = response.getVersion();

  42. boolean created = response.isCreated();

  43.  
  44. System.out.println("索引是: " + index);

  45. System.out.println("类型是: " + type);

  46. System.out.println("文档id是: " + id);

  47. System.out.println("版本是: " + version);

  48. System.out.println("是否创建: " + created);

  49. }

  50.  
  51. // 创建文档,数据源使用的是map

  52. @Test

  53. public void createDocumentByMap(){

  54. Map<String, Object> source = new HashMap<String,Object>();

  55. source.put("name", "华为手机");

  56. source.put("price", 3999);

  57. IndexResponse response = this.client.prepareIndex("eshop", "product")

  58. .setSource(source)

  59. .execute()

  60. .actionGet();

  61.  
  62. // 获取结果

  63. String index = response.getIndex();

  64. String type = response.getType();

  65. String id = response.getId();

  66. long version = response.getVersion();

  67. boolean created = response.isCreated();

  68.  
  69. System.out.println("索引是: " + index);

  70. System.out.println("类型是: " + type);

  71. System.out.println("文档id是: " + id);

  72. System.out.println("版本是: " + version);

  73. System.out.println("是否创建: " + created);

  74. }

  75.  
  76. // 获取客户端

  77. @Before

  78. public void getClient() throws Exception{

  79. client = TransportClient.builder()

  80. .build()

  81. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));

  82. }

  83.  
  84. // 关闭客户端

  85. @After

  86. public void closeClient(){

  87. if (this.client != null){

  88. this.client.close();

  89. }

  90. }

  91. }

查看插入的结果:

猜你喜欢

转载自blog.csdn.net/gebitan505/article/details/81460254