Elasticsearch 7.x入门学习-Java API操作

1 创建项目

在idea开发工具中创建Maven项目
在这里插入图片描述

修改 pom 文件,增加 Maven 依赖关系

<dependencies><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 的客户端 --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 依赖 2.x 的 log4j --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.9</version></dependency><!-- junit 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>

新建ClientTest类创建客户端对象测试是否能连接

public class ClientTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));System.out.println(client);client.close();}
}

备注:9200 端口为 Elasticsearch 的 Web 通信端口,localhost 为启动 ES 服务的主机名
执行代码,查看控制台信息,没有报错证明连接成功:
在这里插入图片描述

2 索引操作

通过 Java API 客户端对象对 ES 索引进行操作

2.1 创建索引

public class IndexTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));System.out.println(client);// 创建索引 - 请求对象CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");// 发送请求,获取响应CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);// 获取响应状态boolean acknowledged = createIndexResponse.isAcknowledged();System.out.println("操作状态 = " + acknowledged);client.close();}
}

执行结果:
在这里插入图片描述

2.2 查看索引

public class IndexTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));// 查询索引 - 请求对象GetIndexRequest request = new GetIndexRequest("user");// 发送请求,获取响应GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);System.out.println("aliases:" + response.getAliases());System.out.println("mappings:" + response.getMappings());System.out.println("settings:" + response.getSettings());client.close();}
}

执行结果:
在这里插入图片描述

2.3 删除索引

public class IndexTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");// 发送请求,获取响应AcknowledgedResponse deleteResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);System.out.println("操作状态 = " + deleteResponse.isAcknowledged());client.close();}
}

删除索引执行完成后再次查看索引会报错
在这里插入图片描述

3 文档操作

3.1 新增文档

创建数据模型实体类User

public class User {private String name;private Integer age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}

创建连接,创建数据添加到文档上,没有索引的话会自动创建索引并添加文档

public class DocumentTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));createDocument(client);System.out.println(client);client.close();}private static void createDocument(RestHighLevelClient client) throws IOException {// 新增文档 - 请求对象IndexRequest request = new IndexRequest();// 设置索引及唯一性标识request.index("user").id("1001");// 创建数据对象User user = new User();user.setName("zhangsan");user.setAge(30);user.setSex("男");ObjectMapper objectMapper = new ObjectMapper();String productJson = objectMapper.writeValueAsString(user);// 添加文档数据,数据格式为 JSON 格式request.source(productJson, XContentType.JSON);// 客户端发送请求,获取响应对象IndexResponse response = client.index(request, RequestOptions.DEFAULT);// 打印结果信息System.out.println("_index:" + response.getIndex());System.out.println("_id:" + response.getId());System.out.println("_result:" + response.getResult());}
}

执行结果:
在这里插入图片描述

3.2 查询文档

根据id查询文档示例

public class DocumentTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));searchDocument(client);client.close();}private static void searchDocument(RestHighLevelClient client) throws IOException {//1.创建请求对象GetRequest request = new GetRequest().index("user").id("1001");//2.客户端发送请求,获取响应对象GetResponse response = client.get(request, RequestOptions.DEFAULT);//3.打印结果信息System.out.println("_index:" + response.getIndex());System.out.println("_type:" + response.getType());System.out.println("_id:" + response.getId());System.out.println("source:" + response.getSourceAsString());}
}

执行结果:
在这里插入图片描述

3.3 修改文档

根据id将sex更新为“女”代码示例:

public class DocumentTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));updateDocument(client);client.close();}private static void searchDocument(RestHighLevelClient client) throws IOException {//1.创建请求对象GetRequest request = new GetRequest().index("user").id("1001");//2.客户端发送请求,获取响应对象GetResponse response = client.get(request, RequestOptions.DEFAULT);//3.打印结果信息System.out.println("_index:" + response.getIndex());System.out.println("_type:" + response.getType());System.out.println("_id:" + response.getId());System.out.println("source:" + response.getSourceAsString());}private static void updateDocument(RestHighLevelClient client) throws IOException {searchDocument(client);UpdateRequest updateRequest = new UpdateRequest();updateRequest.index("user").id("1001");updateRequest.doc(XContentType.JSON, "sex", "女");UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);System.out.println("_index:" + response.getIndex());System.out.println("_id:" + response.getId());System.out.println("_result:" + response.getResult());searchDocument(client);}
}

执行结果:
在这里插入图片描述

3.4 删除文档

根据id删除文档

public class DocumentTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));deleteDocument(client);client.close();}private static void deleteDocument(RestHighLevelClient client) throws IOException {//查看文档searchDocument(client);//删除文档DeleteRequest deleteRequest = new DeleteRequest().index("user").id("1001");DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println(deleteResponse.toString());//查看文档searchDocument(client);}private static void searchDocument(RestHighLevelClient client) throws IOException {//1.创建请求对象GetRequest request = new GetRequest().index("user").id("1001");//2.客户端发送请求,获取响应对象GetResponse response = client.get(request, RequestOptions.DEFAULT);//3.打印结果信息System.out.println("_index:" + response.getIndex());System.out.println("_type:" + response.getType());System.out.println("_id:" + response.getId());System.out.println("source:" + response.getSourceAsString());}
}

执行结果:
在这里插入图片描述

3.5 批量操作

public class BatchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));//创建批量新增请求对象BulkRequest request = new BulkRequest();request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "张三"));request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "李四"));request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "王五"));BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);//打印结果信息//打印结果信息System.out.println("took:" + bulkResponse.getTook());System.out.println("items:" + bulkResponse.getItems());client.close();}
}

执行结果:
在这里插入图片描述

批量删除操作
批量插入文档后再批量删除文档

public class BatchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));//创建批量新增请求对象BulkRequest request = new BulkRequest();request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "张三"));request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "李四"));request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "王五"));BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);//打印结果信息System.out.println("took:" + bulkResponse.getTook());System.out.println("items:" + bulkResponse.getItems());searchDocument(client);BulkRequest deleteRequest = new BulkRequest();deleteRequest.add(new DeleteRequest().index("user").id("1001"));deleteRequest.add(new DeleteRequest().index("user").id("1002"));BulkResponse deleteResponse = client.bulk(deleteRequest, RequestOptions.DEFAULT);//打印结果信息System.out.println("took:" + deleteResponse.getTook());System.out.println("items:" + deleteResponse.getItems());searchDocument(client);client.close();}private static void searchDocument(RestHighLevelClient client) throws IOException {//1.创建请求对象GetRequest getRequest = new GetRequest().index("user").id("1001");//2.客户端发送请求,获取响应对象GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);//3.打印结果信息System.out.println("_index:" + getResponse.getIndex());System.out.println("_type:" + getResponse.getType());System.out.println("_id:" + getResponse.getId());System.out.println("source:" + getResponse.getSourceAsString());GetRequest getRequest2 = new GetRequest().index("user").id("1002");//2.客户端发送请求,获取响应对象GetResponse getResponse2 = client.get(getRequest2, RequestOptions.DEFAULT);//3.打印结果信息System.out.println("_index:" + getResponse2.getIndex());System.out.println("_type:" + getResponse2.getType());System.out.println("_id:" + getResponse2.getId());System.out.println("source:" + getResponse2.getSourceAsString());}
}

执行结果:
在这里插入图片描述

4 高级查询

4.1 请求体查询

4.1.1 查询所有索引数据

查询所有索引数据,代码示例

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));searchAll(client);client.close();}private static void searchAll(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据sourceBuilder.query(QueryBuilders.matchAllQuery());request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}
}

查询结果:
在这里插入图片描述

4.1.2 term查询

term查询,查询条件为关键字,代码示例:

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));termQuery(client);client.close();}private static void termQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据sourceBuilder.query(QueryBuilders.termQuery("age", 30));request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}
}

执行结果:
在这里插入图片描述

4.1.3 分页查询
public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));pageQuery(client);client.close();}private static void pageQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据sourceBuilder.query(QueryBuilders.matchAllQuery());// 当前页其实索引(第一条数据的顺序号)sourceBuilder.from(2);//每页显示多少条sourceBuilder.size(2);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}
}

执行结果:在这里插入图片描述

4.1.2 数据排序

数据排序代码示例:

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));sort(client);client.close();}private static void sort(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据sourceBuilder.query(QueryBuilders.matchAllQuery());sourceBuilder.sort("age", SortOrder.ASC);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}}

执行结果
在这里插入图片描述

4.1.2 过滤字段

代码示例:

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));fetchSource(client);client.close();}private static void fetchSource(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据sourceBuilder.query(QueryBuilders.matchAllQuery());//查询字段过滤String[] excludes = {};String[] includes = {"name", "age"};sourceBuilder.fetchSource(includes, excludes);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}
}

执行结果:
在这里插入图片描述

4.1.2 Bool查询
public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/f61e9a465dba47b69caf62250b594da0.png)boolQuery(client);client.close();}private static void boolQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//查询所有数据BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name", "zhangsan"));boolQueryBuilder.should(QueryBuilders.matchQuery("set", "男"));sourceBuilder.query(boolQueryBuilder);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}}

执行结果:
在这里插入图片描述

4.1.2 范围查询
public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));rangeQuery(client);client.close();}private static void rangeQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");//大于等于rangeQuery.gte(30);//小于等于rangeQuery.lte(40);sourceBuilder.query(rangeQuery);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}
}

执行结果:
在这里插入图片描述

4.1.2 模糊查询

代码示例:

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));fuzzyQuery(client);client.close();}private static void fuzzyQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();FuzzyQueryBuilder builder = QueryBuilders.fuzzyQuery("name", "zhangsan").fuzziness(Fuzziness.ONE);sourceBuilder.query(builder);request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}}

执行结果:
在这里插入图片描述

4.2 高亮查询

代码示例

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));highLightQuery(client);client.close();}private static void highLightQuery(RestHighLevelClient client) throws IOException  {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.query(termsQueryBuilder);//设置高亮字段HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.preTags("<font color='red'>");//设置标签前缀highlightBuilder.postTags("</font>");//设置标签后缀highlightBuilder.field("name");//设置高亮字段//设置高亮构建对象sourceBuilder.highlighter(highlightBuilder);//设置请求体request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());//打印高亮结果Map<String, HighlightField> highlightFields = hit.getHighlightFields();System.out.println(highlightFields);}}}

执行结果
在这里插入图片描述

4.3 聚合查询

4.3.1函数使用

代码示例

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));maxQuery(client);client.close();}private static void maxQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.aggregation(AggregationBuilders.max("maxAge").field("age"));request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);System.out.println(searchResponse);//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}}

执行结果:
在这里插入图片描述

分组统计

public class SearchTest {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
//        searchAll(client);
//        termQuery(client);
//        pageQuery(client);
//        sort(client);
//        fetchSource(client);
//        boolQuery(client);
//        rangeQuery(client);
//        fuzzyQuery(client);
//        highLightQuery(client);
//        maxQuery(client);groupByQuery(client);client.close();}private static void groupByQuery(RestHighLevelClient client) throws IOException {// 创建搜索请求对象SearchRequest request = new SearchRequest();request.indices("student");//构建查询的请求体SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();sourceBuilder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));request.source(sourceBuilder);SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);System.out.println(searchResponse.getAggregations().get("age_groupby").getType());//查询匹配SearchHits hits = searchResponse.getHits();System.out.println("took:" + searchResponse.getTook());System.out.println("timeout:" + searchResponse.isTimedOut());System.out.println("total:" + hits.getTotalHits());System.out.println("MaxScore:" + hits.getMaxScore());System.out.println("hits========>>");for (SearchHit hit : hits) {//输出每条查询的结果信息System.out.println(hit.getSourceAsString());}}}

在这里插入图片描述

官方Java API 文档地址:
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/getting-started-java.html
代码地址:https://github.com/JustCoding-Hai/es-demo

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/49811.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C语言 | Leetcode C语言题解之第268题丢失的数字

题目&#xff1a; 题解&#xff1a; /* 求和运算 */ /* 对[0,n]求和, 减去数组每个元素, 得出丢失的元素 */ int missingNumber(int* nums, int numsSize){int i;int sum numsSize;for (i 0; i < numsSize; i) {sum i - nums[i];}return…

每天五分钟计算机视觉:目标检测模型从RCNN到Fast R-CNN的进化

本文重点 前面的课程中,我们学习了RCNN算法,但是RCNN算法有些慢,然后又有了基于RCNN的Fast-RCNN,Fast R-CNN是一种深度学习模型,主要用于目标检测任务,尤其在图像中物体的识别和定位方面表现出色。它是R-CNN系列算法的一个重要改进版本,旨在解决R-CNN中计算量大、速度慢…

python-阶乘和(赛氪OJ)

题目描述 求Sn​1!2!3!4!5!⋯n!的值&#xff0c;其中 &#x1d45b;n 是一个数字。输入格式&#xff1a; 输入一个整数 n。输出格式&#xff1a; 输出对应的 Sn​。 样例输入输出样例输入 5样例输出 153数据范围 对于 100% 的数据&#xff0c;保证1≤n≤20。来源/分类&#xff…

接口自动化测试框架实战-4-日志方法封装

上一小节我们讲解了文件读写方法的封装方法&#xff0c;本小节我们完成一下框架日志的封装方法。 首先我们讲解一下日志封装和日志记录有哪些用处&#xff1f; 更加方便的设置日志的格式和输出方式全局方法可以各个类/函数中都能统一调用分类记录接口用例执行日志&#xff0c…

【CPS出版】2024年智能计算与数据分析国际学术会议(ICDA 2024,9月6日-8)

为探讨数据科学和计算智能领域的关键问题&#xff0c;促进相关交流&#xff0c;2024年智能计算与数据分析国际学术会议&#xff08;ICDA 2024)将于2024年9月6日-8日在中国青岛召开。 本届会议拟邀请数据分析和计算智能领域的顶级专家、学者和产业界优秀人才&#xff0c;围绕当前…

【C语言】指针大小知多少 ?一场探寻C语言深处的冒险 !

目录 C语言中指针的大小1. 指针大小的基本概念1.1 32位系统1.2 64位系统 2. 指针大小示例2.1 32位系统输出2.2 64位系统输出 3. 指针大小与数据类型无关示例输出示例 4. 跨平台的指针大小示例输出示例 5. 关键点总结5.1 指针大小与平台关系5.2 跨平台编程注意事项 6. 指针大小示…

【QAC】Dashboard服务端如何配置

【更多软件使用问题请点击亿道电子官方网站】 1、 文档目标 解决Dashboard服务端如何配置的问题。 2、 问题场景 客户想使用Dashboard&#xff0c;Dashboard服务端如何配置。 3、软硬件环境 1、软件版本&#xff1a;HelixQAC23.04 2、机器环境&#xff1a;Windows 64bit 3…

arthas在idea和docker中的应用

基于IDEA工具使用 1&#xff0c;安装arthas插件 安装之后重启&#xff0c;然后再代码区域右键如果可以看到如下菜单选项&#xff0c;则说明安装成功 2&#xff0c;pom文件中配置arthas相关jar包 <!-- https://mvnrepository.com/artifact/com.taobao.arthas/arthas-pack…

解决小程序web-view两个恶心问题

&#x1f9d1;‍&#x1f4bb; 写在开头 点赞 收藏 学会&#x1f923;&#x1f923;&#x1f923; 1.web-view覆盖层问题 问题由来 web-view 是一个 web 浏览器组件&#xff0c;可以用来承载网页的容器&#xff0c;会自动铺满整个页面。 所以这得多恶心。。。不仅铺满&…

机会性加密技术:网络安全的新趋势

在当今数字化时代&#xff0c;网络安全已成为各行各业不可忽视的重要议题。随着网络攻击手段的不断演进&#xff0c;传统的加密方式已难以满足复杂多变的安全需求。机会性加密技术&#xff08;Opportunistic Encryption&#xff0c;简称OE&#xff09;&#xff0c;作为一种新兴…

基于微信小程序的健康饮食系统/健康饮食管理系统

摘 要 随着信息技术和网络技术的飞速发展&#xff0c;人类已进入全新信息化时代&#xff0c;传统管理技术已无法高效&#xff0c;便捷地管理信息。为了迎合时代需求&#xff0c;优化管理效率&#xff0c;各种各样的小程序应运而生&#xff0c;各行各业相继进入信息管理时代&…

[Vulnhub] Acid-Reloaded SQLI+图片数据隐写提取+Pkexec权限提升+Overlayfs权限提升

信息收集 IP AddressOpening Ports192.168.101.158TCP:22,33447 $ nmap -p- 192.168.101.158 --min-rate 1000 -sC -sV Not shown: 65534 closed tcp ports (conn-refused) PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 6.7p1 Ubuntu 5ubuntu1.3 (Ubuntu Lin…

前端面试宝典【Javascript篇】【2】

欢迎来到《前端面试宝典》&#xff0c;这里是你通往互联网大厂的专属通道&#xff0c;专为渴望在前端领域大放异彩的你量身定制。通过本专栏的学习&#xff0c;无论是一线大厂还是初创企业的面试&#xff0c;都能自信满满地展现你的实力。 核心特色&#xff1a; 独家实战案例…

jackson序列化(jackson codec)

Jackson 是一个用于 Java 平台的开源 JSON 库&#xff0c;它提供了灵活且高效的方式来处理 JSON 数据的序列化(Java对象 → JSON字符串)和反序列化(JSON 字符串→ Java对象)。 以下是 Jackson 的一些主要特点和功能&#xff1a; 高性能&#xff1a;Jackson 通过使用基于流的处理…

国内环境,怎么做开源才能兼顾生存和情怀?

回答一个朋友提的问题—— 在国内&#xff0c;开源怎么做呢&#xff1f;老夫结合自己多年在开源和商业领域的经历与见闻聊聊。 首先来说&#xff0c;毋庸置疑&#xff0c;开源是对传统的商业模式的一种颠覆&#xff0c;它以一种免费开放的姿态赢得了Hacker群体的心&#xff0c…

mac OS matplotlib missing from font(s) DejaVu Sans

如果能搜索到这篇文章&#xff0c;我猜你遇到了和我一样的问题&#xff1a;matplotlib绘图中文乱码。如下&#xff1a; 出现这个问题的原因是&#xff1a;matplotlib使用的字体列表中默认没有中文字体。 这里说一种解决方案&#xff1a;我们可以在文件中手动指定matplotlib使用…

二叉树详解-第一篇 树以及二叉树的概念

目录 ​编辑 1.树的概念及结构 1.1树的概念 1.2树的特点 1.3树的相关概念 1.4树的表示 2.二叉树的概念及结构 2.1二叉树的概念 2.2特殊二叉树-满二叉树和完全二叉树 1.满二叉树的概念及性质 2.完全二叉树的概念及性质 2.3二叉树的性质(重点) 2.4二叉树的存储 1.顺…

LeetCode - #103 二叉树的锯齿形层序遍历

文章目录 前言1. 描述2. 示例3. 答案关于我们 前言 我们社区陆续会将顾毅&#xff08;Netflix 增长黑客&#xff0c;《iOS 面试之道》作者&#xff0c;ACE 职业健身教练。&#xff09;的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 LeetCode 算法到目前我们已经更新…

沉淀硬化不锈钢压缩弹簧的主要特性

沉淀硬化不锈钢&#xff0c;作为一种兼具高强度与耐腐蚀性的合金材料&#xff0c;被广泛应用于制造高性能的压缩弹簧&#xff0c;尤其是在苛刻环境下的应用需求。以下是对沉淀硬化不锈钢压缩弹簧主要特性的科普介绍&#xff1a; 良好的耐蚀性 沉淀硬化不锈钢与304不锈钢拥有相似…

做个牛奶配送小程序商城是怎样的

商家线上经营奶品&#xff0c;主要是成品包装品牌奶或散奶周边客源生意模式等&#xff0c;无论哪些模式&#xff0c;本身奶产品的需求度就不低&#xff0c;自然也有着大量客户&#xff0c;农场/品牌厂商/经销商/小摊贩&#xff0c;除了线下实体店发展外&#xff0c;线上要完善本…