已經在上一篇文件講述了springboot整合elasticsearch時,包括elasticsearch的搭建,springboot整合到elasticsearch。
文章地址:https://blog.csdn.net/chenxihua1/article/details/94546282#commentBox
這裏我將講述springboot 整合elasticsearch 創建索引的方式。
elasticsearch創建索引,是有很多種方式的。
比如常用的是使用postman提交put請求,我的上一篇文章就是用這種方法。
現在介紹第二種,比較合適項目開發的創建索引的方式:
1: 新建索引實體類
package com.gosuncn.esdemo.domin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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;
/**
* 這個創建索引的過程,最主要是從 @Document 註解中的 createIndex = false 這個屬性開始,
* 因爲 createIndex默認是true, 所以項目啓動的時候,默認就會初始化這個索引,(但是此時還沒有加 settings 等設置)
* 所以,就必須把它設置爲 false。
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "local_library", type = "book", shards = 4, createIndex = false)
public class Library {
/**
* index:是否設置分詞
* analyzer:存儲時使用的分詞器
* ik_max_word
* ik_word
* searchAnalyze:搜索時使用的分詞器
* store:是否存儲
* type: 數據類型
*/
@Id
@Field(type = FieldType.Integer)
private Integer book_id;
@Field(store = true, analyzer = "myanalyzer", type = FieldType.text)
private String book_code;
@Field(store = true, analyzer = "myanalyzer", type = FieldType.text)
private String book_name;
@Field(store = true, analyzer = "myanalyzer", type = FieldType.Integer)
private Integer book_price;
@Field(store = true, analyzer = "myanalyzer", type = FieldType.text)
private String book_author;
@Field(store = true, analyzer = "myanalyzer", type = FieldType.text)
private String book_desc;
}
看到類的頭部註解信息,如註解信息所示。 在啓動項目時,如果 @Document 註解中的 createIndex屬性爲true,它會自動創建索引。 所有我這裏要設置它爲false,禁止它自己創建索引。
2: 新建索引
在項目啓動的時候,判斷是否已經存在索引了,如果不存在就新建。
package com.gosuncn.esdemo.init;
import com.gosuncn.esdemo.constant.EsData;
import com.gosuncn.esdemo.domin.Library;
import com.sun.org.apache.xml.internal.security.Init;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Map;
/**
* @ClassName: InitIndex
* @Create By: chenxihua
* @Author: Administrator
* @Date: 2019/11/29 10:15
**/
@Component
@Order(value = 0)
public class InitIndex implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(InitIndex.class);
@Autowired
ElasticsearchTemplate elasticsearchTemplate;
/**
* 項目啓動的時候,如果elasticsearch已經存有索引,則不做任何操作
* 如果沒有索引,則新建索引
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
boolean indexExists = elasticsearchTemplate.indexExists(Library.class);
if (indexExists){
logger.warn("存在索引");
}else {
logger.warn("索引不存在。。。");
try {
boolean index = elasticsearchTemplate.createIndex(Library.class, EsData.DEFAULT_SETTING);
boolean putMapping = elasticsearchTemplate.putMapping(Library.class);
if (index && putMapping){
logger.info("索引創建成功。。。");
}else {
logger.warn("索引創建失敗。。。");
}
}catch (Exception e){
logger.error("error: {}", e.getLocalizedMessage());
}
}
}
}
這裏是使用了 elasticsearchTemplate 裏面的方法。 elasticsearchTemplate裏面有很多方法是很好用的。如圖:
然後索引中的setting設置如下:
/**
* @ClassName: EsData
* @Create By: chenxihua
* @Author: Administrator
* @Date: 2019/11/29 10:24
*
* 這裏設置默認常量
*
**/
public class EsData {
/**
* 記錄一次我設置的setting, 其實在 DEFAULT_SETTING 的最外層,是不再需要 {\"setting\"} 的了
*/
public static final String DEFAULT_SETTING = "{" +
"\"analysis\": {" +
"\"analyzer\": {" +
"\"myanalyzer\": {" +
"\"tokenizer\": \"mytokenizer\"" +
"}" +
"}," +
"\"tokenizer\": {" +
"\"mytokenizer\": {" +
"\"type\": \"ngram\"," +
"\"min_gram\": 1," +
"\"max_gram\": 2," +
"\"token_chars\": [" +
"\"letter\"," +
"\"digit\"," +
"\"whitespace\"," +
"\"punctuation\"," +
"\"symbol\"" +
"]" +
"}" +
"}" +
"}" +
"}";
}
這樣就可以完成 elasticsearch中創建索引的時候,自定義settings的設置了。
要了解我的這篇博客,請看我的第一篇博客《springboot整合elasticsearch5.x以及IK分詞器做全文檢索》
上一篇的創建索引方式,是使用postman的put方式,這種方式,不符合生產環境。 所以這篇文章,將創建索引的方式,整合到項目啓動時候創建自定義的方式。