|
| 1 | +### Index API |
| 2 | +Index API 允许我们存储一个JSON格式的文档,使数据可以被搜索。文档通过index、type、id唯一确定。我们可以自己提供一个id,或者也使用Index API 为我们自动生成一个。 |
| 3 | + |
| 4 | +这里有几种不同的方式来产生JSON格式的文档(document): |
| 5 | + |
| 6 | +- 手动方式,使用原生的byte[]或者String |
| 7 | +- 使用Map方式,会自动转换成与之等价的JSON |
| 8 | +- 使用第三方库来序列化beans,如Jackson |
| 9 | +- 使用内置的帮助类 XContentFactory.jsonBuilder() |
| 10 | + |
| 11 | +#### 手动方式 |
| 12 | + |
| 13 | +[数据格式](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/mapping-date-format.html) |
| 14 | +``` |
| 15 | +String json = "{" + |
| 16 | + "\"user\":\"kimchy\"," + |
| 17 | + "\"postDate\":\"2013-01-30\"," + |
| 18 | + "\"message\":\"trying out Elasticsearch\"" + |
| 19 | + "}"; |
| 20 | +``` |
| 21 | +##### 实例 |
| 22 | + |
| 23 | +``` |
| 24 | +/** |
| 25 | + * 手动生成JSON |
| 26 | + */ |
| 27 | +@Test |
| 28 | +public void CreateJSON(){ |
| 29 | + |
| 30 | + String json = "{" + |
| 31 | + "\"user\":\"fendo\"," + |
| 32 | + "\"postDate\":\"2013-01-30\"," + |
| 33 | + "\"message\":\"Hell word\"" + |
| 34 | + "}"; |
| 35 | + |
| 36 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 37 | + .setSource(json) |
| 38 | + .get(); |
| 39 | + System.out.println(response.getResult()); |
| 40 | + |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +#### Map方式 |
| 45 | +Map是key:value数据类型,可以代表json结构. |
| 46 | + |
| 47 | +``` |
| 48 | +Map<String, Object> json = new HashMap<String, Object>(); |
| 49 | +json.put("user","kimchy"); |
| 50 | +json.put("postDate",new Date()); |
| 51 | +json.put("message","trying out Elasticsearch"); |
| 52 | +``` |
| 53 | +##### 实例 |
| 54 | + |
| 55 | +``` |
| 56 | + /** |
| 57 | + * 使用集合 |
| 58 | + */ |
| 59 | +@Test |
| 60 | +public void CreateList(){ |
| 61 | + |
| 62 | + Map<String, Object> json = new HashMap<String, Object>(); |
| 63 | + json.put("user","kimchy"); |
| 64 | + json.put("postDate","2013-01-30"); |
| 65 | + json.put("message","trying out Elasticsearch"); |
| 66 | + |
| 67 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 68 | + .setSource(json) |
| 69 | + .get(); |
| 70 | + System.out.println(response.getResult()); |
| 71 | + |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +#### 序列化方式 |
| 76 | +ElasticSearch已经使用了jackson,可以直接使用它把javabean转为json. |
| 77 | + |
| 78 | +``` |
| 79 | +import com.fasterxml.jackson.databind.*; |
| 80 | +
|
| 81 | +// instance a json mapper |
| 82 | +ObjectMapper mapper = new ObjectMapper(); // create once, reuse |
| 83 | +
|
| 84 | +// generate json |
| 85 | +byte[] json = mapper.writeValueAsBytes(yourbeaninstance); |
| 86 | +``` |
| 87 | +##### 实例 |
| 88 | + |
| 89 | +``` |
| 90 | +/** |
| 91 | + * 使用JACKSON序列化 |
| 92 | + * @throws Exception |
| 93 | + */ |
| 94 | +@Test |
| 95 | +public void CreateJACKSON() throws Exception{ |
| 96 | + |
| 97 | + CsdnBlog csdn=new CsdnBlog(); |
| 98 | + csdn.setAuthor("fendo"); |
| 99 | + csdn.setContent("这是JAVA书籍"); |
| 100 | + csdn.setTag("C"); |
| 101 | + csdn.setView("100"); |
| 102 | + csdn.setTitile("编程"); |
| 103 | + csdn.setDate(new Date().toString()); |
| 104 | + |
| 105 | + // instance a json mapper |
| 106 | + ObjectMapper mapper = new ObjectMapper(); // create once, reuse |
| 107 | +
|
| 108 | + // generate json |
| 109 | + byte[] json = mapper.writeValueAsBytes(csdn); |
| 110 | + |
| 111 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 112 | + .setSource(json) |
| 113 | + .get(); |
| 114 | + System.out.println(response.getResult()); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +#### XContentBuilder帮助类方式 |
| 119 | +ElasticSearch提供了一个内置的帮助类XContentBuilder来产生JSON文档 |
| 120 | + |
| 121 | +``` |
| 122 | +// Index name |
| 123 | +String _index = response.getIndex(); |
| 124 | +// Type name |
| 125 | +String _type = response.getType(); |
| 126 | +// Document ID (generated or not) |
| 127 | +String _id = response.getId(); |
| 128 | +// Version (if it's the first time you index this document, you will get: 1) |
| 129 | +long _version = response.getVersion(); |
| 130 | +// status has stored current instance statement. |
| 131 | +RestStatus status = response.status(); |
| 132 | +``` |
| 133 | + |
| 134 | +##### 实例 |
| 135 | + |
| 136 | +``` |
| 137 | +/** |
| 138 | + * 使用ElasticSearch 帮助类 |
| 139 | + * @throws IOException |
| 140 | + */ |
| 141 | +@Test |
| 142 | +public void CreateXContentBuilder() throws IOException{ |
| 143 | + |
| 144 | + XContentBuilder builder = XContentFactory.jsonBuilder() |
| 145 | + .startObject() |
| 146 | + .field("user", "ccse") |
| 147 | + .field("postDate", new Date()) |
| 148 | + .field("message", "this is Elasticsearch") |
| 149 | + .endObject(); |
| 150 | + |
| 151 | + IndexResponse response = client.prepareIndex("fendo", "fendodata").setSource(builder).get(); |
| 152 | + System.out.println("创建成功!"); |
| 153 | + |
| 154 | + |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +#### 综合实例 |
| 159 | +``` |
| 160 | + |
| 161 | +import java.io.IOException; |
| 162 | +import java.net.InetAddress; |
| 163 | +import java.net.UnknownHostException; |
| 164 | +import java.util.Date; |
| 165 | +import java.util.HashMap; |
| 166 | +import java.util.Map; |
| 167 | + |
| 168 | +import org.elasticsearch.action.index.IndexResponse; |
| 169 | +import org.elasticsearch.client.transport.TransportClient; |
| 170 | +import org.elasticsearch.common.settings.Settings; |
| 171 | +import org.elasticsearch.common.transport.InetSocketTransportAddress; |
| 172 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 173 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 174 | +import org.elasticsearch.transport.client.PreBuiltTransportClient; |
| 175 | +import org.junit.Before; |
| 176 | +import org.junit.Test; |
| 177 | + |
| 178 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 179 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 180 | + |
| 181 | +public class CreateIndex { |
| 182 | + |
| 183 | + private TransportClient client; |
| 184 | + |
| 185 | + @Before |
| 186 | + public void getClient() throws Exception{ |
| 187 | + //设置集群名称 |
| 188 | + Settings settings = Settings.builder().put("cluster.name", "my-application").build();// 集群名 |
| 189 | + //创建client |
| 190 | + client = new PreBuiltTransportClient(settings) |
| 191 | + .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * 手动生成JSON |
| 196 | + */ |
| 197 | + @Test |
| 198 | + public void CreateJSON(){ |
| 199 | + |
| 200 | + String json = "{" + |
| 201 | + "\"user\":\"fendo\"," + |
| 202 | + "\"postDate\":\"2013-01-30\"," + |
| 203 | + "\"message\":\"Hell word\"" + |
| 204 | + "}"; |
| 205 | + |
| 206 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 207 | + .setSource(json) |
| 208 | + .get(); |
| 209 | + System.out.println(response.getResult()); |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + /** |
| 215 | + * 使用集合 |
| 216 | + */ |
| 217 | + @Test |
| 218 | + public void CreateList(){ |
| 219 | + |
| 220 | + Map<String, Object> json = new HashMap<String, Object>(); |
| 221 | + json.put("user","kimchy"); |
| 222 | + json.put("postDate","2013-01-30"); |
| 223 | + json.put("message","trying out Elasticsearch"); |
| 224 | + |
| 225 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 226 | + .setSource(json) |
| 227 | + .get(); |
| 228 | + System.out.println(response.getResult()); |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * 使用JACKSON序列化 |
| 234 | + * @throws Exception |
| 235 | + */ |
| 236 | + @Test |
| 237 | + public void CreateJACKSON() throws Exception{ |
| 238 | + |
| 239 | + CsdnBlog csdn=new CsdnBlog(); |
| 240 | + csdn.setAuthor("fendo"); |
| 241 | + csdn.setContent("这是JAVA书籍"); |
| 242 | + csdn.setTag("C"); |
| 243 | + csdn.setView("100"); |
| 244 | + csdn.setTitile("编程"); |
| 245 | + csdn.setDate(new Date().toString()); |
| 246 | + |
| 247 | + // instance a json mapper |
| 248 | + ObjectMapper mapper = new ObjectMapper(); // create once, reuse |
| 249 | + |
| 250 | + // generate json |
| 251 | + byte[] json = mapper.writeValueAsBytes(csdn); |
| 252 | + |
| 253 | + IndexResponse response = client.prepareIndex("fendo", "fendodate") |
| 254 | + .setSource(json) |
| 255 | + .get(); |
| 256 | + System.out.println(response.getResult()); |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * 使用ElasticSearch 帮助类 |
| 261 | + * @throws IOException |
| 262 | + */ |
| 263 | + @Test |
| 264 | + public void CreateXContentBuilder() throws IOException{ |
| 265 | + |
| 266 | + XContentBuilder builder = XContentFactory.jsonBuilder() |
| 267 | + .startObject() |
| 268 | + .field("user", "ccse") |
| 269 | + .field("postDate", new Date()) |
| 270 | + .field("message", "this is Elasticsearch") |
| 271 | + .endObject(); |
| 272 | + |
| 273 | + IndexResponse response = client.prepareIndex("fendo", "fendodata").setSource(builder).get(); |
| 274 | + System.out.println("创建成功!"); |
| 275 | + |
| 276 | + |
| 277 | + } |
| 278 | + |
| 279 | +} |
| 280 | +``` |
| 281 | + |
| 282 | + |
| 283 | +> 你还可以通过startArray(string)和endArray()方法添加数组。.field()方法可以接受多种对象类型。你可以给它传递数字、日期、甚至其他XContentBuilder对象。 |
0 commit comments