【1】jar包配置
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</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>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</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>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
【2】配置applicationContext文件
????????
<?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:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
">
<!--1Client客户端配置-->
<elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"
/>
<!--2配置包扫描器,扫描dao的接口-->
<!--扫描后会放入bean容器中-->
<elasticsearch:repositories base-package="com.itheima.repositories"/>
<!--3配置模板对象-->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<!--4想让模板能用,还需要配置一个构造参数,client-->
<constructor-arg name="client" ref="esClient"/>
</bean>
<!--步骤
1.创建一个entity实体类,就是一个javabean(pojo类)映射到一个document上
2.创建一个Dao,是一个接口,需要继承ElasticSearchRepository接口
3.编写测试代码
-->
</beans>
?【3】pojo类写法,后面加上get。set
@Document(indexName = "springdate_hello",type = "article")
public class Article {
//配置映射关系mapping。
@Id
@Field(type = FieldType.Long,index = true,store = true)//设置域的参数,和es一样
private long id;
@Field(type = FieldType.text,index = true,store = true,analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text,index = true,store = true,analyzer = "ik_smart")
private String content;
【4】测试
???????? 这里的?articleRepository就是我们配置XML文件时扫描的那个接口,他会自动被添加到ElasticSearchtemple的bean容器中。所以这个接口可以被调用。
//想让spring的bean在test中初始化需要用到@Runwith和@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest {
@Autowired
private ArticleRepository articleRepository;//这里报错不用管,运行的时候就会配置进来
@Autowired
private ElasticsearchTemplate template;//需要用模板类进行crud,创建索引就用template
@Test
public void createIndex() throws Exception{
//创建索引并且配置映射关系
template.createIndex(Article.class);
//只有索引库没有映射时配置映射关系
//template.putMapping(Article.class);
}
@Test
public void addDocument() throws Exception{
//创建一个article对象
for (int i = 2; i <=10; i++) {
Article article=new Article();
article.setId((long)i);
article.setTitle("利用springdata进行添加文档操作"+i);
article.setContent("比javaEs更方便"+i);
//把文档写入索引库
articleRepository.save(article);
}
}
@Test
public void delDocument(){
articleRepository.deleteById(2l);
}
//修改文档,就是addDocument,里面的id不变即可,前提是已经存在这个id
@Test
public void searchAll(){
Iterable<Article> all = articleRepository.findAll();
for (Article article : all) {
System.out.println(article);
}
}
@Test
public void findById(){
// optional是jdk8中新的类,是为了防止空指针异常,
Optional<Article> byId = articleRepository.findById(2l);
Article article = byId.get();
System.out.println(byId);
System.out.println(article);
//输出的结果一样
}
@Test
public void findByTitle(){
List<Article> list = articleRepository.findByTitle("springdata");
for (int i = 0; i < list.size(); i++) {
Article article = list.get(i);
System.out.println(article);
}
}
@Test
public void findByTitleOrContent(){
List<Article> list = articleRepository.findByTitleOrContent("spring","方便");
for (int i = 0; i < list.size(); i++) {
Article article = list.get(i);
System.out.println(article);
}
}
@Test
public void findByTitleOrContentPage(){
Pageable pageable= PageRequest.of(0,15);//从0页开始,每页15行
List<Article> list = articleRepository.findByTitleOrContent("spring","方便",pageable);
for (int i = 0; i < list.size(); i++) {
Article article = list.get(i);
System.out.println(article);
}
}
}
【5】自定义方法
???????? 在ArticleRespository(继承了ElasticSearchRespository的接口)中提供自定义方法
???????? 例如FindByTitle,这些都是需要在这个接口中声明,才可以调用的。
?
|