- 创建实体类
@Document(indexName = "moon_blog", createIndex = true)
public class ESArticleDto {
@Id
private String id;
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
private String articleTitle;
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
private String articleContent;
@Field(type = FieldType.Boolean)
private Boolean deleted;
@Field(type = FieldType.Long)
private Long articleId;
@Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
private String articleSummary;
public ESArticleDto() {}
@PersistenceCreator
public ESArticleDto(
String id, String articleTitle,
String articleContent,
Boolean deleted, Long articleId,
String articleSummary) {
this.id = id;
this.articleTitle = articleTitle;
this.articleContent = articleContent;
this.deleted = deleted;
this.articleId = articleId;
this.articleSummary = articleSummary;
}
...getter
...setter
}
- 创建Repository(重要)
@Repository
public interface ES extends ElasticsearchRepository<ESArticleDto, String> {
}
- 启动应用,可以查询到已经配置上了分词器
{
"moon_blog": {
"aliases": {},
"mappings": {
"properties": {
"_class": {
"type": "keyword",
"index": false,
"doc_values": false
},
"articleContent": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"articleId": {
"type": "long"
},
"articleSummary": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"articleTitle": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"deleted": {
"type": "boolean"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"refresh_interval": "1s",
"number_of_shards": "1",
"provided_name": "moon_blog",
"creation_date": "1676942756292",
"number_of_replicas": "1",
"uuid": "xh4Y5X5qRyidGrH8TE6o9w",
"version": {
"created": "8060199"
}
}
}
}
}