Spring Boot使用docker整合ElasticSearch ik分詞搜索和拼音搜索

前面的blog已經處理完Spring Boot和docker安裝配置ElasticSearch,這篇blog主要講一下使用docker安裝ElasticSearch ik分詞器和拼音搜索功能。java

準備工做

安裝ik分詞器

咱們直接使用elasticsearch-plugin安裝ik分詞器git

// 進入elasticsearch容器
docker exec -it elasticsearch /bin/bash

// 安裝ik分詞器7.8.0
 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
複製代碼

安裝過程以下: github

安裝ik分詞器

安裝pinyin插件

和ik分詞器同樣咱們一樣使用elasticsearch-plugin安裝pinyin插件spring

// 安裝ik分詞器7.8.0
 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.8.0/elasticsearch-analysis-pinyin-7.8.0.zip
複製代碼

在這裏插入圖片描述
檢查一下plugins目錄查看是否安裝成功,準備工做就完成了。
檢查plugins目錄

Spring Boot進行分詞和拼音查詢

前面已經完成了一部分編碼工做了,請查看Spring Boot 使用docker整合ElasticSearch,那麼此次咱們就繼續在上次的基礎上改造,使其支持拼音和分詞搜索。docker

建立setting和mapping文件

文件存放在resource目錄下,爲了方便區分,因此我新建了一個elasticsearch目錄。 shell

文件目錄
elasticsearch_mapping.json文件內容

{
  "block": {
    "properties": {
      "userName": {
        "type": "text",
        "analyzer": "pinyin_analyzer",
        "search_analyzer": "pinyin_analyzer",
        "fields": {
          "pinyin": {
            "type": "string",
            "ignore_above": 256
          }
        }
      },
      "sex": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "age": {
        "type": "keyword",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}
複製代碼

elasticsearch_setting.json文件內容json

{
  "index": {
    "analysis": {
      "analyzer": {
        "pinyin_analyzer": {
          "tokenizer": "my_pinyin"
        }
      },
      "tokenizer": {
        "my_pinyin": {
          "type": "pinyin",
          "keep_first_letter": true,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": true,
          "keep_original": true,
          "limit_first_letter_length": 16,
          "lowercase": true,
          "remove_duplicated_term": true
        }
      }
    }
  }
}
複製代碼

將setting和mapping配置到UserEntity中

若是以前已經建立了索引了,則須要刪除,或者從新建立新的全部才能建立分詞和拼音相關索引,支持分詞和拼音搜索api

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Mapping;
import org.springframework.data.elasticsearch.annotations.Setting;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Setting(settingPath = "classpath:elasticsearch/elasticsearch_setting.json")
@Mapping(mappingPath = "classpath:elasticsearch/elasticsearch_mapping.json")
@Document(indexName = "user")
public class UserEntity {

    @Id
    private Long userId;

    @Field(type = FieldType.Text, analyzer = "pinyin", searchAnalyzer = "pinyin")
    private String userName;

    @Field(type = FieldType.Keyword)
    private Integer age;

    @Field(type = FieldType.Keyword)
    private Integer sex;

}

複製代碼

編寫測試類

依舊是在以前的測試類基礎上進行編寫,首先執行save方法批量插入一些數據方便測試,而後執行searchByUserName查看結果。bash

import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.service.ElasticSearchService;
import com.example.elasticsearch.util.StringUtils;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import javax.annotation.Resource;

@SpringBootTest
class ElasticsearchApplicationTests {

    @Resource
    private ElasticSearchService elasticSearchService;

	/** * @Author David * @Description 批量插入測試數據 **/
    @Test
    void save() {
        List<String> randomName = StringUtils.getRandomName(100);
        List<UserEntity> list = new ArrayList<>(100);
        for (int i = 0; i < randomName.size(); i++) {
            UserEntity userEntity = UserEntity.builder()
                    .userId(i + 1L).userName(randomName.get(i)).age(ThreadLocalRandom.current().nextInt(50))
                    .sex(ThreadLocalRandom.current().nextInt(2))
                    .build();
            list.add(userEntity);
        }
        elasticSearchService.saveUser(list);
    }
	
	/** * @Author David * @Description 根據id搜索 **/
    @Test
    void findById() {
        UserEntity byId = elasticSearchService.findById(1L);
        System.out.println(byId);
    }

	/** * @Author David * @Description 搜索 **/
    @Test
    void searchByUserName() {
        List<UserEntity> da = elasticSearchService.searchByUserName("wpt");
        System.out.println(da);
        List<UserEntity> da1 = elasticSearchService.searchByUserName("聞平塗");
        System.out.println(da1);
    }

}
// searchByUserName output
[UserEntity(userId=7, userName=聞平塗, age=44, sex=1)]
[UserEntity(userId=7, userName=聞平塗, age=44, sex=1)]
複製代碼

總結

ElasticSearch使用很是普遍,支持的場景也很是多,除了分詞和拼音以外,還有像繁體搜索,以及高亮顯示都用的比較多,後面一一梳理,若是對你有幫助記得關注支持一下哦,這是我前進的最大動力!app

相關文章
相關標籤/搜索