当前位置:网站首页>RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
2022-07-17 05:12:00 【其然乐衣】
一、RestAPI实现自动补全查询(代码讲解)



代码:
@Autowired
private RestHighLevelClient client;
@Test
void testSuggestion() throws IOException {
//1.准备Request
SearchRequest request = new SearchRequest("hotel");
//2.准备DSL
request.source().suggest(new SuggestBuilder().addSuggestion(
"suggestions",
SuggestBuilders.completionSuggestion("suggestion")
.prefix("hm")
.skipDuplicates(true)
.size(10)
));
//3.发起请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//4.解析结果
Suggest suggest = response.getSuggest();
//4.1 根据补全查询名称,获取更全的结果
CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");
//4,2 获取options
List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
//4.3 遍历
for( CompletionSuggestion.Entry.Option option : options){
String text = option.getText().toString();
System.out.println(text);
}
// System.out.println(response);
}其中:
@Autowired
private RestHighLevelClient client;
要在项目启动方法里面注入到bean里

二、以下进行黑马旅游网的案例实现自动补全功能:
开始时,输入x,不能自动补全

RestAPI实现自动补全:
(1)在controller层写好接口

@GetMapping("suggestion")
public List<String> getSuggestions(@RequestParam("key") String predix){
return hotelService.getSuggestions(predix);
}(2)在service层写好

@Override
public List<String> getSuggestions(String predix) {
try {
//1.准备Request
SearchRequest request = new SearchRequest("hotel");
//2.准备DSL
request.source().suggest(new SuggestBuilder().addSuggestion(
"suggestions",
SuggestBuilders.completionSuggestion("suggestion")
.prefix(predix)
.skipDuplicates(true)
.size(10)
));
//3.发起请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//4.解析结果
Suggest suggest = response.getSuggest();
//4.1 根据补全查询名称,获取更全的结果
CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");
//4,2 获取options
List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
//4.3 遍历
List<String> list = new ArrayList<String>(options.size());
for( CompletionSuggestion.Entry.Option option : options){
String text = option.getText().toString();
System.out.println(text);
list.add(text);
}
return list;
} catch (IOException e) {
throw new RuntimeException();
}
}(3)重新启动项目
便可以进行自动补全了

边栏推荐
- Wide voltage input high voltage output voltage control type
- MySQL service is starting MySQL service failed to start
- KU115 FPGA 高性能万兆光纤网络硬件加速卡 / 2 路 10G 光纤数据加速卡
- go语言介绍及应用场景分析
- 单片机的好搭档-CS创世SD NAND FLASH
- 无线充电芯片IC
- 什么是tSD/qSD?CS创世 SD NAND到底是什么?
- [附代码] 如何用HOG+SVM实现手写数字识别
- Wireless charging mouse pad RGB LED lighting wireless charging mouse pad
- CV learning notes [1]: transforms
猜你喜欢

HRA2460D-2w高压电源高压模块-高压---高精度hra2460d-2w

FMC子卡:8 通道 125MSPS采样率16 位 AD 采集子卡
![Pytorch learning notes [5]: generalization using convolution](/img/a8/0a02e36f0ea531b0d3169f6cc04ad1.png)
Pytorch learning notes [5]: generalization using convolution

什么是tSD/qSD?CS创世 SD NAND到底是什么?

IT4058A型号单节锂离子电池充电管理

4-channel encoder pulse counter, 8-Channel do, Modbus TCP module

CUDA编程-01: 搭建CUDA编程环境

Image compression based on Quadtree

基于 Kintex UltraScale 系列 FPGA 的高性能 PXIE 数据预处理载板(KU060 +FMC子卡接口)

分享CS品牌贴片式T卡在打猎相机领域的运用案例
随机推荐
Xilinx UltraScale+ MPSOC(ZU9EG/ZU15EG)高性能 PCIe 数据预处理板
4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter
5V boost charging 8.4v chip
RS-485/232转4-20mA/0-10V隔离D/A转换器
[introduction to speech recognition] basic concepts and framework
如何用NumPy读取和保存点云数据
CS品牌SD NAND在空气质量检测行业中的应用案例
Thermal resistance PT100 cu50 isolation converter to 4-20mA analog output temperature transmitter 0-10V
CUDA编程-01: 搭建CUDA编程环境
Tips for using tp4054 charging IC -- used in conjunction with Zhongke Lanxun ab5365b
4 路 FMC+基带信号处理板( 4 路 2G 瞬时带宽 AD+DA)
3.7V锂电池升压到5V1A,FS2114升压转换芯片设计布局
VSCode 即时英文翻译插件 【翻译(英汉词典)】
4-channel encoder pulse counter, speed measurement, 8-Channel do, Modbus TCP data acquisition module
热电阻pt100 CU50隔离转换器转4-20ma模拟量输出温度变送器0-10V
General review of software process and management
minio安装部署及简单使用
String distance problem
Contrastive learning for image semantic segmentation (two articles)
4-20MA转0-5KHz,5V脉冲转换器