当前位置: 首页 > news >正文

[微服务]分布式搜索Java客户端

快速入门

使用RestClient客户端进行数据搜索可以分为两步

  1. 构建并发起请求

代码解读:

  • 第一步,创建SearchRequest对象,指定索引库名
  • 第二步,利用request.source()构建DSL,DSL中可以包含查询、分页、排序、高亮等
    • query():代表查询条件,利用QueryBuilders.matchAllQuery()构建一个match_all查询的DSL
  • 第三步,利用client.search()发送请求,得到响应

核心步骤:

  • 这里关键的API有两个,一个是request.source(),它构建的就是DSL中的完整JSON参数。其中包含了querysortfromsizehighlight等所有功能:

  • 另一个是QueryBuilders,其中包含了我们学习过的各种叶子查询复合查询等:

  1. 解析查询结果

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文档数据

示例

  1. 新建测试类

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();}}
}
  1. 创建并发送请求, 解析结果
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();}}
}
  1. 执行结果

构建查询条件

全文检索的查询条件构造API如下

精确查询的查询条件构造API如下:

布尔查询的查询条件构造API如下:

构建复杂查询条件的搜索

需求: 利用lavaRestClient实现搜索功能, 条件如下

  1. 搜索关键字为脱脂牛奶
  2. 品牌必须为德亚
  3. 价格必须低于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);// 按需处理高亮结果Map<String, 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();}}
}

http://www.lryc.cn/news/515500.html

相关文章:

  • 如何使用 `uiautomator2` 控制 Android 设备并模拟应用操作_VIVO手机
  • 在Ubuntu 18.04.6 LTS安装OpenFace流程
  • C 语言的整型提升问题
  • 第0章 机器人及自动驾驶SLAM定位方法全解析及入门进阶学习建议
  • video.js视频播放上手
  • 【LLM-Agent】Building effective agents和典型workflows
  • 《量子比特大阅兵:不同类型量子比特在人工智能领域的优劣势剖析》
  • 《探秘开源大模型:AI 世界的“超级引擎”》
  • el-table行列转换简单版,仅限单行数据
  • 2025年1月4日蜻蜓q旗舰版st完整开源·包含前后端所有源文件·开源可商用可二开·优雅草科技·优雅草kir|优雅草星星|优雅草银满|优雅草undefined
  • SQL把字符串按逗号分割成记录
  • C#设计模式(行为型模式):观察者模式
  • pytorch镜像源
  • Verilog语法之常用行为级语法
  • PADS Logic原理图中有很多页原理图,如何(怎样)删除其中一页或者多页
  • 蓝色简洁引导页网站源码
  • Apache PDFBox添加maven依赖,pdf转成图片
  • mybatis 和 mybatisPlus 兼容性问题
  • Mono里运行C#脚本23—mono_jit_exec
  • 第十一章 图论
  • 纯前端实现将pdf转为图片(插件pdfjs)
  • 【IT人物系列】之MySQL创始人
  • 在Typora中实现自动编号
  • Single Shot MultiBox Detector(SSD)
  • kafka生产者专题(原理+拦截器+序列化+分区+数据可靠+数据去重+事务)
  • 【React+TypeScript+DeepSeek】穿越时空对话机
  • 公共数据授权运营系统建设手册(附下载)
  • 基于HTML和CSS的旅游小程序
  • maven之插件调试
  • SQL Sever 数据库损坏,只有.mdf文件,如何恢复?