如需ElasticSearch整合postman基本操作请点击如下链接:
https://blog.csdn.net/m0_61228138/article/details/122862929
//创建ES客户端RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));//创建索引CreateIndexRequest request = new CreateIndexRequest("user");CreateIndexResponse createIndexResponse = esClient.indices().create(request,RequestOptions.DEFAULT);//响应状态boolean acknowledged = createIndexResponse.isAcknowledged();//查询索引GetIndexRequest request = new GetIndexRequest("user");GetIndexResponse getIndexResponse = esclient.indices().get(request,RequestOptions.DEFAULT);//响应状态boolean acknowledged = getIndexResponse.getAliases();//getMappings(),getSettings()//删除索引DeleteIndexRequest request = new DeleteIndexRequest("user");AcknowledgedResponse response = esclient.indices().delete(request,RequestOptions.DEFAULT);//响应状态boolean acknowledged = response.isAcknowledged();//插入数据IndexRequest request = new IndexRequest();request.index("user").id("1001");User user = new User("zhangsan","男",20);//向ES插入数据,必须将数据转换为JSON格式ObjectMapper mapper = new ObjectMapper();String userJson = mapper.writevalueAsString(user);request.source(userJson,XcontentType.JSON);IndexResponse response = esClient.index(request,RequestOptions.DEFAULT);response.getResult();//修改数据UpdateRequest request = new UpdateRequest();request.index("user").id("1001");request.doc(XContentType.JSON,"sex","女")UpdateResponse response = esClient.update(request,RequestOptions.DEFAULT);//查询数据GetRequest request = new GetRequest();request.index("user").id("1001");GetResponse response = esClient.get(request,RequestOptions.DEFAULT);response.getSourceAsString();//删除数据DeleteRequest request = new DeleteRequest();request.index("user").id("1001");DeleteResponse response = esClient.delete(request,RequestOptions.DEFAULT)response.toString();//批量插入数据BulkRequest request = new BulkRequest();request.add(new IndexRequest().index("user").id("1001").source(XCntentType.JSON,"name","zhangsan"));BulkResponse response = esClient.bulk(request,RequestOptions.DEFAULT)response.getTook();//getItems()//高级查询,查询索引中全部数据SearchRequest request = new SearchRequest();request.indices("user");request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));SearchResponse response = esClient.search(request,RequestOptions.DEFAULT);response.getHits();//条件查询termQuerySearchRequest request = new SearchRequest();request.indices("user");request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//分页查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());builder.from(0);builder.size(2);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//排序SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());builder.sort("age",SortOrder.DESC);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//过滤字段SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());String[] excludes = {"age"};String[] includes = {}builder.fetchSource(includes,excludes);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//组合查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();builder.query(boolQueryBuilder);boolQueryBuilder.must(QueryBuilders.matchQuery("age",30));boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","男"));boolQueryBuilder.should(QueryBuilders.matchQuery("age",30));boolQueryBuilder.should(QueryBuilders.matchQuery("age",40));request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//范围查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");rangeQuery.gte(30);rangeQuery.lte(40);builder.query(rangeQuery);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//模糊查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();builder.query(QueryBuilders.fuzzyQuery("name","wangwu").fuzziness(Fuzziness.ONE));request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//高亮查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();TermsQueryBuilder termsQueryBuilder = QueryBuilders.termQuery("name","zhangsan");HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.preTags("");highlightBuilder.field("name");highlightBuilder.postTags("");builder.highlighter(highlightBuilder);builder.query(termsQueryBuilder)request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//聚合查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");builder.aggregation(aggregationBuilder);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//分组查询SearchRequest request = new SearchRequest();request.indices("user");SearchSourceBuilder builder = new SearchSourceBuilder();AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");builder.aggregation(aggregationBuilder);request.source(builder);SearchResponse response = esClient.search(request,RequestOption.DEFAULT);response.getHits();//关闭ES客户端esClient.close