1.创建工程,引入坐标:
<dependencies> <!-- springData整合elasticsearch --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.5.RELEASE</version> </dependency> <!-- 日志相关包 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.6.RELEASE</version> </dependency> </dependencies>
2.创建applicationContext-es.xml配置文件,引入elasticsearch命名空间:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 扫描dao包,自动创建实例 --> <elasticsearch:repositories base-package="com.fgy.dao"/> <!-- 配置Client --> <elasticsearch:transport-client id="client" cluster-name="es-fan-cluster" cluster-nodes="192.168.43.182:9300,192.168.43.182:9301,192.168.43.182:9302"/> <!-- 配置搜索模板 --> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean> </beans>
3.编写实体类:
// @Document 文档对象(索引信息、文档类型) @Document(indexName = "news", type = "article") public class Article { // @Id 文档主键 唯一标识 @Id @Field(store = true, index = false, type = FieldType.Integer) private Long id; @Field(analyzer = "ik_max_word", store = true, searchAnalyzer = "ik_max_word", type = FieldType.text) private String title; @Field(analyzer = "ik_smart", store = true, searchAnalyzer = "ik_smart", type = FieldType.text) private String content; /******************** get/set方法 ********************/ }
注解解释如下:
@Document(indexName="news",type="article"):
indexName:索引的名称(必填项)
type:索引的类型
replicas:副本数量,默认1
shards:分片数量,默认5
@Id:主键的唯一标识
@Field(index=true,analyzer="ik_smart",store=true,searchAnalyzer="ik_smart",type=FieldType.text)
index:是否设置分词,默认true
analyzer:存储时使用的分词器
searchAnalyze:搜索时使用的分词器
ik_max_word:会将文本做最细粒度的拆分 ik_smart:会做最粗粒度的拆分
store:是否存储,默认false
type: 数据类型
4.编写 dao 接口
// 自定义dao接口继承ElasticsearchRepository<实体类型,主键类型> 有基本的crud和分页功能 public interface ArticleDao extends ElasticsearchRepository<Article, Long> { }
5.测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-es.xml") public class ArticleDaoTest { @Autowired ElasticsearchTemplate esTemplate; @Autowired private ArticleDao articleDao; @Test public void testSave() { // 1.创建索引 esTemplate.createIndex(Article.class); // 2.创建映射 esTemplate.putMapping(Article.class); // 3.创建文档 Article article = new Article(); article.setId(1L); article.setTitle("疯狂Java"); article.setContent("入门到放弃"); // 4.保存文档 articleDao.save(article); } }