您当前的位置: 首页 >  Java

星夜孤帆

暂无认证

  • 7浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Elasticsearch之Java Api操作

星夜孤帆 发布时间:2021-11-15 21:53:36 ,浏览量:7

1、Elasticsearch概述 2、Elasticsearch入门 3、Elasticsearch之Java Api操作 4、Elasticsearch集群搭建 5、Elasticsearch进阶 6、Elasticsearch集成SpringBoot 7、Elasticsearch优化 8、Elasticsearch系列总结 一、创建Maven项目 

Elasticsearch软件是由 Java 语言开发的,所以也可以通过 Java API 的方式对 Elasticsearch服务进行访问。

修改pom 文件,增加 Maven 依赖关系。


        
            org.projectlombok
            lombok
            1.16.12
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.elasticsearch
            elasticsearch
            7.8.0
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.8.0
        
        
        
            org.apache.logging.log4j
            log4j-api
            2.8.2
        
        
            org.apache.logging.log4j
            log4j-core
            2.8.2
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.9
        
        
        
            junit
            junit
            4.12
        
    
 1.1 客户端对象

创建类,代码中创建 Elasticsearch 客户端对象

因为早期版本的客户端对象已经不再推荐使用,且在未来版本中会被删除,所以这里我们采用高级 REST 客户端对象

    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        // 关闭客户端连接
        client.close();
    }

注意:

9200 端口为 Elasticsearch 的 Web 通信端口 localhost 为启动 ES 服务的主机名

执行代码,查看控制台信息:

 二、索引操作

ES服务器正常启动后,可以通过 Java API 客户端对象对 ES 索引进行操作 

public class EsIndex {

    public static void main(String[] args) throws IOException {

        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        // 关闭客户端连接
        client.close();
    }

    // 创建索引
    public static void createIndex(RestHighLevelClient client) throws IOException {
        // 创建索引 - 请求对象
        CreateIndexRequest request = new CreateIndexRequest("user");
        // 发送请求,获取响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        boolean acknowledged = response.isAcknowledged();
        // 响应状态
        System.out.println("操作状态 = " + acknowledged);
    }

    // 查看索引
    public static void getIndex(RestHighLevelClient client) throws IOException {
        // 查询索引 - 请求对象
        GetIndexRequest request = new GetIndexRequest("user");
        // 发送请求,获取响应
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
        System.out.println("aliases: " + response.getAliases());
        System.out.println("mappings: " + response.getMappings());
        System.out.println("settings: " + response.getSettings());
    }

    // 删除索引
    public static void deleteIndex(RestHighLevelClient client) throws IOException {
        // 删除索引 - 请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("user");
        // 发送请求,获取响应
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 操作结果
        System.out.println("操作结果: " + response.isAcknowledged());
    }
}
三、文档操作
public class EsDoc {

    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        // 关闭客户端连接
        client.close();
    }

    // 创建文档
    public static void createDoc(RestHighLevelClient client) throws IOException {
        // 新增文档 - 请求对象
        IndexRequest request = new IndexRequest();
        // 设置索引及唯一性标识
        request.index("user").id("1001");
        // 创建数据对象
        User user = new User();
        user.setAge(26);
        user.setSex("男");
        user.setName("jak");
        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        // 添加文档数据, 数据格式为Json格式
        request.source(productJson, XContentType.JSON);
        // 客户端发送请求,获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index: " + response.getIndex());
        System.out.println("id: " + response.getId());
        System.out.println("_result: " + response.getResult());
    }

    // 修改文档
    public static void updateDoc(RestHighLevelClient client) throws IOException {
        // 修改文档 - 请求对象
        UpdateRequest request = new UpdateRequest();
        // 配置修改参数
        request.index("user").id("1001");
        // 设置请求体,对数据进行修改
        request.doc(XContentType.JSON, "sex", "女");
        // 客户端发送请求,获取响应对象
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("_index: " + response.getIndex());
        System.out.println("_id: " + response.getId());
        System.out.println("_result: " + response.getResult());
    }

    // 查询文档
    public static void getDoc(RestHighLevelClient client) throws IOException {
        // 创建请求对象
        GetRequest request = new GetRequest().index("user").id("1001");
        // 客户端发送请求,获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index: " + response.getIndex());
        System.out.println("_type: " + response.getType());
        System.out.println("_id: " + response.getId());
        System.out.println("source: " + response.getSourceAsString());
    }

    // 删除文档
    public static void deleteDoc(RestHighLevelClient client) throws IOException {
        // 创建请求对象
        DeleteRequest request = new DeleteRequest().index("user").id("1");
        // 客户端发送请求,获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        // 打印信息
        System.out.println(response.toString());
    }

    // 批量新增
    public static void bulkCreateDoc(RestHighLevelClient client) throws IOException {
        // 创建批量新增请求对象
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan"));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi"));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu"));
        // 客户端发送请求,获取响应对象
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("took: " + responses.getTook());
        System.out.println("items: " + Arrays.toString(responses.getItems()));
    }

    // 批量删除
    public static void bulkDeleteDoc(RestHighLevelClient client) throws IOException {
        // 创建批量删除请求对象
        BulkRequest request = new BulkRequest();
        request.add(new DeleteRequest().index("user").id("1001"));
        request.add(new DeleteRequest().index("user").id("1002"));
        request.add(new DeleteRequest().index("user").id("1003"));
        // 客户端发送请求,获取响应对象
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("took: " + responses.getTook());
        System.out.println("items: " + Arrays.toString(responses.getItems()));

    }
}
四、高级查询
public class EsSearch {

    public static void main(String[] args) throws IOException {

        // 创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
        );

        // 关闭客户端连接
        client.close();
    }

    // 查询所有索引数据
    public static void matchAllQuery(RestHighLevelClient client) throws IOException {
        // 创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");

        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());
        request.source(sourceBuilder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        // 查询匹配
        SearchHits hits = response.getHits();
        System.out.println("took: " + response.getTook());
        System.out.println("timeout: " + response.isTimedOut());
        System.out.println("total: " + hits.getTotalHits());
        System.out.println("MaxScore: " + hits.getMaxScore());
        System.out.println("hits------------->");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("");
        for (SearchHit hit : hits) {
            // 输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        }
        System.out.println("            
关注
打赏
1636984416
查看更多评论
0.0497s