Elasticsearch之自动补全案例实战

Posted 爱上口袋的天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch之自动补全案例实战相关的知识,希望对你有一定的参考价值。

1、案例需求:

通过拼音以及汉字筛选对应的明星!

2、设置分词器以及mapping

PUT starts
{
  "settings": {
    "analysis": {
      "analyzer": {
        "start_name_analyzer":{
          "char_filter":["html_strip"],
          "tokenizer":"keyword",
          "filter":"start_name_filter"
        }
      },
      "filter": {
        "start_name_filter":{
          "type":"pinyin",
          "keep_full_pinyin":false,
          "keep_joined_full_pinyin":true,
          "keep_original": true
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name":{
        "type": "completion",
        "analyzer": "start_name_analyzer",
        "search_analyzer": "keyword"
      }
    }
  }
}

3、批量插入数据

PUT starts/_bulk
{"index":{}}
{"name":"张学友"}
{"index":{}}
{"name":"刘德华"}
{"index":{}}
{"name":"柳岩"}
{"index":{}}
{"name":"李易峰"}
{"index":{}}
{"name":"黄晓明"}
{"index":{}}
{"name":"刘青云"}
{"index":{}}
{"name":"易中天"}
{"index":{}}
{"name":"李小璐"}
{"index":{}}
{"name":"黄家驹"}
{"index":{}}
{"name":"李思思"}
{"index":{}}
{"name":"庄媛媛"}
{"index":{}}
{"name":"刘亦菲"}

4、在kibana上测试一下效果

GET starts/_search
{
  "_source": false,
  "suggest": {
    "start_name_suggest": {
      "prefix":"柳",
      "completion":{
        "field":"name",
        "size": 10,
        "skip_duplicates": true
      }
    }
  }
}

5、在java服务端实现上面测试的自动补全案例

5.1、Springboot整合ES7.8.1已经提前整合了

5.2、创建controller类

package com.kgf.es.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("start")
@RestController
public class StartController {


    @Resource
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("autoComplete")
    public Object autoComplete(String term) throws IOException {
        Request request = new Request("GET", "starts/_search");
        request.setJsonEntity(String.format("{" +
                "  \\"_source\\": false," +
                "  \\"suggest\\": {" +
                "    \\"start_name_suggest\\": {" +
                "      \\"prefix\\":\\"%s\\"," +
                "      \\"completion\\":{" +
                "        \\"field\\":\\"name\\"," +
                "        \\"size\\": 10," +
                "        \\"skip_duplicates\\": true" +
                "      }" +
                "    }" +
                "  }" +
                "}",term));
        Response response = restHighLevelClient.getLowLevelClient().performRequest(request);
        String json = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray suggests = jsonObject.getJSONObject("suggest").getJSONArray("start_name_suggest");
        JSONArray options = suggests.getJSONObject(0).getJSONArray("options");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < options.size(); i++) {
            JSONObject object = options.getJSONObject(i);
            list.add(object.getString("text"));
        }
        return list;
    }
}

6、访问测试效果

以上是关于Elasticsearch之自动补全案例实战的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch之新闻案例实战

Elasticsearch 分布式搜索引擎 -- 自动补全(拼音分词器自定义分词器自动补全查询实现搜索框自动补全)

Elasticsearch语法知多少之Match query

Elasticsearch Suggester API(自动补全)

elasticsearch补全功能之只补全筛选后的部分数据context suggester

Elasticsearch顶尖高手系列-快速入门篇