@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());}}
推荐阅读
-
-
安德烈亚·皮尔洛|意甲每日要闻滚动:曼联大佬成皮尔洛见面礼,国米有意收购英超神将
-
人民日报|解码在线教育:隔屏上课,如何改变教与学?
-
Angelababy|《跑男》最新路透,baby的造型简直美翻了!仿佛再见“岳绮罗”
-
新冠肺炎|专家:治愈后的新冠肺炎患者没有传染性 不应歧视和恐惧
-
高血脂|偷寿命的“食物”已公布,不少人当成宝,整箱往家扛,劝你不要碰
-
-
新民晚报|两车相撞 多位热心市民协助交警扶正侧翻车辆
-
阳光八卦馆|俄中边界后贝加尔边疆区新冠患者新增再破新高
-
高铁|直击郑州市民超市囤货:郑州高铁站多名工作人员感染 主城区实行足不出区
-
-
美你的|漳州隐形矫正为什么这么贵?8千和3万到底有什么区别?
-
通天战队 为啥不将根目录文件译成中文?,华为、小米等安卓手机
-
-
「花间雨屋星座」跟这四大星座结婚婚后也会甜得要死,很懂情趣
-
半夜看车|价值上千万,迈巴赫也得躲着走,马路上遇移动“蒙古包”
-
君笺雅侃红楼|连太监都看不起她,贾元春因为得宠晋封贤德妃?别闹了
-
郑太尉观天下|土耳其新战舰变废铁!要搞世界首艘无人机航母?,F35B搞不到
-
车祸事故|广东一特斯拉Model 3追尾货车!现场太惨烈
-