Elasticsearch实战-实现Hotel索引库的自动补全、拼音搜索功能

一、实现思路

1.修改hotel索引库结构,设置自定义拼音分词器
2.修改索引库的name、all字段,使用自定义分词器
3.索引库添加一个新字段suggestion,类型为completion类型,使用自定义的分词器
4.给HotelDoc类添加suggestion字段,内容包含brand、business
5.重新导入数据到hotel库

二、参考文献

三、数据准备

1、新建索引库hotel2

增加字段suggestion


# 创建新的索引库2
PUT /hotel2
{
  "settings":{
    "analysis": {
      "analyzer": {
        "text_analyzer":{
          "tokenizer": "ik_max_word",
          "filter":"py"
        },
        "completion_analyzer":{
          "tokenizer": "keyword",
          "filter":"py"
        }
      },
      "filter": {
        "py":{
          "type":"pinyin",
          "keep_full_pinyin":false,
          "keep_joined_full_pinyin":true,
          "keep_original":true,
          "limit_first_letter_length":16,
          "remove_duplicated_term":true,
          "none_chinese_pinyin_tokenize":false
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "text_analyzer",
        "search_analyzer": "ik_smart",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type":"keyword"
      },
      "score":{
        "type":"keyword"
      },
      "brand":{
        "type":"keyword",
        "copy_to": "all"
      },
      "city":{
        "type":"keyword"
      },
      "starName":{
        "type":"keyword"
      },
      "business":{
        "type":"keyword",
        "copy_to": "all"
      },
      "location":{
        "type":"geo_point"
      },
      "pic":{
        "type":"keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "text_analyzer",
        "search_analyzer": "ik_smart"
      },
      "suggestion":{
        "type": "completion",
        "analyzer": "completion_analyzer"
      }
    }
  }
}

2、重新导入数据

在这里插入图片描述

3、测试自动补全

# 自动补全查询
GET /hotel2/_search
{
  "suggest": {
    "suggestions": {
      "text": "s",
      "completion": {
        "field": "suggestion",
        "skip_duplicates":true,
        "size":10
      }
    }
  }
}

在这里插入图片描述

四、RestAPI实现自动补全

1、先看请求参数构造的API

在这里插入图片描述

2、执行测试方法

testSuggest()

/**
     * 测试自动补全
     * @throws IOException
     */
    @Test
    void testSuggest() throws IOException {
        // 1. 准备Request
        SearchRequest request = new SearchRequest(INDEX);
        // 2. 准备DSL
        request.source().suggest(new SuggestBuilder().addSuggestion(
           "suggestions",
                SuggestBuilders
                        .completionSuggestion("suggestion")
                .prefix("h")
                .skipDuplicates(true)
                .size(10)

        ));
        // 3. 发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        // 4.解析响应
        //得到响应结果
       // System.out.println(response);
        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);
        }
    }


3、解析结果

在这里插入图片描述

五、实战案例

1、前端页面请求

在这里插入图片描述

2、代码

// controller
/**
     * 自动补全
     * @param prefix
     * @return
     */
    @GetMapping("/suggestion")
    public List<String> getFilters(@RequestParam("key") String prefix){
        return hotelService.getSuggestions(prefix);
    }
...
...
// service
@Override
    public List<String> getSuggestions(String prefix) {
        try{
            // 1. 准备Request
            SearchRequest request = new SearchRequest(INDEX);
            // 2. 准备DSL
            request.source().suggest(new SuggestBuilder().addSuggestion(
                    "suggestions",
                    SuggestBuilders
                            .completionSuggestion("suggestion")
                            .prefix(prefix)
                            .skipDuplicates(true)
                            .size(10)

            ));
            // 3. 发送请求
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            // 4.解析响应
            //得到响应结果
            // System.out.println(response);
            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<>(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(e);
        }
    }

3、效果

在这里插入图片描述

六、源码

hotel2分支

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值