实现es搜索栏自动补全功能

1.下载拼音分词器

github官方地址:GitHub - medcl/elasticsearch-analysis-pinyin: This Pinyin Analysis plugin is used to do conversion between Chinese characters and Pinyin.

将文件下载后解压,放到es的目录下(与ik分词器放在同一目录下)

重启es即可

2.测试:

实现es搜索栏自动补全功能_第1张图片

 会将文本的每一个拼音进行分词及所有的拼音首字母进行分词

#测试
POST /_analyze
{
  "text": ["如家酒店"],
  "analyzer": "pinyin"
}

2.自定义分词器(必须在创建索引库时创建)

实现es搜索栏自动补全功能_第2张图片

 

# 自定义拼音分词器
PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": { 
        "my_analyzer": { 
          "tokenizer": "ik_max_word",
          "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": {
      "name":{
        "type": "text",
        "analyzer": "my_analyzer",-------------创建索引时使用的分词器
        "search_analyzer": "ik_smart"----------搜索时使用的分词器
      }
    }
  }
}

#测试
POST /test/_analyze
{
  "text": ["如家酒店"],
  "analyzer": "my_analyzer"
}

3.实现自动补全功能

(1)实现自动补全的字段必须是completion类型的

实现es搜索栏自动补全功能_第3张图片

 


#创建索引库
PUT test2
{
  "mappings": {
    "properties": {
      "title":{
        "type": "completion"
      }
    }
  }
}
#补充数据
POST test2/_doc
{
  "title":["sony","wh-1000xm3"]
}
POST test2/_doc
{
  "title":["sk-2","pitera"]
}
POST test2/_doc
{
  "title":["nintendo","switch"]
}

自动补全查询
POST /test/_search
{
  "suggest": {
    "title_suggest": {
      "text": "s", 
      "completion": {
        "field": "title", 
        "skip_duplicates": true, 
        "size": 10 
      }
    }
  }
}

使用java代码实现自动补全的查询

实现es搜索栏自动补全功能_第4张图片

 解析结果

实现es搜索栏自动补全功能_第5张图片

 

@Test
    void testSuggest() throws IOException {
        //1.准备request
        SearchRequest request = new SearchRequest("hotel");
        //2.准备dsl
        request.source().suggest(new SuggestBuilder().addSuggestion(
                "suggestions",
                SuggestBuilders.completionSuggestion("suggestion")
                        .prefix("hz")
                        .skipDuplicates(true)
                        .size(10)
        ));
        //3.发起请求
        SearchResponse search = client.search(request, RequestOptions.DEFAULT);
        //4.解析结果
        Suggest suggest = search.getSuggest();
        //根据补全查询名称,获取补全结果
        CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");
        //获取options
        List options = suggestions.getOptions();
        for (CompletionSuggestion.Entry.Option option : options) {
            String text = option.getText().toString();
            System.out.println(text);
        }
    }

你可能感兴趣的:(es,elasticsearch,大数据,搜索引擎)