1.下载拼音分词器
将文件下载后解压,放到es的目录下(与ik分词器放在同一目录下)
重启es即可
2.测试:
会将文本的每一个拼音进行分词及所有的拼音首字母进行分词
#测试
POST /_analyze
{
"text": ["如家酒店"],
"analyzer": "pinyin"
}
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类型的
#创建索引库
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代码实现自动补全的查询
解析结果
@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<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
for (CompletionSuggestion.Entry.Option option : options) {
String text = option.getText().toString();
System.out.println(text);
}
}