赞
踩
public class EsTest_client {
//ES测试代码
public static void main(String[] args) throws Exception {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
//连接地址,接口,连接方式
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//关闭Es客户端
esClient.close();
}
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); CreateIndexRequest user = new CreateIndexRequest("user"); CreateIndexResponse response = esClient.indices().create(user, RequestOptions.DEFAULT); //响应状态 boolean acknowledged = response.isAcknowledged(); System.out.println("索引操作,"+ acknowledged); //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
//连接地址,接口,连接方式
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetIndexRequest user = new GetIndexRequest("user");
//获取目录数据
GetIndexResponse getIndexResponse = esClient.indices().get(user, RequestOptions.DEFAULT);
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
System.out.println(getIndexResponse.getAliases());
//关闭Es客户端
esClient.close();
}
public static void main(String[] args) throws Exception {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
//连接地址,接口,连接方式
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
DeleteIndexRequest user = new DeleteIndexRequest("user");
//删除索引
AcknowledgedResponse delete = esClient.indices().delete(user, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
//关闭Es客户端
esClient.close();
}
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); //操作插入的index IndexRequest request = new IndexRequest("user"); //设置id request.id("1001"); User user1 = new User(); user1.setName("张三"); user1.setAge(20); user1.setAddress("中国"); //将插入对象JSON化 ObjectMapper objectMapper = new ObjectMapper(); String userJson = objectMapper.writeValueAsString(user1); //指定插入数据的类型 request.source(userJson, XContentType.JSON); //插入数据 IndexResponse index = esClient.index(request, RequestOptions.DEFAULT); System.out.println(index.getResult()); //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
//连接地址,接口,连接方式
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetRequest getRequest = new GetRequest();
GetRequest user = getRequest.index("user").id("1001");
GetResponse response = esClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.getSourceAsString());
//关闭Es客户端
esClient.close();
}
public static void main(String[] args) throws Exception {
//创建ES客户端
RestHighLevelClient esClient = new RestHighLevelClient(
//连接地址,接口,连接方式
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").id("1001").doc(XContentType.JSON,"name","李四");
UpdateResponse update = esClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getGetResult());
//关闭Es客户端
esClient.close();
}
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.index("user").id("1001"); DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete.getResult()); //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); // 批量插入 BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","zhangsan","address","河北","age","18")); bulkRequest.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","address","河南","age","19")); bulkRequest.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu","address","山东","age","17")); bulkRequest.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","zhaoliu1","address","山西","age","18")); bulkRequest.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","zhaoliu2","address","江苏","age","18")); bulkRequest.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","zhaoliu3","address","天津","age","18")); bulkRequest.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON,"name","zhaoliu4","address","北京","age","18")); BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.getItems()); System.out.println(bulk.getTook()); //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); // 批量删除 BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new DeleteRequest().index("user").id("1001")); bulkRequest.add(new DeleteRequest().index("user").id("1002")); bulkRequest.add(new DeleteRequest().index("user").id("1003")); bulkRequest.add(new DeleteRequest().index("user").id("1004")); BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.getItems()); System.out.println(bulk.getTook()); //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //分页查询 SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); //分页 query.from(0); query.size(2); //配置条件 SearchRequest source = searchRequest.source(query); //进行查询 SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); // 条件构建的类 SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); query.sort("age", SortOrder.DESC); //配置条件 SearchRequest source = searchRequest.source(query); //进行查询 SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //构建查询对象 SearchSourceBuilder builder = new SearchSourceBuilder(); // 针对哪一个字段进行模糊查询(name),查询的值是 zhaoliu,模糊查询的数量,相差一个都能进行匹配得到Fuzziness.ONE FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "zhaoliu").fuzziness(Fuzziness.ONE); builder.query(fuzziness); searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //构建查询对象 SearchSourceBuilder builder = new SearchSourceBuilder(); AggregationBuilder aggregationBuilder = AggregationBuilders.max("MaxAge").field("age"); builder.aggregation(aggregationBuilder); searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //构建查询对象 SearchSourceBuilder builder = new SearchSourceBuilder(); TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan"); HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.preTags("<font color='red'>"); highlightBuilder.postTags("</font>"); highlightBuilder.field("name"); builder.highlighter(highlightBuilder); builder.query(termsQueryBuilder); searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
//ES测试代码 分组查询 public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //构建查询对象 SearchSourceBuilder builder = new SearchSourceBuilder(); TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("ageFroup").field("age"); builder.aggregation(termsAggregationBuilder); searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); } ``` ### 条件查询 ```java //ES测试代码 条件查询 public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //条件查询 termQuery searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",18))); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
//ES测试代码 字段过滤 public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //条件查询 termQuery SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); // 包含 和 不包含 String[] include = {}; // 查询出来的结果就不会包含age字段 String[] exInclude = {"age"}; builder.fetchSource(include,exInclude); SearchRequest source = searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
//ES测试代码 组合查询 public static void main(String[] args) throws Exception { //创建ES客户端 RestHighLevelClient esClient = new RestHighLevelClient( //连接地址,接口,连接方式 RestClient.builder(new HttpHost("localhost",9200,"http")) ); SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("user"); //条件查询 termQuery SearchSourceBuilder builder = new SearchSourceBuilder(); // BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); // boolQueryBuilder.must(QueryBuilders.matchQuery("age",18)); // boolQueryBuilder.must(QueryBuilders.matchQuery("name","zhaoliu1")); //范围查询 RangeQueryBuilder age = QueryBuilders.rangeQuery("age"); age.gte(10); age.lte(18); builder.query(age); searchRequest.source(builder); SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits(); System.out.println(hits.getHits()); System.out.println(search.getTook()); for (SearchHit hit : hits){ System.out.println(hit.getSourceAsString()); } //关闭Es客户端 esClient.close(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。