当前位置:   article > 正文

ElasticSearch在Java中的基本使用方式_java引入elasticsearch-java如何使用

java引入elasticsearch-java如何使用

客户端创建

public class EsTest_client {
    //ES测试代码
    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

索引使用方式

索引创建

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        CreateIndexRequest user = new CreateIndexRequest("user");
        CreateIndexResponse response = esClient.indices().create(user, RequestOptions.DEFAULT);

        //响应状态
        boolean acknowledged = response.isAcknowledged();
        System.out.println("索引操作,"+ acknowledged);

        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

索引查询

   public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        GetIndexRequest user = new GetIndexRequest("user");
        //获取目录数据
        GetIndexResponse getIndexResponse = esClient.indices().get(user, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getSettings());
        System.out.println(getIndexResponse.getAliases());
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

删除索引

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        DeleteIndexRequest user = new DeleteIndexRequest("user");
        //删除索引
        AcknowledgedResponse delete = esClient.indices().delete(user, RequestOptions.DEFAULT);

        System.out.println(delete.isAcknowledged());
        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

查询基本操作

插入一条数据

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //操作插入的index
        IndexRequest request = new IndexRequest("user");
        //设置id
        request.id("1001");
        User user1 = new User();
        user1.setName("张三");
        user1.setAge(20);
        user1.setAddress("中国");
        //将插入对象JSON化
        ObjectMapper objectMapper = new ObjectMapper();
        String userJson = objectMapper.writeValueAsString(user1);
        //指定插入数据的类型
        request.source(userJson, XContentType.JSON);
        //插入数据
        IndexResponse index = esClient.index(request, RequestOptions.DEFAULT);
        System.out.println(index.getResult());
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

获取一条ID的数据

   public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        GetRequest getRequest = new GetRequest();
        GetRequest user = getRequest.index("user").id("1001");
        GetResponse response = esClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.getSourceAsString());
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

更新一条数据

   public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("user").id("1001").doc(XContentType.JSON,"name","李四");
        UpdateResponse update = esClient.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(update.getGetResult());
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

删除一条数据

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.index("user").id("1001");
        DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.getResult());

        //关闭Es客户端
        esClient.close();

    }
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

批量操作

批量新增

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
       // 批量插入
        BulkRequest bulkRequest = new BulkRequest();

        bulkRequest.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","zhangsan","address","河北","age","18"));
        bulkRequest.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","address","河南","age","19"));
        bulkRequest.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu","address","山东","age","17"));
        bulkRequest.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","zhaoliu1","address","山西","age","18"));
        bulkRequest.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","zhaoliu2","address","江苏","age","18"));
        bulkRequest.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","zhaoliu3","address","天津","age","18"));
        bulkRequest.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON,"name","zhaoliu4","address","北京","age","18"));

        BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.getItems());
        System.out.println(bulk.getTook());
        
        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

批量删除

public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
       // 批量删除
        BulkRequest bulkRequest = new BulkRequest();

        bulkRequest.add(new DeleteRequest().index("user").id("1001"));
        bulkRequest.add(new DeleteRequest().index("user").id("1002"));
        bulkRequest.add(new DeleteRequest().index("user").id("1003"));
        bulkRequest.add(new DeleteRequest().index("user").id("1004"));

        BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.getItems());
        System.out.println(bulk.getTook());
        //关闭Es客户端
        esClient.close();

    }
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

高级查询

全量查询

 public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        
        searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));

        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

分页查询

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        //分页查询
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //分页
        query.from(0);
        query.size(2);
        //配置条件
        SearchRequest source = searchRequest.source(query);
        //进行查询
        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

排序查询

public static void main(String[] args) throws Exception {
     //创建ES客户端
     RestHighLevelClient esClient = new RestHighLevelClient(
             //连接地址,接口,连接方式
             RestClient.builder(new HttpHost("localhost",9200,"http"))
     );
     SearchRequest searchRequest = new SearchRequest();
     searchRequest.indices("user");

     // 条件构建的类
     SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
     query.sort("age", SortOrder.DESC);

     //配置条件
     SearchRequest source = searchRequest.source(query);

     //进行查询
     SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
     SearchHits hits = search.getHits();
     System.out.println(hits.getHits());
     System.out.println(search.getTook());

     for (SearchHit hit : hits){
         System.out.println(hit.getSourceAsString());
     }

     //关闭Es客户端
     esClient.close();

 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

模糊查询

    public static void main(String[] args) throws Exception {
       //创建ES客户端
       RestHighLevelClient esClient = new RestHighLevelClient(
               //连接地址,接口,连接方式
               RestClient.builder(new HttpHost("localhost",9200,"http"))
       );
       SearchRequest searchRequest = new SearchRequest();
       searchRequest.indices("user");
       //构建查询对象
       SearchSourceBuilder builder = new SearchSourceBuilder();
       // 针对哪一个字段进行模糊查询(name),查询的值是 zhaoliu,模糊查询的数量,相差一个都能进行匹配得到Fuzziness.ONE
       FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "zhaoliu").fuzziness(Fuzziness.ONE);

       builder.query(fuzziness);
       searchRequest.source(builder);
       SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
       SearchHits hits = search.getHits();
       System.out.println(hits.getHits());
       System.out.println(search.getTook());

       for (SearchHit hit : hits){
           System.out.println(hit.getSourceAsString());
       }

       //关闭Es客户端
       esClient.close();
   } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

聚合查询

  public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");


        //构建查询对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.max("MaxAge").field("age");

        builder.aggregation(aggregationBuilder);

        searchRequest.source(builder);
        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

高亮查询

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");


        //构建查询对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");


        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");

        builder.highlighter(highlightBuilder);
        builder.query(termsQueryBuilder);

        searchRequest.source(builder);
        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
        //关闭Es客户端
        esClient.close();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

分组查询

//ES测试代码   分组查询
    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");


        //构建查询对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("ageFroup").field("age");
        builder.aggregation(termsAggregationBuilder);
        
        searchRequest.source(builder);
        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();

    }
    ```
### 条件查询
```java
  //ES测试代码 条件查询
    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        //条件查询 termQuery
        searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",18)));

        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

条件查询,查询结果包含和不包含某些字段

    //ES测试代码 字段过滤

    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        //条件查询 termQuery
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        // 包含 和 不包含
        String[] include = {};
        // 查询出来的结果就不会包含age字段
        String[] exInclude = {"age"};

        builder.fetchSource(include,exInclude);
        SearchRequest source = searchRequest.source(builder);
        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

多条件查询

   //ES测试代码 组合查询
    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                //连接地址,接口,连接方式
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        //条件查询 termQuery
        SearchSourceBuilder builder = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age",18));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("name","zhaoliu1"));

        //范围查询
        RangeQueryBuilder age = QueryBuilders.rangeQuery("age");
        age.gte(10);
        age.lte(18);

        builder.query(age);

        searchRequest.source(builder);

        SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        System.out.println(hits.getHits());
        System.out.println(search.getTook());

        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭Es客户端
        esClient.close();

    }
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/220495?site
推荐阅读
相关标签
  

闽ICP备14008679号