@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());}}
推荐阅读
-
-
交通|北京一男子划皮划艇上班不堵车 护城河里欣赏优美环境:你觉得合适吗?
-
-
榴莲酥皮|鞠婧祎造型遭吐槽,古装模仿并非不可,但是过于“买家秀”
-
有什么低成本创业的项目,2020年低成本创业好项目-
-
天天时尚传媒▲米奇头造型上演捧脸杀,少女感十足!,李兰迪瘦身成功
-
奖项|张艺兴连拿4个奖 小绵羊是真的棒!科普一下张艺兴拿了什么奖项
-
如果你只能留下你电脑中的一款游戏而其他的全部要被删掉,你会留下哪部作品为啥?
-
-
「日本」偷窥这些日本主妇家,我发现有娃家里不乱的秘密是……
-
数码生活互动打破手机影像上限!OPPO Reno4 Pro实力呈现“所见即所得”
-
-
华为老用户注意!Mate20等14款设备开启EMUI11公测
-
林志玲|“罗志祥”有多会玩?让综艺告诉你:“女艺人”毫无尊严,“占尽便宜”只能配合
-
张智霖|难怪《披哥2》口碑下滑,明明不熟还刻意煽情,看了让人尴尬症都犯了
-
女人梦见和别的男人发生了性关系,是咋回事 女人梦见和别的男人发生了性关系
-
#平陆交警大队#副大队长史向东“五一”期间慰问一线执勤民警
-
-
华为公司@华为P40系列摄影力分析:这次憋了哪些大招?
-