Bootstrap

使用Java Api 查询ElasticSearch 索引 条件查询

查询 索引 user  中 年龄 为 30 岁的信息

public class ESTest_Doc_Query {
    public static void main(String[] args) throws Exception {
        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        //2、条件查询 termQuery
        SearchRequest request = new SearchRequest();
        request.indices("user");

        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
        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());
        }


        //关闭ES客户端
        esClient.close();
    }
}

;