Elasticsearch学习笔记

0-创建索引

public class Es_Create {
    public static void main(String[] args) throws Exception {
        //创建es客户端对象
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建索引请求对象
        CreateIndexRequest request = new CreateIndexRequest("system");
        //发送请求获取响应对象
        CreateIndexResponse response = esClient.indices().create(request, RequestOptions.DEFAULT);
        Boolean flag = response.isAcknowledged();
        System.out.println("创建请求状态"+flag);
        //关闭es客户端
        esClient.close();
    }
}

 CreateIndexResponse response = esClient.indices().create(request,RequesOption.DEFAULT);

CreateIndexResponse

1-查询索引

public class Es_Get {
    public static void main(String[] args) throws Exception {
        //创建es客户端对象
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建查询索引请求对象
        GetIndexRequest request = new GetIndexRequest("system");
        //发送请求获取响应对象
        GetIndexResponse response = esClient.indices().get(request, RequestOptions.DEFAULT);
        System.out.println(response);
        System.out.println(response.getSettings());
        System.out.println(response.getAliases());
        System.out.println(response.getMappings());
        //关闭客户端
        esClient.close();
    }
}

 GetIndexResponse response = esClient.indices().get(request,RequestOptions.DEFAULT);

GetIndexResponse

2-删除索引

public class Es_Delete {
    public static void main(String[] args) throws Exception{
        //创建es客户端
        RestHighLevelClient esClient  = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建删除索引请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("system");
        //发送请求接收响应对象
        AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println("删除索引"+response.isAcknowledged());
        //关闭es客户端
        esClient.close();
    }
}

 AcknowledgedResponse response = esClient.indices().delete(request,RequestOptions.DEFAULT);

3-添加索引

public class Es_Insert {
    public static void main(String[] args) throws Exception{
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        IndexRequest request = new IndexRequest();
        //设置索引及其唯一标志id
        request.index("system").id("8520");
        //创建数据对象
        User user = new User();
        user.setSex("男");
        user.setAge(18);
        user.setName("小红");
        //将数据对象转成json
        ObjectMapper objectMapper = new ObjectMapper();
        String userJson = objectMapper.writeValueAsString(user);
        //添加数据文档,格式为json
        request.source(userJson, XContentType.JSON);
        //发送请求获取请求对象
        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        //打印索引名称
        System.out.println(response.getIndex());
        //打印响应状态
        System.out.println(response.status());
        System.out.println(response);
        //关闭客户端
        esClient.close();
    }
}

4-修改索引

public class Es_Update {
    public static void main(String[] args) throws Exception {
        //创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //修改文档请求对象
        UpdateRequest request = new UpdateRequest();
        //配置修改参数
        request.index("system").id("8520");
        //设置请求体对数据进行修改
        request.doc(XContentType.JSON,"sex","女");
        //发送请求获取响应对象
        UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
        System.out.println(response.getIndex());
        System.out.println(response.getId());
        System.out.println(response.getResult());
        //关闭es客户端
        esClient.close();
    }
}

5-指定文档查询

public class Es_Select {
    public static void main(String[] args) throws Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        GetRequest request = new GetRequest();
        request.index("system").id("8520");
        GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
        System.out.println(response.getIndex());
        System.out.println(response.getId());
        System.out.println(response.getSourceAsString());
        System.out.println(response.getType());
        esClient.close();
    }
}

6-指定文档删除

public class Es_DeleteById {
    //指定文档删除
    public static void main(String[] args) throws Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        DeleteRequest request = new DeleteRequest().index("system").id("8520");
        DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
        esClient.close();
    }
}

7-批量新增

public class Es_InsertBatch {
    public static void main(String[] args) throws Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        BulkRequest request = new BulkRequest();
        request.add(new
                IndexRequest().index("system").id("100101")
                .source(XContentType.JSON,"name", "小一"));
        request.add(new
                IndexRequest().index("system").id("100102")
                .source(XContentType.JSON,"name", "小二"));
        request.add(new
                IndexRequest().index("system").id("100103")
                .source(XContentType.JSON,"name", "小三"));
        request.add(new
                IndexRequest().index("system").id("100104")
                .source(XContentType.JSON,"name", "小四"));
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getItems());
        System.out.println(response.getTook());
        esClient.close();
    }
}

8-批量删除

public class Es_InsertBatch {
    public static void main(String[] args) throws Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        BulkRequest request = new BulkRequest();
        request.add(DeleteRequest.index("system").id("100101"));
        request.add(DeleteRequest.index("system").id("100102"));
        request.add(DeleteRequest.index("system").id("100103"));
        request.add(DeleteRequest.index("system").id("100104"));
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getItems());
        System.out.println(response.getTook());
        esClient.close();
    }

9-索引全文档查询

9.1批量添加数据

request.add(new IndexRequest().index("system").id("123456001").source(XContentType.JSON,"name","小一","sex","男","age",18));
        request.add(new IndexRequest().index("system").id("123456002").source(XContentType.JSON,"name","小二","sex","男","age",18));
        request.add(new IndexRequest().index("system").id("123456003").source(XContentType.JSON,"name","小三","sex","男","age",18));
        request.add(new IndexRequest().index("system").id("123456004").source(XContentType.JSON,"name","小四","sex","男","age",18));
        request.add(new IndexRequest().index("system").id("123456005").source(XContentType.JSON,"name","小五","sex","男","age",18));
        request.add(new IndexRequest().index("system").id("123456006").source(XContentType.JSON,"name","小六","sex","男","age",18));

9.2查询

public class Es_SelectAll {
    public static void main(String[] args) throws Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        //创建请求对象
        SearchRequest request = new SearchRequest();
        request.indices("system");
        //构建查询请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查询所有数据  QueryBuilders.matchAllQuery()
        request.source(sourceBuilder.query(QueryBuilders.matchAllQuery()));
        //发送请求获取响应
        SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(response.getTook());
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
            System.out.println("++++++++++++++");
        }
        esClient.close();
    }

10-条件查询

public class Es_SelectBy {
    public static void main(String[] args) throws  Exception{
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        SearchRequest request = new SearchRequest();
        request.indices("system");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        request.source(sourceBuilder.query(QueryBuilders.termQuery("age",18)));
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits){
            System.out.println(hit.getSourceAsString());
        }
        System.out.println(response.getHits());
        esClient.close();
    }
}

11-分页查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值