1.创建es连接,输入账号密码
- final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(AuthScope.ANY,
- new UsernamePasswordCredentials("elastic", "elastic"));
- //es账号密码(默认用户名为elastic,因为在设置密码时其中的一个用户就是elastic)
- RestHighLevelClient esclient =new RestHighLevelClient(
- RestClient.builder(
- new HttpHost("172.16.8.35",9200)
- ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
- public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
- httpClientBuilder.disableAuthCaching();
- return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
- }
- })
- );
2.创建es索引:user
- CreateIndexRequest request = new CreateIndexRequest("user");
- CreateIndexResponse createIndexResponse = esclient.indices().create(request, RequestOptions.DEFAULT);
-
- boolean acknowledged = createIndexResponse.isAcknowledged();
- System.out.println("创建索引操作结果:" + acknowledged);
3.插入user索引的数据
- //插入数据
- IndexRequest indexRequest = new IndexRequest();
- //需要插入的数据 index|索引 id|唯一标识
- indexRequest.index("user").id("1020");
- User user = new User();
- user.setName("李为");
- user.setSex("男");
- user.setAge(27);
- // 向es插入数据,必须将数据转换为json jackson
- ObjectMapper mapper=new ObjectMapper();
- String userjson = mapper.writeValueAsString(user);//对象转str
- indexRequest.source(userjson, XContentType.JSON);//index为user,id1020的数据
- IndexResponse index = esclient.index(indexRequest, RequestOptions.DEFAULT);
- System.out.println(index);
4.查询索引user下的数据
- //这个对象增加查询条件
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- //指定只返回user对象的name字段
- searchSourceBuilder.fetchSource(new String[]{"name"}, new String[]{});
- //根据name等于李为查询
- BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
- boolQueryBuilder.must(QueryBuilders.matchQuery("name", "李为"));
- searchSourceBuilder.query(boolQueryBuilder);
- //es查询对象
- SearchRequest searchRequest = new SearchRequest("user");
- //把条件设置到es查询数据里面查询
- searchRequest.source(searchSourceBuilder);
- log.info("es查询语句:{}", searchRequest.source());
- SearchResponse searchResponse = esclient.search(searchRequest,
- RequestOptions.DEFAULT);
- if (searchResponse.status().equals(RestStatus.OK)) {
- SearchHits hits = searchResponse.getHits();
- for(SearchHit hit:hits){
- User user1 = JsonUtil.toBean(hit.getSourceAsString(), User.class);
- System.out.println(user1);
- }
- }
5.更新索引为user的id为1020的数据
- //把user索引id为1020的数据性别和年龄分别改为女和18
- UpdateRequest update = new UpdateRequest();
- update.index("user").id("1020");
- update.doc(XContentType.JSON,"sex","女","age",18);
- esclient.update(update, RequestOptions.DEFAULT);
6.删除索引user下id为1020的数据
- DeleteRequest deleteRequest = new DeleteRequest();
- deleteRequest.index("user").id("1020");
- esclient.delete(deleteRequest, RequestOptions.DEFAULT);
7.索引下user的数据批量插入
- //批量插入索引user下的数据
- BulkRequest request = new BulkRequest();
- request.add(new IndexRequest().index("user").id("1010").source(XContentType.JSON,
- "name","李伟","age",27,"sex","男"));
- request.add(new IndexRequest().index("user").id("1011").source(XContentType.JSON,
- "name","lisi","age",22,"sex","女"));
- request.add(new IndexRequest().index("user").id("1012").source(XContentType.JSON,
- "name","wangwu","age",26,"sex","女"));
- request.add(new IndexRequest().index("user").id("1013").source(XContentType.JSON,
- "name","zhaoliu","age",27,"sex","女"));
- esclient.bulk(request, RequestOptions.DEFAULT);
8.批量删除user索引下的数据
- //批量删除user索引下的数据
- BulkRequest request = new BulkRequest();
- request.add(new DeleteRequest().index("user").id("1010"));
- request.add(new DeleteRequest().index("user").id("1011"));
- request.add(new DeleteRequest().index("user").id("1012"));
- request.add(new DeleteRequest().index("user").id("1013"));
- esclient.bulk(request, RequestOptions.DEFAULT);
9.删除user索引
- //删除索引user
- DeleteIndexRequest shoppingg = new DeleteIndexRequest("user");
- AcknowledgedResponse delete = esclient.indices().delete(shoppingg,
- RequestOptions.DEFAULT);
10.查询user索引
- //查询user索引 要索引user存在的情况,否则会报错索引不存在
- GetIndexRequest user= new GetIndexRequest("user");
- GetIndexResponse response = esclient.indices().get(user,
- RequestOptions.DEFAULT);
- System.out.println(response.getAliases());
- System.out.println(response.getDataStreams());
- System.out.println(response.getDefaultSettings());
- System.out.println(response.getIndices());
- System.out.println(response.getMappings());
- System.out.println(response.getSettings());
11.分页查询user索引的数据
- //查询user索引下第0页的一页3条数据
- SearchRequest searchRequest = new SearchRequest();
- SearchSourceBuilder query = new
- SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
- query.from(0);//当前页
- query.size(3);//每页条数
- searchRequest.indices("user");
- searchRequest.source(query);
- SearchResponse search = esclient.search(searchRequest, RequestOptions.DEFAULT);
- SearchHits hits = search.getHits();
- for(SearchHit hit:hits){
- System.out.println(hit.getSourceAsString());
- }
12.组合查询索引user的数据
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.indices("user");
- SearchSourceBuilder builder = new SearchSourceBuilder();
- BoolQueryBuilder bool = new BoolQueryBuilder();
- bool.must(QueryBuilders.matchQuery("sex","男"));
- bool.must(QueryBuilders.matchQuery("age",27));
- //组合查询
- builder.query(bool);
- searchRequest.source(builder);
-
- SearchResponse search = esclient.search(searchRequest, RequestOptions.DEFAULT);
- ///关闭连接
- SearchHits hits = search.getHits();
- esclient.close();
- for(SearchHit hit:hits){
- System.out.println(hit.getSourceAsString());
- }