RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)

一、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)重新启动项目

便可以进行自动补全了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值