前面的blog已經處理完Spring Boot和docker安裝配置ElasticSearch,這篇blog主要講一下使用docker安裝ElasticSearch ik分詞器和拼音搜索功能。java
咱們直接使用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分詞器同樣咱們一樣使用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目錄查看是否安裝成功,準備工做就完成了。
前面已經完成了一部分編碼工做了,請查看Spring Boot 使用docker整合ElasticSearch,那麼此次咱們就繼續在上次的基礎上改造,使其支持拼音和分詞搜索。docker
文件存放在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
}
}
}
}
}
複製代碼
若是以前已經建立了索引了,則須要刪除,或者從新建立新的全部才能建立分詞和拼音相關索引,支持分詞和拼音搜索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