@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit {@Autowiredprivate RestHighLevelClient client;/*** 添加索引映射字段* @throws IOException*/@Testpublic void addMapping() throws IOException {PutMappingRequest request = new PutMappingRequest();request.indices("cs_index");request.type("_doc");//添加字段Map<String, Object> properties = new HashMap();properties.put("accountName", ImmutableBiMap.of("type", "keyword"));Map<String, Object> mapping = new HashMap<>();mapping.put("properties", properties);request.source(mapping);PutMappingResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());}}2.5、文档管理所谓文档 , 就是向索引里面添加数据 , 方便进行数据查询 , 详细操作内容 , 请看下文!
public class UserDocument {private String id;private String name;private String sex;private Integer age;private String city;private Date createTime;//省略get、set...}@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit {@Autowiredprivate RestHighLevelClient client;/*** 添加文档* @throws IOException*/@Testpublic void addDocument() throws IOException {// 创建对象UserDocument user = new UserDocument();user.setId("1");user.setName("里斯");user.setCity("武汉");user.setSex("男");user.setAge(20);user.setCreateTime(new Date());// 创建索引,即获取索引IndexRequest request = new IndexRequest();// 外层参数request.id("1");request.index("cs_index");request.type("_doc");request.timeout(TimeValue.timeValueSeconds(1));// 存入对象request.source(JSON.toJSONString(user), XContentType.JSON);// 发送请求System.out.println(request.toString());IndexResponse response = client.index(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit {@Autowiredprivate RestHighLevelClient client;/*** 更新文档(按需修改)* @throws IOException*/@Testpublic void updateDocument() throws IOException {// 创建对象UserDocument user = new UserDocument();user.setId("2");user.setName("程咬金");user.setCreateTime(new Date());// 创建索引,即获取索引UpdateRequest request = new UpdateRequest();// 外层参数request.id("2");request.index("cs_index");request.type("_doc");request.timeout(TimeValue.timeValueSeconds(1));// 存入对象request.doc(JSON.toJSONString(user), XContentType.JSON);// 发送请求System.out.println(request.toString());UpdateResponse response = client.update(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit {@Autowiredprivate RestHighLevelClient client;/*** 删除文档* @throws IOException*/@Testpublic void deleteDocument() throws IOException {// 创建索引,即获取索引DeleteRequest request = new DeleteRequest();// 外层参数request.id("1");request.index("cs_index");request.type("_doc");request.timeout(TimeValue.timeValueSeconds(1));// 发送请求System.out.println(request.toString());DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit {@Autowiredprivate RestHighLevelClient client;/*** 查询文档是不是存在* @throws IOException*/@Testpublic void exists() throws IOException {// 创建索引,即获取索引GetRequest request = new GetRequest();// 外层参数request.id("3");request.index("cs_index");request.type("_doc");// 发送请求System.out.println(request.toString());boolean response = client.exists(request, RequestOptions.DEFAULT);System.out.println(response);}}@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit {@Autowiredprivate RestHighLevelClient client;/*** 通过ID , 查询指定文档* @throws IOException*/@Testpublic void getById() throws IOException {// 创建索引,即获取索引GetRequest request = new GetRequest();// 外层参数request.id("1");request.index("cs_index");request.type("_doc");// 发送请求System.out.println(request.toString());GetResponse response = client.get(request, RequestOptions.DEFAULT);System.out.println(response.toString());}}
推荐阅读
-
-
内存|一加首次用上16GB大内存 一加Ace Pro把行业顶配做成标配
-
腕表之家|机芯太厉害很难不心动 入手名士白盘Clifton
-
游研社|有玩家做了个自我惩罚机器,每在黑魂里挨一次打就会物理喷血
-
「咸鱼湾的咸鱼王」这才是荣耀的底牌,荣耀再亮剑!麒麟820+真全面屏+22.5W快充
-
风水秘术|不是迷信,很重要!,这3个位置千万别放扫把
-
板材第一站|| 一周市场 No.130,货源紧张,木材涨价信号强烈
-
【】乌鲁木齐启动新一轮核酸检测工作、各类防疫物资保障充分……乌鲁木齐发布会还有这些要点
-
-
大话西游|大话西游2“去疾”该如何打造,这些玩法你喜欢哪种?
-
-
TVB|距离大红只差一步!TVB上位小花入行十多年默默耕耘,输在人气偏低
-
-
-
-
宋丹丹60岁生日宴众星云集|宋丹丹60岁生日宴众星云集 与干儿子大竣一起过生日
-
华为荣耀@现在手机都放弃曲面屏,追求极致的屏占比,这是为什么?
-
一家|原创胡慧中近照憔悴没有阔太样!一家终于重聚,71岁二婚老公消瘦一圈
-
-
娱乐圈穿搭指南■连衣裙“仙”味十足,背带裙修身显气质,初夏超火的穿搭方式