1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| //1,索引创建 elasticSearch.getClient().admin().indices().prepareCreate(indices).execute().actionGet(); //2,Mapping 构建 XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject(mappingType).startObject("properties")......endObject().endObject().endObject(); //3,创建使用mapping(mapping 在 Elasticsearch 中的作用就是约束。) //mapping用于数据类型声明, Mapping它定义了 Type 的属性,指定分词器。如: //"id": { "index": "not_analyzed", "type": "string" }
PutMappingRequest mapping = Requests.putMappingRequest(indices) .type(mappingType).source(builder); elasticSearch.getClient().admin().indices().putMapping(mapping).actionGet(); //4,批量放入索引数据(一般循环里使用bulkRequest.add) BulkRequestBuilder bulkRequest = client.prepareBulk(); Map<String, Object> map = new HashMap<>(); map.put("name", "Jack"); IndexRequest request = client.prepareIndex("dept", "employee","3433").setSource(map).request(); bulkRequest.add(request);
|