es的bool,排序,聚合组合查询

传入的JSONObject:
{
    "列名1": "ETR",
    "列名2": "2226",
    "列名3": "INDAS",
    "列名4":"*材料*",  //通配符查询
    "range":  //范围查询指定key
        {
            "@timestamp":
            {
              "gt": "2018-08-06 19:17:56",
              "lt": "2018-09-06 19:17:26"
            }
        },
    "sort":  //排序指定key
        {"@timestamp":"desc","列名5":"desc"},
    "aggs":  //聚合指定key(sql的count()操作)
    "列名6"
}
    //获取连接,clusterName:集群名,hostName:ip,port:端口号(9300)
    public TransportClient init(){
        Settings seeting  = Settings.builder().put("cluster.name",clusterName)
                .put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
                .build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(seeting).addTransportAddress(new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port)));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }
    //多条件bool精准通配符查询
    public JSON boolQuery(TransportClient client,JSONObject body){
        //eportmonitor-2018-08是索引,doc是类型
        SearchRequestBuilder response = client.prepareSearch("eportmonitor-2018-08").setTypes("doc");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        Map map = body.getInnerMap();
        Iterator.Entry> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = entries.next();
            switch (entry.getKey()){
                case "range":
                    Map rangeMap = (Map) entry.getValue();
                    Iterator.Entry> rangeEntries = rangeMap.entrySet().iterator();
                    while (rangeEntries.hasNext()){
                        Map.Entry rangeEntry = rangeEntries.next();
                        Map valueMap = (Map) rangeEntry.getValue();
                        //对于非时间类型的字段防止分析器进行分析加上.keyword
                        if(rangeEntry.getKey().equals("@timestamp")){
                            boolQueryBuilder.must(QueryBuilders.rangeQuery(rangeEntry.getKey()).gt(valueMap.get("gt").toString()).lt(valueMap.get("lt").toString()));
                        }else{
                            boolQueryBuilder.must(QueryBuilders.rangeQuery(rangeEntry.getKey()+".keyword").gt(valueMap.get("gt").toString()).lt(valueMap.get("lt").toString()));
                        }
                    }
                    break;
                case "sort":
                    Map sortMap = (Map) entry.getValue();
                    Iterator.Entry> sortEntries = sortMap.entrySet().iterator();
                    while (sortEntries.hasNext()){
                        Map.Entry sortEntry = sortEntries.next();
                        if(sortEntry.getKey().equals("@timestamp")){
                            if(sortEntry.getValue().toString().equals("desc")){
                                response.addSort(sortEntry.getKey(),SortOrder.DESC);
                            }else {
                                response.addSort(sortEntry.getKey(),SortOrder.ASC);
                            }
                        }else {
                            if(sortEntry.getValue().toString().equals("desc")){
                                response.addSort(sortEntry.getKey()+".keyword",SortOrder.DESC);
                            }else {
                                response.addSort(sortEntry.getKey()+".keyword",SortOrder.ASC);
                            }
                        }
                    }
                    break;
                case "aggs":
                   if(entry.getValue().toString().equals("@timestamp")){
                       TermsAggregationBuilder IEAgg = AggregationBuilders.terms("aggOne").field(entry.getValue().toString());
                       response.addAggregation(IEAgg);
                   }else {
                       TermsAggregationBuilder IEAgg = AggregationBuilders.terms("aggOne").field(entry.getValue().toString()+".keyword");
                       response.addAggregation(IEAgg);
                   }
                    break;
                default:
                    //这里的模糊查询,做成了bool查询的通配符wildcard查询
                    if (entry.getValue().toString().indexOf("*")!=-1){
                        boolQueryBuilder.must(QueryBuilders.wildcardQuery(entry.getKey()+".keyword",entry.getValue().toString()));
                    }else {
                        if(entry.getKey().equals("@timestamp")){
                            boolQueryBuilder.must(QueryBuilders.termQuery(entry.getKey(),entry.getValue().toString()));
                        }else {
                            boolQueryBuilder.must(QueryBuilders.termQuery(entry.getKey()+".keyword",entry.getValue().toString()));
                        }
                    }
                    break;
            }
        }
        SearchResponse searchResponse =null;
        //排序的字段如果数据库不存在会报错
        try{
            searchResponse = response.setQuery(boolQueryBuilder).setFrom(0).setSize(10).execute().actionGet();
        }catch (Exception e){
            System.out.println(e.toString());
            return (JSON) JSON.parse("{\"error\":\"输入有误,请检查输入字段是否正确\"}");
        }
        return (JSON) JSON.parse(searchResponse.toString());
     }

你可能感兴趣的:(elasticsearch)