es 新版
public class EstablishIndex { public static JdbcTemplate getLocalHostTemplate() { DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver"); driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/contract_manage"); driverManagerDataSource.setUsername("root"); driverManagerDataSource.setPassword("root"); return new JdbcTemplate(driverManagerDataSource); } public static RestHighLevelClient getRestHighLevelClient() { return new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))); } public static void main(String[] args) throws Exception { // createIndex(); batchSave(); } /** * 批量新增 * * @param * @return 返回保存结果 * @throws Exception exception */ public static BulkResponse batchSave() { try { RestHighLevelClient client = getRestHighLevelClient(); JdbcTemplate localHostTemplate = getLocalHostTemplate(); String sql = "select * from sys_clause"; List<Map<String, Object>> list = localHostTemplate.queryForList(sql); BulkRequest bulkRequest = new BulkRequest(); for (Map<String, Object> map : list) { Map<String, Object> mapTo = new HashMap<>(); for (String key : map.keySet()) { String s = Tool.lineToHump(key); String value = map.get(s) == null ? "" : map.get(s).toString().trim(); // 这个对象字段get方法的值 mapTo.put(s, value); } String s = JSON.toJSONString(mapTo, SerializerFeature.WriteMapNullValue); System.out.println(s); } return client.bulk(bulkRequest, RequestOptions.DEFAULT); } catch (Exception e) { return null; } } private static void createIndex() throws Exception { CreateIndexRequest createIndexRequest = new CreateIndexRequest("es_clause_index"); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); { builder.startObject("id"); { builder.field("type", "keyword"); } builder.endObject(); builder.startObject("content"); { builder.field("type", "text").field("analyzer", "ik_smart").field("search_analyzer", "ik_max_word"); } builder.endObject(); builder.startObject("tags"); { builder.field("type", "text").field("analyzer", "ik_smart").field("search_analyzer", "ik_max_word"); } builder.endObject(); } builder.endObject(); } builder.endObject(); createIndexRequest.mapping(builder); CreateIndexResponse response = getRestHighLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println(response); } }
故乡明