• ElasticSeach的Api操作学习


    1.创建es连接,输入账号密码 

    1. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    2. credentialsProvider.setCredentials(AuthScope.ANY,
    3. new UsernamePasswordCredentials("elastic", "elastic"));
    4. //es账号密码(默认用户名为elastic,因为在设置密码时其中的一个用户就是elastic)
    5. RestHighLevelClient esclient =new RestHighLevelClient(
    6. RestClient.builder(
    7. new HttpHost("172.16.8.35",9200)
    8. ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
    9. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    10. httpClientBuilder.disableAuthCaching();
    11. return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    12. }
    13. })
    14. );

    2.创建es索引:user

    1. CreateIndexRequest request = new CreateIndexRequest("user");
    2. CreateIndexResponse createIndexResponse = esclient.indices().create(request, RequestOptions.DEFAULT);
    3. boolean acknowledged = createIndexResponse.isAcknowledged();
    4. System.out.println("创建索引操作结果:" + acknowledged);

    3.插入user索引的数据

    1. //插入数据
    2. IndexRequest indexRequest = new IndexRequest();
    3. //需要插入的数据 index|索引 id|唯一标识
    4. indexRequest.index("user").id("1020");
    5. User user = new User();
    6. user.setName("李为");
    7. user.setSex("男");
    8. user.setAge(27);
    9. // 向es插入数据,必须将数据转换为json jackson
    10. ObjectMapper mapper=new ObjectMapper();
    11. String userjson = mapper.writeValueAsString(user);//对象转str
    12. indexRequest.source(userjson, XContentType.JSON);//index为user,id1020的数据
    13. IndexResponse index = esclient.index(indexRequest, RequestOptions.DEFAULT);
    14. System.out.println(index);

    4.查询索引user下的数据

    1. //这个对象增加查询条件
    2. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    3. //指定只返回user对象的name字段
    4. searchSourceBuilder.fetchSource(new String[]{"name"}, new String[]{});
    5. //根据name等于李为查询
    6. BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    7. boolQueryBuilder.must(QueryBuilders.matchQuery("name", "李为"));
    8. searchSourceBuilder.query(boolQueryBuilder);
    9. //es查询对象
    10. SearchRequest searchRequest = new SearchRequest("user");
    11. //把条件设置到es查询数据里面查询
    12. searchRequest.source(searchSourceBuilder);
    13. log.info("es查询语句:{}", searchRequest.source());
    14. SearchResponse searchResponse = esclient.search(searchRequest,
    15. RequestOptions.DEFAULT);
    16. if (searchResponse.status().equals(RestStatus.OK)) {
    17. SearchHits hits = searchResponse.getHits();
    18. for(SearchHit hit:hits){
    19. User user1 = JsonUtil.toBean(hit.getSourceAsString(), User.class);
    20. System.out.println(user1);
    21. }
    22. }

    5.更新索引为user的id为1020的数据

    1. //user索引id为1020的数据性别和年龄分别改为女和18
    2. UpdateRequest update = new UpdateRequest();
    3. update.index("user").id("1020");
    4. update.doc(XContentType.JSON,"sex","女","age",18);
    5. esclient.update(update, RequestOptions.DEFAULT);

    6.删除索引user下id为1020的数据

    1. DeleteRequest deleteRequest = new DeleteRequest();
    2. deleteRequest.index("user").id("1020");
    3. esclient.delete(deleteRequest, RequestOptions.DEFAULT);

    7.索引下user的数据批量插入

    1. //批量插入索引user下的数据
    2. BulkRequest request = new BulkRequest();
    3. request.add(new IndexRequest().index("user").id("1010").source(XContentType.JSON,
    4. "name","李伟","age",27,"sex","男"));
    5. request.add(new IndexRequest().index("user").id("1011").source(XContentType.JSON,
    6. "name","lisi","age",22,"sex","女"));
    7. request.add(new IndexRequest().index("user").id("1012").source(XContentType.JSON,
    8. "name","wangwu","age",26,"sex","女"));
    9. request.add(new IndexRequest().index("user").id("1013").source(XContentType.JSON,
    10. "name","zhaoliu","age",27,"sex","女"));
    11. esclient.bulk(request, RequestOptions.DEFAULT);

    8.批量删除user索引下的数据

    1. //批量删除user索引下的数据
    2. BulkRequest request = new BulkRequest();
    3. request.add(new DeleteRequest().index("user").id("1010"));
    4. request.add(new DeleteRequest().index("user").id("1011"));
    5. request.add(new DeleteRequest().index("user").id("1012"));
    6. request.add(new DeleteRequest().index("user").id("1013"));
    7. esclient.bulk(request, RequestOptions.DEFAULT);

     9.删除user索引

    1. //删除索引user
    2. DeleteIndexRequest shoppingg = new DeleteIndexRequest("user");​
    3. AcknowledgedResponse delete = esclient.indices().delete(shoppingg,
    4. RequestOptions.DEFAULT);

    10.查询user索引

    1. //查询user索引 要索引user存在的情况,否则会报错索引不存在
    2. GetIndexRequest user= new GetIndexRequest("user");
    3. GetIndexResponse response = esclient.indices().get(user,
    4. RequestOptions.DEFAULT);​
    5. System.out.println(response.getAliases());
    6. System.out.println(response.getDataStreams());
    7. System.out.println(response.getDefaultSettings());
    8. System.out.println(response.getIndices());
    9. System.out.println(response.getMappings());
    10. System.out.println(response.getSettings());

    11.分页查询user索引的数据

    1. //查询user索引下第0页的一页3条数据
    2. SearchRequest searchRequest = new SearchRequest();
    3. SearchSourceBuilder query = new
    4. SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
    5. query.from(0);//当前页
    6. query.size(3);//每页条数
    7. searchRequest.indices("user");
    8. searchRequest.source(query);
    9. SearchResponse search = esclient.search(searchRequest, RequestOptions.DEFAULT);
    10. SearchHits hits = search.getHits();
    11. for(SearchHit hit:hits){
    12. System.out.println(hit.getSourceAsString());
    13. }

    12.组合查询索引user的数据

    1. SearchRequest searchRequest = new SearchRequest();
    2. searchRequest.indices("user");
    3. SearchSourceBuilder builder = new SearchSourceBuilder();
    4. BoolQueryBuilder bool = new BoolQueryBuilder();
    5. bool.must(QueryBuilders.matchQuery("sex","男"));
    6. bool.must(QueryBuilders.matchQuery("age",27));
    7. //组合查询
    8. builder.query(bool);
    9. searchRequest.source(builder);
    10. SearchResponse search = esclient.search(searchRequest, RequestOptions.DEFAULT);
    11. ///关闭连接
    12. SearchHits hits = search.getHits();
    13. esclient.close();
    14. for(SearchHit hit:hits){
    15. System.out.println(hit.getSourceAsString());
    16. }

  • 相关阅读:
    Pytorch基础:Tensor的transpose方法
    总结:Tomcat的IO模型
    Arc length
    HashMap、HashTable、CurrentHashMap对比
    Redis 事务
    堆排序+TOPK问题
    浅谈代码语言的魅力
    数据结构-堆
    C++ 学习系列 -- std::stack 与 std::queue
    报错 - Junit 单元测试@Before没有执行
  • 原文地址:https://blog.csdn.net/qq_33816292/article/details/126271241