移动端网站 优帮云,app软件免费模板下载网站,视频网站开发要多少钱,网站名称怎样做快速入门
使用RestClient客户端进行数据搜索可以分为两步
构建并发起请求 代码解读#xff1a;
第一步#xff0c;创建SearchRequest对象#xff0c;指定索引库名第二步#xff0c;利用request.source()构建DSL#xff0c;DSL中可以包含查询、分页、排序、高亮等 query…快速入门
使用RestClient客户端进行数据搜索可以分为两步
构建并发起请求 代码解读
第一步创建SearchRequest对象指定索引库名第二步利用request.source()构建DSLDSL中可以包含查询、分页、排序、高亮等 query()代表查询条件利用QueryBuilders.matchAllQuery()构建一个match_all查询的DSL
第三步利用client.search()发送请求得到响应
核心步骤:
这里关键的API有两个一个是request.source()它构建的就是DSL中的完整JSON参数。其中包含了query、sort、from、size、highlight等所有功能 另一个是QueryBuilders其中包含了我们学习过的各种叶子查询、复合查询等 解析查询结果 elasticsearch返回的结果是一个JSON字符串结构包含
hits命中的结果 total总条数其中的value是具体的总条数值max_score所有结果中得分最高的文档的相关性算分hits搜索结果的文档数组其中的每个文档都是一个json对象 _source文档中的原始数据也是json对象
因此我们解析响应结果就是逐层解析JSON字符串流程如下
SearchHits通过response.getHits()获取就是JSON中的最外层的hits代表命中的结果 SearchHits#getTotalHits().value获取总条数信息SearchHits#getHits()获取SearchHit数组也就是文档数组 SearchHit#getSourceAsString()获取文档结果中的_source也就是原始的json文档数据 示例
新建测试类 public class ElasticSearchTest {private RestHighLevelClient client;Testvoid test() {System.out.println(client client);}BeforeEachvoid setup() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.1.97:9200)));}AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}
}创建并发送请求, 解析结果
public class ElasticSearchTest {private RestHighLevelClient client;Testvoid test() {System.out.println(client client);}Testvoid testMatchAll() throws IOException {//1.创建request对象SearchRequest request new SearchRequest(items);//2.配置request参数request.source().query(QueryBuilders.matchAllQuery());//3.发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);System.out.println(response response);//4.解析结果SearchHits searchHits response.getHits();// 总条数long total searchHits.getTotalHits().value;System.out.println(total total);// 命中的数据SearchHit[] hits searchHits.getHits();for (SearchHit hit : hits) {// 获取source结果String json hit.getSourceAsString();// 转为ItemDocItemDoc doc JSONUtil.toBean(json, ItemDoc.class);System.out.println(doc doc);}}BeforeEachvoid setup() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.1.97:9200)));}AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}
}执行结果 构建查询条件
全文检索的查询条件构造API如下 精确查询的查询条件构造API如下: 布尔查询的查询条件构造API如下: 构建复杂查询条件的搜索
需求: 利用lavaRestClient实现搜索功能, 条件如下
搜索关键字为脱脂牛奶品牌必须为德亚价格必须低于300
public class ElasticSearchTest {private RestHighLevelClient client;Testvoid test() {System.out.println(client client);}Testvoid testSearch() throws IOException {//1.创建request对象SearchRequest request new SearchRequest(items);//2.组织DSL参数request.source().query(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery(name, 脱脂牛奶)).filter(QueryBuilders.termQuery(brand, 德亚)).filter(QueryBuilders.rangeQuery(price).lt(3000)));//3.发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);System.out.println(response response);//4.解析结果parseResponseResult(response);}private void parseResponseResult(SearchResponse response) {//4.解析结果SearchHits searchHits response.getHits();// 总条数long total searchHits.getTotalHits().value;System.out.println(total total);// 命中的数据SearchHit[] hits searchHits.getHits();for (SearchHit hit : hits) {// 获取source结果String json hit.getSourceAsString();// 转为ItemDocItemDoc doc JSONUtil.toBean(json, ItemDoc.class);System.out.println(doc doc);}}BeforeEachvoid setup() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.1.97:9200)));}AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}
}排序和分页
与query类似排序和分页参数都是基于request.source()来设置: public class ElasticSearchTest {private RestHighLevelClient client;Testvoid test() {System.out.println(client client);}Testvoid testSortAndPage() throws IOException {// 模拟前端分页参数int pageNo 1, pageSize 5;//1.创建request对象SearchRequest request new SearchRequest(items);//2.组织DSL条件//2.1query条件request.source().query(QueryBuilders.matchAllQuery());//2.2.分页条件request.source().from((pageNo - 1) * pageSize).size(pageSize);//2.3 排序条件request.source().sort(sold, SortOrder.DESC).sort(price,SortOrder.ASC);//3.发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);//4.解析结果parseResponseResult(response);}private void parseResponseResult(SearchResponse response) {//4.解析结果SearchHits searchHits response.getHits();// 总条数long total searchHits.getTotalHits().value;System.out.println(total total);// 命中的数据SearchHit[] hits searchHits.getHits();for (SearchHit hit : hits) {// 获取source结果String json hit.getSourceAsString();// 转为ItemDocItemDoc doc JSONUtil.toBean(json, ItemDoc.class);System.out.println(doc doc);}}BeforeEachvoid setup() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.1.97:9200)));}AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}
}高亮展示
高亮显示的条件构造API如下 高亮显示的结果解析API如下: 示例
public class ElasticSearchTest {private RestHighLevelClient client;Testvoid test() {System.out.println(client client);}Testvoid testHighLight() throws IOException {//1.创建request对象SearchRequest request new SearchRequest(items);//2.组织DSL条件//2.1query条件request.source().query(QueryBuilders.matchQuery(name, 脱脂牛奶));//2.2.高亮条件request.source().highlighter(SearchSourceBuilder.highlight().field(name));//3.发送请求SearchResponse response client.search(request, RequestOptions.DEFAULT);//4.解析结果parseResponseResult(response);}private void parseResponseResult(SearchResponse response) {//4.解析结果SearchHits searchHits response.getHits();// 总条数long total searchHits.getTotalHits().value;System.out.println(total total);// 命中的数据SearchHit[] hits searchHits.getHits();for (SearchHit hit : hits) {// 获取source结果String json hit.getSourceAsString();// 转为ItemDocItemDoc doc JSONUtil.toBean(json, ItemDoc.class);// 按需处理高亮结果MapString, HighlightField hfs hit.getHighlightFields();if (hfs ! null !hfs.isEmpty()) {// 根据高亮字段名获取高亮结果HighlightField hf hfs.get(name);// 获取高亮结果, 覆盖非高亮结果String name hf.fragments()[0].string();doc.setName(name);}System.out.println(doc doc);}}BeforeEachvoid setup() {client new RestHighLevelClient(RestClient.builder(HttpHost.create(http://192.168.1.97:9200)));}AfterEachvoid tearDown() throws IOException {if (client ! null) {client.close();}}
}