1.引入maven坐标
<!--spring-data-elasticsearch-->
<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>
<!--elasticsearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<!--transport-->
<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>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<!--json-->
<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>
2.配置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: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
">
<!-- 配置elasticSearch客户端的连接 -->
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9301,localhost:9302,localhost:9303" cluster-name="my-application"/>
<!-- 扫描Dao包,自动创建实例 -->
<elasticsearch:repositories base-package="com.caizhen.springdata.elasticsearch.dao"/>
<!-- 扫描Service包,创建Service的实体 -->
<context:component-scan base-package="com.caizhen.springdata.elasticsearch.service"/>
<!-- ElasticSearch模版对象 -->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"></constructor-arg>
</bean>
</beans>
3.配置dao
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Integer> {
}
4.配置service
@Service public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
//新建索引
public void creatIndex() {
//创建索引,并配置映射关系
elasticsearchTemplate.createIndex(Article.class);
//创建映射关系
// elasticsearchTemplate.putMapping(Article.class);
}
//保存文档
public void save(Article article) {
articleRepository.save(article);
}
//更新文档,ealasticsearch根据id判断该数据是否存在若存在则进行修改
@Test
public void update(){
Article article = new Article();
article.setId(1001);
article.setTitle("elasticSearch 3.0版本发布...更新");
article.setContent("ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口");
articleService.save(article);
}
//删除文档
public void delete(){
Article article = new Article();
article.setId(1001);
articleService.delete(article);
}
//分页查询
@Test
public void findAllPage(){
Pageable pageable = PageRequest.of(1,10);//page,pagesize,初始页是从0开始
Page<Article> page = articleService.findAll(pageable);
for(Article article:page.getContent()){
System.out.println(article);
}
}
}
3.3.2 常用查询命名规则
| 关键字 | 命名规则 | 解释 | 示例 |
| and | findByField1AndField2 | 根据Field1和Field2获得数据 | findByTitleAndContent |
| or | findByField1OrField2 | 根据Field1或Field2获得数据 | findByTitleOrContent |
| is | findByField | 根据Field获得数据 | findByTitle |
| not | findByFieldNot | 根据Field获得补集数据 | findByTitleNot |
| between | findByFieldBetween | 获得指定范围的数据 | findByPriceBetween |
| lessThanEqual | findByFieldLessThan | 获得小于等于指定值的数据 | findByPriceLessThan |
3.3.3 查询方法测试
1)dao层实现
public interface ArticleRepository extends ElasticsearchRepository<Article, Integer> {
//根据标题查询
List<Article> findByTitle(String condition);
//根据标题查询(含分页)
Page<Article> findByTitle(String condition, Pageable pageable);
}
2)service实现
public interface ArticleService {
//根据标题查询
List<Article> findByTitle(String condition);
//根据标题查询(含分页)
Page<Article> findByTitle(String condition, Pageable pageable);
}
3)service层实现
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
public List<Article> findByTitle(String condition) {
return articleRepository.findByTitle(condition);
}
public Page<Article> findByTitle(String condition, Pageable pageable) {
return articleRepository.findByTitle(condition,pageable);
}
}
4)单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringDataESTest {
@Autowired
private ArticleService articleService;
@Autowired
private TransportClient client;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**条件查询*/
@Test
public void findByTitle(){
String condition = "版本";
List<Article> articleList = articleService.findByTitle(condition);
for(Article article:articleList){
System.out.println(article);
}
}
/**条件分页查询*/
@Test
public void findByTitlePage(){
String condition = "版本";
Pageable pageable = PageRequest.of(2,10);
Page<Article> page = articleService.findByTitle(condition,pageable);
for(Article article:page.getContent()){
System.out.println(article);
}
}
/**条件分页查询*/
@Test
public void findByTitlePage(){
String condition = "版本";
Pageable pageable = PageRequest.of(2,10);
Page<Article> page = articleService.findByTitle(condition,pageable);
for(Article article:page.getContent()){
System.out.println(article);
}
}
}
5)使用Elasticsearch的原生查询对象进行查询。
@Test
public void findByNativeQuery() {
//创建一个SearchQuery对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
//设置查询条件,此处可以使用QueryBuilders创建多种查询
.withQuery(QueryBuilders.queryStringQuery("SpringData").defaultField("title"))
//还可以设置分页信息
.withPageable(PageRequest.of(0, 5))
//创建SearchQuery对象
.build();
//使用模板对象执行查询
List<Article> articleList= elasticsearchTemplate.queryForList(searchQuery, Article.class);
for(Article list: articleList){
System.out.println(list);
}
}
6)总结
自定义方法可以对搜索的内容先分词再进行查询,每个词之间条件都是and的关系,一个词分词后的片段必须都同时满足
原生方法是or的关系