@es学习入门API
学习此技术需要先熟悉es的基本语法,更佳!!!
官网文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
1、下载ES软件包
es下载地址
ElasticSearch: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D kibana: https://mirrors.huaweicloud.com/kibana/?C=N&O=D
2、配置es的核心开发依赖
org.elasticsearch elasticsearch7.8.0 org.elasticsearch.client elasticsearch-rest-high-level-client7.8.0 org.apache.logging.log4j log4j-api2.8.2 org.apache.logging.log4j log4j-core2.8.2 com.fasterxml.jackson.core jackson-databind2.8.2 junit junit4.12 test
3、入门核心API
//创建ES客户端 RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) ); //创建索引 // GetIndexResponse users = restHighLevelClient.indices().create(new GetIndexRequest("users"), RequestOptions.DEFAULT); //查询索引 // GetIndexResponse users = restHighLevelClient.indices().get(new GetIndexRequest("users"), RequestOptions.DEFAULT); // 删除索引 // AcknowledgedResponse users1 = restHighLevelClient.indices().delete(new DeleteIndexRequest("users"), RequestOptions.DEFAULT); //批量插入数据 bulk // BulkResponse response=null; // for (int i = 0; i < n; i++) { // Users users1 = new Users(); // users1.setName(UUID.randomUUID().toString().substring(1,4)); // users1.setAge(new Random().nextInt(100)); // users1.setSex(Integer.toString(new Random().nextInt(2))); // users1.setBalance(new BigDecimal(1124)); // String jsonUsers = new ObjectMapper().writevalueAsString(users1); // response = restHighLevelClient.bulk(new BulkRequest().add(new IndexRequest("users").id(Integer.toString(new Random().nextInt(1000))).source(jsonUsers, XContentType.JSON)), RequestOptions.DEFAULT); // } //批量删除 bulk // BulkResponse response=restHighLevelClient.bulk(new BulkRequest().add(new DeleteRequest("users").id("487")),RequestOptions.DEFAULT); **注:**:这里的id值可以使用以下查询API的查询的数据的id值进行删除,这里我进行写死为了熟悉API //全量查询数据 // SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())),RequestOptions.DEFAULT); //条件查询 // SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name","xxx"))),RequestOptions.DEFAULT); //组合查询 // BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); // boolQueryBuilder.should(QueryBuilders.matchQuery("name","xxx")); // boolQueryBuilder.must(QueryBuilders.matchQuery("age","89")); // boolQueryBuilder.must(QueryBuilders.matchQuery("age","65")); //范围查询 // RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age"); // rangeQuery.gt(80); // rangeQuery.lte(90); // rangeQuery.from(); //高亮查询 highlight TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "xxx"); //分组查询 SearchSourceBuilder builder = new SearchSourceBuilder(); //对name字段:进行高亮查询显示 HighlightBuilder highlightBuilder = new HighlightBuilder().preTags("").postTags("").field("name"); builder.highlighter(highlightBuilder); //分页规则:(当前页码-1)*每页显示数据条数 分页查询 // builder.from(2); // builder.size(5); //排序 // builder.sort("age", SortOrder.DESC); //过滤字段 // builder.fetchSource("name","balance"); // builder.fetchSource(new String[]{"name","age"},new String[]{"sex","balance"}); //对某个字段进行聚合 *** 作 AggregationBuilders builder.aggregation(AggregationBuilders.avg("avgBal").field("balance")); builder.query(termQuery); SearchResponse response = restHighLevelClient.search(new SearchRequest().indices("users").source(builder),RequestOptions.DEFAULT); List list = new ArrayList<>(); System.out.println(response.getHits().getTotalHits()); for (SearchHit hit : response.getHits()) { ConcurrentHashMapmap = new ConcurrentHashMap<>(); System.out.println(hit.getSourceAsString()); map.put("index", hit.getIndex()); map.put("fields", hit.getFields()); map.put("id", hit.getId()); map.put("source", hit.getSourceAsString()); map.put("score", hit.getScore()); map.put("type", hit.getType()); map.put("highlightFields", hit.getHighlightFields()); list.add(map); } System.out.println(list); System.out.println(list.size()); System.out.println(response); System.out.println(response.getHits()); System.out.println(response.getTook()); //关闭es客户端 restHighLevelClient.close();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)