@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());}}
推荐阅读
-
中美关系|澳洲出现劳动力危机,一托儿中心开出70万年薪,三年来却无人应聘
-
老友相伴|平时坚持“2多2少”,或许对肝脏健康有好处,男性日常“养肝”
-
有手就行10.9被凤凰传奇和波比血虐?设计师却说:这是最完美最平衡的版本
-
-
新华网|加强长江干堤和“两湖”湖区堤防巡查防守是当前长江防汛重点任务
-
3男子黄瓜配花生米喝117瓶啤酒:3男子黄瓜配花生米喝117瓶啤酒 商家回应确有此事
-
-
中国青年报|参与消费扶贫 超七成受访者最看重产品质量
-
讲武堂|再击毙阿塞拜疆1名军官4名士兵,幕后原因揭开,亚美尼亚突然出手
-
二小埃米|与郭晶晶共谈育儿,产后身材像少女,35岁吴敏霞嫁CEO
-
-
-
右手网一加为尚未正式发售的 OnePlus Nord 推送首个 OxygenOS 更新
-
「直播吧官方」尤文对伊卡尔迪兴趣有所冷却,意媒:考虑引进米利克
-
车家号“东瀛宝马”,造型运动,操控好,真香,新款马自达6
-
青柠的院子|欧洲最受欢迎思维方法《赢者思维》告诉你:你会成为你想成为的人
-
-
国籍■十年寒窗比不过一纸国籍?教育部出新规:“假外国人”的好日子该结束了
-
法拉利|出必买?法拉利首款SUV官宣:前脸凶狠又“邪恶”
-