java api操作es

获取es客户端


private static TransportClient getClient() throws UnknownHostException {
        // 指定es集群名称
        Settings set = Settings.builder().put("cluster.name", "my-elasticsearch").build();
        // 创建访问es集群的客户端
        TransportClient client = new PreBuiltTransportClient(set)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.*.*.*"), 9300));
        return client;
}

从es中查询数据


public void testQuery() throws UnknownHostException {
        //hutool工具类
        TimeInterval timer = DateUtil.timer();
        TransportClient client = getClient();
        GetResponse response = client.prepareGet("hardware", "equipment", "160009").execute().actionGet();
        System.out.println("花费时间:"+timer.interval());
        System.out.println(response.getSourceAsString());
        client.close();
}

插入数据到es

public void testInsert() throws IOException {
        TransportClient client = getClient();
        XContentBuilder doc = XContentFactory.jsonBuilder().startObject()
                .field("id", "aa")
                .field("name", "摄像头")
                .field("createTime", new Date())
                .field("updateTime", new Date())
                .endObject();
        IndexResponse response = client.prepareIndex("hardware", "equipment", "160009").setSource(doc).get();
        System.out.println(response.status());
}

es在原有文档上更新文档

public void testUpdate() throws IOException, ExecutionException, InterruptedException {
        TransportClient client = getClient();
        UpdateRequest request = new UpdateRequest();
        request.index("hardware").type("equipment").id("160009")
                .doc(XContentFactory.jsonBuilder().startObject().field("name", "campera")
                        .field("peopleIn", 20).field("peopleOut", 10).endObject());
        UpdateResponse response = client.update(request).get();
        System.out.println(response.status());
}

es更新文档

public void testUpdate2() throws IOException, ExecutionException, InterruptedException {
        TransportClient client = getClient();
        IndexRequest request = new IndexRequest("hardware", "equipment", "160009")
                .source(XContentFactory.jsonBuilder().startObject()
                        .field("name", "campera").field("id", "aa")
                        .field("desc", "摄像头").endObject());
        UpdateRequest request2 = new UpdateRequest("hardware", "equipment", "160009")
                .doc(XContentFactory.jsonBuilder().startObject().field("id", "160009")
                        .field("name", "what is this").endObject()).upsert(request);
        UpdateResponse response = client.update(request2).get();
        System.out.println(response.status());
        client.close();
}

删除文档

public void testDelete() throws UnknownHostException {
        TransportClient client = getClient();
        DeleteResponse response = client.prepareDelete("hardware", "equipment", "150009").get();
        System.out.println(response.status());
        client.close();
}

multiGet批量查询

public void testMultiGet() throws UnknownHostException {
        TransportClient client = getClient();
        MultiGetResponse responses =
                client.prepareMultiGet().add("hardware", "equipment", "1008", "1009", "1010")
                        .add("hardware", "equipment", "1011", "1012", "1013")
                        .get();
        for (MultiGetItemResponse item : responses) {
            if (null != item.getResponse()) {
                System.out.println(item.getResponse().getSourceAsString());
            }
        }
}

bulk批量操作

public void tetstBulk() throws IOException {
        TransportClient client = getClient();
        BulkRequestBuilder builder = client.prepareBulk();
        builder.add(client.prepareUpdate("hardware", "equipment", "160009")
                .setDoc(XContentFactory.jsonBuilder().startObject().field("id", "15001")
                        .field("name", "what the fuck is this").endObject()));
        builder.add(client.prepareIndex("hardware", "equipment", "160012")
                .setSource(XContentFactory.jsonBuilder().startObject().field("id", "15069")
                        .field("name", "jjjjjjj").endObject()));
        builder.add(client.prepareDelete("hardware", "equipment", "160010"));
        BulkResponse responses = builder.get();
        System.out.println(responses.status());
}

匹配所有

public void testMatchAll() throws UnknownHostException {
        TransportClient client = getClient();
        MatchAllQueryBuilder allQuery = QueryBuilders.matchAllQuery();
        SearchResponse response = client.prepareSearch("hardware").
                setTypes("equipment").setQuery(allQuery)
                //默认查询10条
                .setFrom(0).setSize(10000)
                .get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
            Map<String, Object> map = hit.getSourceAsMap();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue().toString());
            }
        }
        //所有记录条数
        System.out.println(response.getHits().getTotalHits());
}

条件查询

public void testMatch() throws UnknownHostException {
        TransportClient client = getClient();
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", "星期一");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(matchQuery)
                .setSize(10).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }

一个值多个字段去匹配

 public void testMultiMatch() throws UnknownHostException {
        TransportClient client = getClient();
        //入参前面是值,右面是字段
        MultiMatchQueryBuilder matchQuery = QueryBuilders.multiMatchQuery("马云", "name", "id");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(matchQuery)
                .setSize(20).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
}

准确查询term

 public void testTerm() throws UnknownHostException {
        TransportClient client = getClient();
        TermQueryBuilder termQuery = QueryBuilders.termQuery("peopleIn", 800);
        //查询名字没有显示  TODO
//        TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "机械石膏板机械水杯装配全能后宫");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(termQuery)
                .setSize(12).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
}

一个字段多个值terms查询

 public void testTerms() throws UnknownHostException {
        TransportClient client = getClient();
        TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("id", "248", "300");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(termsQuery)
                .setSize(30).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
}

各种查询

 public void testManyQuery() throws UnknownHostException, ParseException {
        TransportClient client = getClient();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = sdf.parse("2017-1-1 00:00:00");
        Date createTime2 = sdf.parse("2017-1-9 00:00:00");
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("createTime").from(createTime).to(createTime2);
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("peopleIn").from(100).to(150);
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(rangeQuery).get();
        //前缀匹配
        PrefixQueryBuilder query = QueryBuilders.prefixQuery("id", "5");

        //模糊(通配符)查询
        WildcardQueryBuilder query = QueryBuilders.wildcardQuery("id", "*5");
        //模糊查询(
        FuzzyQueryBuilder query = QueryBuilders.fuzzyQuery("id", "6");
        // 类型查询type
        TypeQueryBuilder query = QueryBuilders.typeQuery("id");
        // ids查询
        IdsQueryBuilder query = QueryBuilders.idsQuery("id").addIds("2120", "3600");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(query).setSize(18).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
}

聚合查询

  public void testAggregation() throws UnknownHostException {
        TransportClient client = getClient();
        //查询最大值
        MaxAggregationBuilder builder = AggregationBuilders.max("max").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(builder).get();
        Max valueCount = response.getAggregations().get("max");
        double value = valueCount.getValue();

        //查询最小值
        MinAggregationBuilder builder = AggregationBuilders.min("min").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(builder).get();
        Min valueCount = response.getAggregations().get("min");
        double value = valueCount.getValue();

        //查询总和
        SumAggregationBuilder builder = AggregationBuilders.sum("sum").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(builder).get();
        Sum valueCount = response.getAggregations().get("sum");
        double value = valueCount.getValue();

        //查询平均值
        AvgAggregationBuilder builder = AggregationBuilders.avg("avg").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(builder).get();
        Avg valueCount = response.getAggregations().get("avg");
        double value = valueCount.getValue();

        //基数
        CardinalityAggregationBuilder builder = AggregationBuilders.cardinality("aaa").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("hardware").addAggregation(builder).get();
        Cardinality valueCount = response.getAggregations().get("aaa");
        double value = valueCount.getValue();

        //计数
        AggregationBuilder builder = AggregationBuilders.count("nameCount").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(builder).get();
        ValueCount valueCount = response.getAggregations().get("nameCount");
        long value = valueCount.getValue();
        System.out.println(value);
}

统计样本基本指标

public void testBasic() throws UnknownHostException {
        TransportClient client = getClient();
        AggregationBuilder termsBuilder = AggregationBuilders.stats("stats").field("peopleIn");
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(termsBuilder).get();
        //import org.elasticsearch.search.aggregations.metrics.stats.Stats;
        Stats valueCount = response.getAggregations().get("stats");
        System.out.println("max :" + valueCount.getMaxAsString());
        System.out.println("min " + valueCount.getMinAsString());
        System.out.println("avg " + valueCount.getAvgAsString());
        System.out.println("sum " + valueCount.getSumAsString());
        System.out.println("count :" + valueCount.getCount());
}

分组求数据

 public void testGroup() throws UnknownHostException {
        TransportClient client = getClient();
        TermsAggregationBuilder termsBuilder = AggregationBuilders.terms("by_people_in").field("peopleIn");
        AggregationBuilder sumBuilder = AggregationBuilders.sum("peopleInSum").field("peopleIn");
        AggregationBuilder avgBuilder = AggregationBuilders.avg("peopleInAvg").field("peopleIn");
        AggregationBuilder countBuilder = AggregationBuilders.count("peopleInCount").field("peopleIn");
        termsBuilder.subAggregation(sumBuilder).subAggregation(avgBuilder).subAggregation(countBuilder);
        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").addAggregation(termsBuilder).get();
        Aggregations terms = response.getAggregations();
        for (Aggregation a : terms) {
            LongTerms teamSum = (LongTerms) a;
            for (LongTerms.Bucket bucket : teamSum.getBuckets()) {
                System.out.println(bucket.getKeyAsString() + "   " + bucket.getDocCount() + "    " + ((Sum) bucket.getAggregations().asMap()
                        .get("peopleInSum")).getValue() + "    " + ((Avg) bucket.getAggregations().asMap().get("peopleInAvg")).getValue() + "    "
                        + ((ValueCount) bucket.getAggregations().asMap().get("peopleInCount")).getValue());
            }
        }
}

组合查询

public void testMuchQuery() throws UnknownHostException {
        TransportClient client = getClient();
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("peopleIn", 500))
                .mustNot(QueryBuilders.matchQuery("peopleOut", 500))
                .should(QueryBuilders.matchQuery("name", "马云"))
                .filter(QueryBuilders.rangeQuery("createTime").from("2017-01-01").to("2017-01-03").format("yyyy-MM-dd"));

        SearchResponse response = client.prepareSearch("hardware").setTypes("equipment").setQuery(queryBuilder).get();
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
}

一段时间内每月统计的最大,最小,总和,平均值

public void testGroup4() throws Exception {
        TimeInterval timer = DateUtil.timer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time1 = sdf.parse("2017-01-01 08:00:00");
        Date time2 = sdf.parse("2017-12-31 23:59:59");
        TransportClient client = getClient();
        AggregationBuilder dateHistogram
                = AggregationBuilders.dateHistogram("agg").field("createTime").dateHistogramInterval(DateHistogramInterval.MONTH);
        StatsAggregationBuilder stats = AggregationBuilders.stats("stats").field("peopleIn");
        RangeQueryBuilder builder = QueryBuilders.rangeQuery("createTime").from(time1.getTime()).to(time2.getTime());
        dateHistogram.subAggregation(stats);
        SearchResponse response =
                client.prepareSearch("hardware").setTypes("equipment").setQuery(builder).addAggregation(dateHistogram).get();
        Histogram timeAgg = response.getAggregations().get("agg");
        for (Histogram.Bucket entry : timeAgg.getBuckets()) {
            System.out.println(entry.getKey() + "-----" + entry.getDocCount());
            Aggregation aggregation = entry.getAggregations().get("stats");
            Entity entity = new Gson().fromJson(aggregation.toString(), Entity.class);
            System.out.println("max         "+entity.getStats().getMax());
            System.out.println("min         "+entity.getStats().getMin());
            System.out.println("avg         "+entity.getStats().getAvg());
            System.out.println("sum         "+entity.getStats().getSum());
            System.out.println("count       "+entity.getStats().getCount());
        }

        System.out.println("花费时间:"+timer.interval());
    }
}

  转载请注明: jacky's blog java api操作es

 上一篇
emq集群搭建 emq集群搭建
mqtt基础1.服务器集群搭建1.常见mqtt服务器常见mqtt服务端有emq,mosquitto等,我选用的是emq3.2.0版本. 2.下载文件下载zip文件,地址:https://www.emqx.io/cn/downloads 3
2019-07-30
下一篇 
kibana搭建 kibana搭建
具体步骤 创建kibana目录mkdir /usr/local/kibana 进入kibana的yml文件,修改 server.host 和 elasticsearch.url 的配置,分别指定 kibana 和 elasticsea
2019-04-03
  目录