SpringDateElasticSearch
一.工程搭建
1.新建项目
2.导入jar包
<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>
<scope>test</scope>
</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>
3.创建springdate的applicationConfig.xml配置文件
<?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
">
<!--es客户端对象的配置-->
<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"></elasticsearch:transport-client>
<!-- 配置包扫描器-->
<elasticsearch:repositories base-package="cn.xkm.es.repositories"></elasticsearch:repositories>
<!-- ES模板对象 -->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"></constructor-arg>
</bean>
</beans>
二.管理索引库
1.操作前准备
1.创建一个Entity类对象,就是一个javaBean映射到Document,需要添加一些注解进行标注。
//定义在哪个索引哪个type下
@Document(indexName = "sdes_blog",type = "article")
public class Article {
@Id
@Field(type = FieldType.Long,store = true)
private long id;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String content;
2.创建一个Dao,是一个接口,需要继承ElasticSearchRepository接口。
// 定义该Dao要对哪个类进行操作,主键是Long类型
//方法不需要定义,继承的接口中有常用的方法
public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
}
2.测试代码
2.1 创建索引库
//用spring-test包的注解测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElaticSearchTest {
@Autowired
private ArticleRepository articleRepository;
//导入Spring配置文件中的ES模板对象
@Autowired
private ElasticsearchTemplate template;
@Test
public void createIndex() throws Exception{
// 创建索引并配置映射关系
template.createIndex(Article.class);
// 配置映射关系
// template.putMapping(Article.class);
}
}
2.2 添加文档记录
1)创建一个Article对象
2)使用ElasticSearchRepository对象向索引库中添加文档
//用spring-test包的注解测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElaticSearchTest {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void saveIndex() throws Exception{
Article article = new Article();
article.setId(1);
article.setTitle("第一个");
article.setContent("这是我的第一个SDES添加的文档");
articleRepository.save(article);
}
}
2.3 删除文档记录
@Test
public void deleteIndex() throws Exception{
articleRepository.deleteById(1l);
}
2.4 修改文档内容
添加一个ID相同,内容不同的文档即可覆盖。
原理是:自动进行先删除后添加
2.5 简单查询
// 查询所有
@Test
public void findAll() throws Exception{
Iterable<Article> all = articleRepository.findAll();
all.forEach(a-> System.out.println(a));
}
// 根据ID查询一个
@Test
public void findById() throws Exception{
Optional<Article> optional = articleRepository.findById(2l);
Article article = optional.get();
System.out.println(article);
}
2.6 自定义查询及分页
1.在自定义的接口中,按照标准的命名规范创建方法,不需要实现,直接可用
2.如果需要定义分页信息,要在方法的参数中加上Pageable对象
利用Pageable的子类PageRequest配置好分页信息,再传入方法中即可:
Pageable pageable = PageRequest.of(0,3);
public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
List<Article> findByTitle(String title);
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}
@Test
public void findByTitle() throws Exception{
List<Article> byTitle = articleRepository.findByTitle("第02个");
byTitle.stream().forEach(a -> System.out.println(a));
}
@Test
public void findByTitleOrContent() throws Exception{
Pageable pageable = PageRequest.of(0,3);
List<Article> byTitle = articleRepository.findByTitleOrContent("第02个","这是",pageable);
byTitle.stream().forEach(a -> System.out.println(a));
}
2.7 使用原生查询(如:QueryString)
@Test
public void testNativeSearchQuery() throws Exception{
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("第一个").defaultField("title"))
.withPageable(PageRequest.of(0,3))
.build();
List<Article> articles = template.queryForList(query, Article.class);
articles.forEach(article -> System.out.println(article));
}