ElasticSearch学习(八)在Java应用中实现批量操作(mget&bulk)和查询删除、match_all(查询所有)

 //mget批量查询
    @Test
    public void test6() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));


        MultiGetResponse response = client.prepareMultiGet()
                .add("index1", "blog", "8", "10")
                .add("lib3", "user", "1", "2", "3")
                .get();
        for (MultiGetItemResponse itrm : response){
            GetResponse gr = itrm.getResponse();
            if (gr != null && gr.isExists()){
                System.out.println(gr.getSourceAsString());
            }

        }
    }

    //bulk批量操作可以增删改   这里是添加
    @Test
    public void test7() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        BulkRequestBuilder bulk = client.prepareBulk();

        //批量添加
        bulk.add(client.prepareIndex("lib2", "books", "8")
                .setSource(
                        XContentFactory.jsonBuilder().startObject()
                                .field("titie", "python")
                                .field("price", 99)
                                .endObject()
                )
        );
        bulk.add(client.prepareIndex("lib2", "books", "9")
                .setSource(
                        XContentFactory.jsonBuilder().startObject()
                                .field("titie", "VR")
                                .field("price", 29)
                                .endObject()
                )
        );

        BulkResponse response = bulk.get();
        System.out.println(response.status());

        if (response.hasFailures()){
            System.out.println("失败了");
        }
    }

    //查询删除
    //查询出满足条件的文档之后删除
    @Test
    public void test8() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        BulkByScrollResponse response = DeleteByQueryAction.INSTANCE
                .newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("title", "工厂"))
                .source("index1")
                .get();

        long counts = response.getDeleted();
        System.out.println(counts);


    }
    //查询所有
    //match_all
    @Test
    public void test9() throws Exception {
        //指定ES集群
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();

        //创建访问ES服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.123.101"),9300));

        QueryBuilder qb = QueryBuilders.matchAllQuery();
        SearchResponse sr = client.prepareSearch("lib3")
                .setQuery(qb)
                .setSize(3)
                .get();

        SearchHits hits = sr.getHits();

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

            Map map = hit.getSourceAsMap();

            for (String key : map.keySet()){
                System.out.println(key +"= " +map.get(key));
            }

        }
    }

 

你可能感兴趣的:(ElasticSearch)