ElasticSearch的JavaAPI入门2

/**
 * 模糊查询,自动纠正拼写错误单词,最大纠正次数是两次
 * 查询  say   helol
 */
@Test
public void fuzzyQuery(){
    //通过模糊查询,查询helol这个单词
    FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("say", "helol").fuzziness(Fuzziness.TWO);
    SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(fuzziness).get();
    SearchHits hits = searchResponse.getHits();
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit searchHit : hits1) {
        System.out.println(searchHit.getId());
        System.out.println(searchHit.getSourceAsString());
    }
}


/**
 * 通配符查询
 *  *  表示匹配任意多个字符
 *  ?  表示匹配一个字符
 */
@Test
public void  wildCardQuery(){
    WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("say", "hell?");
    SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(wildcardQuery).get();

    SearchHits hits = searchResponse.getHits();
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit searchHit : hits1) {
        System.out.println(searchHit.getId());
        System.out.println(searchHit.getSourceAsString());
    }

}


/**
 * 使用booleanQuery实现多条件组合查询
 * 查询年龄是18到28范围内且性别是男性的,第一个条件  里面包含两部分  年龄和性别  且的关系
 * 或者id范围在10到13范围内的  第二个条件
 * 第一个条件与第二个条件是一个或的关系
 */
@Test
public void booleanQuery(){
    //年龄范围的条件
    RangeQueryBuilder age = QueryBuilders.rangeQuery("age").gt(17).lt(29);
    //性别的条件
    TermQueryBuilder sex = QueryBuilders.termQuery("sex", "1");
    //数据id的条件
    RangeQueryBuilder id = QueryBuilders.rangeQuery("id").gt("9").lt("14");
    //使用booleanQuery组合我们多个查询条件
    BoolQueryBuilder should = QueryBuilders.boolQuery()
            .should(id).should(QueryBuilders.boolQuery().must(sex).must(age));

    SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(
            should
    ).get();

    SearchHits hits = searchResponse.getHits();
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit searchHit : hits1) {
        System.out.println(searchHit.getId());
        System.out.println(searchHit.getSourceAsString());
    }


}


/**
 * 分页查询
 */
@Test
public void pageQuery(){

    int pageSize  = 5 ;
    int pageNum =2;

    //计算起始的数据
    int startNum = (pageNum - 1 ) * pageSize;

    SearchResponse searchResponse = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.matchAllQuery())
            .addSort("id", SortOrder.ASC)
            .setFrom(startNum)
            .setSize(pageSize)
            .get();

    SearchHits hits = searchResponse.getHits();
    SearchHit[] hits1 = hits.getHits();
    for (SearchHit searchHit : hits1) {
        System.out.println(searchHit.getId());
        System.out.println(searchHit.getSourceAsString());
    }


}


/**
 * 高亮显示,对我们查询出来的say   hello
 * 这个字段进行高亮显示
 */
@Test
public void highLighter(){

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("indexsearch").setTypes("mysearch").setQuery(QueryBuilders.termQuery("say", "hello"));
    //通过HighlightBuilder 来定义我们对哪个字段进行高亮显示,以及设置高亮的前缀和后缀
    HighlightBuilder highlightBuilder = new HighlightBuilder();
    highlightBuilder.field("say").preTags("").postTags("");
    //通过SearchRequestBuilder 来设置高亮显示
    SearchResponse searchResponse = searchRequestBuilder.highlighter(highlightBuilder).get();
    //获取高亮结果
    SearchHits hits = searchResponse.getHits();
    for (SearchHit hit : hits) {
        System.out.println(hit.getSourceAsString());
        Text[] says = hit.getHighlightFields().get("say").getFragments();
        for (Text say : says) {
            System.out.println(say);
        }
    }
}




/**
 * 批量添加数据
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void addIndexDatas() throws IOException, ExecutionException, InterruptedException {
    //获取settings
    //配置es集群的名字
    Settings settings = Settings.builder().put("cluster.name", "myes").build();
    //获取客户端
    TransportAddress transportAddress = new TransportAddress(InetAddress.getByName("node01"), 9300);

    TransportAddress transportAddress2 = new TransportAddress(InetAddress.getByName("node02"), 9300);

    TransportAddress transportAddress3 = new TransportAddress(InetAddress.getByName("node03"), 9300);
    //获取client客户端
    TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress).addTransportAddress(transportAddress2).addTransportAddress(transportAddress3);

    /**
     * 创建索引
     * */
    client.admin().indices().prepareCreate("player").get();
    //构建json的数据格式,创建映射
    XContentBuilder mappingBuilder = jsonBuilder()
            .startObject()
            .startObject("player")
            .startObject("properties")
            .startObject("name").field("type","text").field("index", "true").field("fielddata","true").endObject()
            .startObject("age").field("type","integer").endObject()
            .startObject("salary").field("type","integer").endObject()
            .startObject("team").field("type","text").field("index", "true").field("fielddata","true").endObject()
            .startObject("position").field("type","text").field("index", "true").field("fielddata","true").endObject()
            .endObject()
            .endObject()
            .endObject();
    PutMappingRequest request = Requests.putMappingRequest("player")
            .type("player")
            .source(mappingBuilder);
    client.admin().indices().putMapping(request).get();


    //批量添加数据开始

    BulkRequestBuilder bulkRequest = client.prepareBulk();

// either use client#prepare, or use Requests# to directly build index/delete requests
    bulkRequest.add(client.prepareIndex("player", "player", "1")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "郭德纲")
                    .field("age", 33)
                    .field("salary",3000)
                    .field("team" , "cav")
                    .field("position" , "sf")
                    .endObject()
            )
    );
    bulkRequest.add(client.prepareIndex("player", "player", "2")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "于谦")
                    .field("age", 25)
                    .field("salary",2000)
                    .field("team" , "cav")
                    .field("position" , "pg")
                    .endObject()
            )
    );
    bulkRequest.add(client.prepareIndex("player", "player", "3")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "岳云鹏")
                    .field("age", 29)
                    .field("salary",1000)
                    .field("team" , "war")
                    .field("position" , "pg")
                    .endObject()
            )
    );

    bulkRequest.add(client.prepareIndex("player", "player", "4")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "爱因斯坦")
                    .field("age", 21)
                    .field("salary",300)
                    .field("team" , "tim")
                    .field("position" , "sg")
                    .endObject()
            )
    );

    bulkRequest.add(client.prepareIndex("player", "player", "5")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "张云雷")
                    .field("age", 26)
                    .field("salary",2000)
                    .field("team" , "war")
                    .field("position" , "pf")
                    .endObject()
            )
    );
    bulkRequest.add(client.prepareIndex("player", "player", "6")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "爱迪生")
                    .field("age", 40)
                    .field("salary",1000)
                    .field("team" , "tim")
                    .field("position" , "pf")
                    .endObject()
            )
    );
    bulkRequest.add(client.prepareIndex("player", "player", "7")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "牛顿")
                    .field("age", 21)
                    .field("salary",500)
                    .field("team" , "tim")
                    .field("position" , "c")
                    .endObject()
            )
    );

    bulkRequest.add(client.prepareIndex("player", "player", "8")
            .setSource(jsonBuilder()
                    .startObject()
                    .field("name", "特斯拉")
                    .field("age", 20)
                    .field("salary",500)
                    .field("team" , "tim")
                    .field("position" , "sf")
                    .endObject()
            )
    );
    BulkResponse bulkResponse = bulkRequest.get();
    client.close();
}


/**
 * 统计每个球队当中有多少人
 */
@Test
public void countByTeam(){
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");

    //设置我们按照哪个字段进行分组
    TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");

    searchRequestBuilder.addAggregation(team);
    //调用get方法,触发请求真正的去执行
    SearchResponse searchResponse = searchRequestBuilder.get();

    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
        //将aggregation进行强转,转换成为 StringTerms
        StringTerms stringTerms = (StringTerms) aggregation;

        List buckets = stringTerms.getBuckets();

        for (StringTerms.Bucket bucket : buckets) {
            System.out.println(bucket.getKey());//获取聚合别名字段
            System.out.println(bucket.getDocCount());//统计每个球队当中一共有多少个球员
        }


    }


}


/**
 * 统计每个球队当中每个位置有多少人
 */

@Test
public  void countByTeamAndPosition(){

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");

    //设置我们team的聚合条件
    TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team");

    //设置我们位置的聚合条件
    TermsAggregationBuilder position = AggregationBuilders.terms("position_count").field("position");

    //关联两个聚合条件,team排前面
    team.subAggregation(position);

    //调用get方法,触发请求真正的去执行
    SearchResponse searchResponse = searchRequestBuilder.addAggregation(team).get();

    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
        StringTerms stringTerms = (StringTerms) aggregation;

        System.out.println("队伍聚合数据为" +  stringTerms.toString());


        List buckets = stringTerms.getBuckets();
        for (StringTerms.Bucket bucket : buckets) {
            //获取我们聚合的数据一共有多少条
            long docCount = bucket.getDocCount();
            System.out.println(docCount);

            //获取数据的key
            Object key = bucket.getKey();
            System.out.println("当前队伍名称为" +  key+"当前队伍当中一共有" +  docCount + "人");

            //求每个队伍当中,每个位置有多少人
            Aggregation position_count = bucket.getAggregations().get("position_count");

            StringTerms positionTerm = (StringTerms) position_count;
            System.out.println("按照位置进行分组 " +  positionTerm.toString());

            List buckets1 = positionTerm.getBuckets();
            for (StringTerms.Bucket bucket1 : buckets1) {
                Object key1 = bucket1.getKey();
                long docCount1 = bucket1.getDocCount();//获取数据一共有多少条
                System.out.println("该队伍下面位置为" +  key1 + "有" +  docCount1+"人");
            }
        }
    }
}


/**
 * 计算每个球队当中年龄最大的值
 */
@Test
public void maxAndMinAge(){

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");

    //按照球队进行分组
    TermsAggregationBuilder team = AggregationBuilders.terms("team_group").field("team");

    AvgAggregationBuilder age = AggregationBuilders.avg("avg_age").field("age");


    //求取每个球队当中年年龄最小值
   // MinAggregationBuilder age = AggregationBuilders.min("min_age").field("age");

    //对age字段取最大值
  //  MaxAggregationBuilder age = AggregationBuilders.max("max_age").field("age");
    //设置分组以及求最大值的关系
    team.subAggregation(age);

    SearchResponse searchResponse = searchRequestBuilder.addAggregation(team).get();
    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
        StringTerms stringTerms = (StringTerms) aggregation;
        System.out.println(stringTerms.toString());

        List buckets = stringTerms.getBuckets();
        //遍历每一个bucket,然后对数据获取最大的年龄值
        for (StringTerms.Bucket bucket : buckets) {
            //求每个球队当中年龄最大值
           // Aggregation max_age = bucket.getAggregations().get("max_age");
            //求每个球队当中年龄最小值
            //Aggregation min_age = bucket.getAggregations().get("min_age");


            //求每个球队当中年龄的平均值
            Aggregation avg_age = bucket.getAggregations().get("avg_age");



            System.out.println(avg_age.toString());
        }


    }

}




/**
 * 统计每个球队当中的球员平均年龄,以及队员总年薪
 */
@Test
public void avgAndSum(){
    SearchRequestBuilder builder = client.prepareSearch("player").setTypes("player");

    TermsAggregationBuilder team_group = AggregationBuilders.terms("team_group").field("team");

    AvgAggregationBuilder avg_age = AggregationBuilders.avg("avg_age").field("age");

    SumAggregationBuilder sumMoney = AggregationBuilders.sum("sum_money").field("salary");


    TermsAggregationBuilder termsAggregationBuilder = team_group.subAggregation(avg_age).subAggregation(sumMoney);

    SearchResponse searchResponse = builder.addAggregation(termsAggregationBuilder).get();
    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
        System.out.println(aggregation.toString());
    }


}


/**
 * 计算每个球队球员的平均年龄 age ,同时又要计算总年薪 salary
 *
 */
@Test
public void groupDemo(){

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");


    /**
     * 按照球队进行聚合
     */
    TermsAggregationBuilder team_group = AggregationBuilders.terms("team_group").field("team");

    /**
     * 平均年龄
     */
    AvgAggregationBuilder avg_age = AggregationBuilders.avg("avg_age").field("age");

    /**
     * 求取每个球队的总年薪
     */
    SumAggregationBuilder sumMoney = AggregationBuilders.sum("sum_money").field("salary");


    //组织我们的聚合条件
    team_group.subAggregation(avg_age).subAggregation(sumMoney);
    //获取到我们的查询结果值
    SearchResponse searchResponse = searchRequestBuilder.addAggregation(team_group).get();
//获取到聚合值
    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
       // StringTerms stringTerms = (StringTerms) aggregation;

        System.out.println(aggregation.toString());
    }


}


/**
 * 聚合排序
 * 计算每个球队的总年薪,然后按照年薪进行排序(降序)
 */
@Test
public void groupAndCount(){

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch("player").setTypes("player");

    //按照球队进行分组,()
 
    //指定聚合条件
    TermsAggregationBuilder team = AggregationBuilders.terms("player_count").field("team")
            .order(BucketOrder.aggregation("sum_salary", false));//false是降序,true是升序
    //获取每个球队的年薪的总和
    SumAggregationBuilder sumSalary = AggregationBuilders.sum("sum_salary").field("salary");

    team .subAggregation(sumSalary);
    //调用get方法,触发请求真正的去执行
    SearchResponse searchResponse = searchRequestBuilder.addAggregation(team).get();
    //获取我们的数据的聚合值
    Aggregations aggregations = searchResponse.getAggregations();
    for (Aggregation aggregation : aggregations) {
        System.out.println(aggregation.toString());
    }


}




@Test
public void esJdbc() throws SQLException {
    //es的数据源
    EsDataSource dataSource = new EsDataSource();
    //es连接字符串
    String address = "jdbc:es://http://node01:9200" ;

    dataSource.setUrl(address);
    Properties connectionProperties = new Properties();
    dataSource.setProperties(connectionProperties);
    //获取es连接
    Connection connection = dataSource.getConnection();
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from library");
    while(resultSet.next()){
        String string = resultSet.getString(0);
        String string1 = resultSet.getString(1);
        int anInt = resultSet.getInt(2);
        String string2 = resultSet.getString(4);
        System.out.println(string + "\t" +  string1 + "\t" +  anInt + "\t" + string2);
    }
    connection.close();
}

你可能感兴趣的:(大数据)