@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());}}
推荐阅读
-
[食客老饕]原创 卤牛肉时,千万别放它,不然这锅牛肉就废了,怪不得肉质又干又柴
-
中英联合声明|加美英澳新外长发表涉港声明,驻加拿大使馆发言人回应
-
【仝卓】仝卓事件最新进展:高考成绩无效,继父被撤职!
-
育儿有道理|预防贫血,改善视力,促进生长发育,一道老少皆宜的营养菜
-
-
在电影院关门的日子里,被替换成了黄金搭档周星驰和吴孟达
-
-
羽书谈兵|美媒揭露内幕:有人隐瞒病情,“罗斯福”号航母5名舰员返舰后复阳
-
智能手机|499元乐视Y1 Pro真机曝光:网友调侃“要窒息了”
-
-
#快科技#13.4/13.4.1正式版金身告破,最新越狱工具发布:iOS
-
本地游 本地游本省游回暖加速!美团大数据:景区门票订单增至2.35倍,近9成来自本省
-
ZAKER生活|女王不允许卡米拉戴王冠、进教堂,查尔斯与卡米拉结婚时
-
-
气温越低,发病率越高?天气开启“速冻模式”,这种疾病需格外警惕
-
「设计师」盘点世界上“奇葩”发明,看完怀疑设计师的智商,网
-
50.8度!印度现“死亡高温”逼近纪录,网友:这样看我国还挺凉快
-
-
-
[换股]A股下周一操作出炉:大盘将迎“暴风雨”,主力调仓换股