elasticsearch之hello(spring data整合)
Posted fjk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch之hello(spring data整合)相关的知识,希望对你有一定的参考价值。
1.书写pom.xml文件
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.8.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies>
2.applicationContext.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 "> <!-- 扫描DAO包 自动创建实现 --> <elasticsearch:repositories base-package="com.baidu.dao" /> <!-- 扫描Service包 --> <context:component-scan base-package="com.baidu.service" /> <!-- 配置elasticsearch 连接 --> <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" /> <!-- spring data elasticsearch DAO 必须依赖 elasticsearchTemplate --> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client" /> </bean> </beans>
Demo01.java
package com.baidu.test01; import org.elasticsearch.client.Client; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baidu.domain.Article; import com.baidu.service.ArticleService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class Demo01 { @Autowired private ArticleService articleService; @Autowired private Client client; // 基于原生API @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Test public void createIndex() { //创建索引 elasticsearchTemplate.createIndex(Article.class); //创建映射 elasticsearchTemplate.putMapping(Article.class); } @Test //创建文档 public void createDoc() { for (int i = 2; i < 20; i++) { Article ac=new Article(); ac.setId(i); ac.setTitle("testSave"+i); ac.setContent("仅仅只是在测试test"+i); //文档操作 articleService.save(ac); } } @Test //查询文档 public void searchDoc() { //文档操作 articleService.findAll(); } @Test //查询文档 public void searchDoc01() { //文档操作 articleService.findById(1); } @Test //查询文档 public void searchDoc02() { //模糊查询 articleService.findByTitle("testsave"); } @Test //查询文档 public void searchDoc03() { PageRequest pr=new PageRequest(0, 4,new Sort(Direction.DESC, "id")); //分页查询 按照降序排序 articleService.findAll(pr); } }
ArticleService.java文件(接口)
package com.baidu.service; import org.springframework.data.domain.PageRequest; import com.baidu.domain.Article; public interface ArticleService { void save(Article ac); void findAll(); void findById(Integer i); void findAll(PageRequest pr); void findByTitle(String string); }
ArticleService实现类
package com.baidu.service.imp; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.WildcardQueryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.stereotype.Service; import com.baidu.dao.ArticleDao; import com.baidu.domain.Article; import com.baidu.service.ArticleService; @Service public class ArticleServiceImp implements ArticleService{ @Autowired private ArticleDao articleDao; public void save(Article ac) { articleDao.save(ac); } public void findAll() { Iterable<Article> all = articleDao.findAll(); for (Article article : all) { System.out.println(article); } } public void findById(Integer i) { Article article = articleDao.findById(i); System.out.println(article); } public void findAll(PageRequest pr) { Page<Article> page = articleDao.findAll(pr); System.out.println(page.getContent()); } public void findByTitle(String string) { //需要根据内容的分词进行查询 QueryBuilder query= new WildcardQueryBuilder("title", "*"+string+"*"); SearchQuery searchQuery = new NativeSearchQuery(query); PageRequest pr=new PageRequest(0, 4,new Sort(Direction.DESC, "id")); searchQuery.setPageable(pr); Page<Article> page = articleDao.search(searchQuery); System.out.println(page.getContent()); } }
ArticleDao.java
package com.baidu.dao; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import com.baidu.domain.Article; public interface ArticleDao extends ElasticsearchRepository<Article, Integer> { Article findById(Integer i); }
以上是关于elasticsearch之hello(spring data整合)的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch之hello(spring data整合)
Elasticsearch 学习+SpringBoot实战教程
Elasticsearch学习 spring boot整合Elasticsearch的原生方式