1.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 "> <elasticsearch:transport-client id="client" cluster-name="my-elasticsearch" cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean> <elasticsearch:repositories base-package="cn.itcast.repository"/> </beans>
2.pojo
@Document(indexName = "spring_test",type = "article") public class Article { @Id @Field(store = true,index = false,type= FieldType.Long) private long id; @Field(store = true,index = true,analyzer = "ik_smart",type=FieldType.text) private String title; @Field(store = true,index = true,analyzer = "ik_smart",type=FieldType.text) private String content; //getter setter方法
3.dao 继承ElasticsearchRepository接口,该接口中有默认方法,还可自定义规则命名方法
package cn.itcast.repository; import cn.itcast.domain.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface ArticleRepository extends ElasticsearchRepository<Article,Long> { public List<Article> findByTitle(String title); public List<Article> findByContentOrTitle(String content, String title, Pageable pageable); }
4.测试:
package cn.itcast; import cn.itcast.domain.Article; import cn.itcast.repository.ArticleRepository; import org.elasticsearch.index.query.QueryBuilders; 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.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleRepository articleRepository; @Autowired private ElasticsearchTemplate template; @Test public void createIndex() throws Exception{ template.createIndex(Article.class); } @Test public void addDocument() throws Exception{ for (int i = 1; i <= 100; i++) { Article article = new Article(); article.setId(i); article.setTitle("java垃圾回收"+i); article.setContent("JAVA特有功能,垃圾收集意味着程序不再需要的对象是\"无用信息\",这些信息将被丢弃回收。"+i); articleRepository.save(article); } } @Test public void findById() throws Exception{ Optional<Article> optional = articleRepository.findById(1l); if(optional.isPresent()){ Article article = optional.get(); System.out.println(article); } } @Test public void delById() throws Exception{ articleRepository.deleteById(14l); } //查询所有,返回所有数据 @Test public void findAll() throws Exception{ Iterable<Article> all = articleRepository.findAll(); for (Article article : all) { System.out.println(article); } } //按照规则命名的方法查询结果默认只会有10条 @Test public void findByTitle() throws Exception{ List<Article> list = articleRepository.findByTitle("垃圾回收"); for (Article article : list) { System.out.println(article); } } @Test public void findByContentOrTitle() throws Exception{ Pageable pageable = PageRequest.of(1,5 ); List<Article> byContentOOrTitle = articleRepository.findByContentOrTitle("功能", "垃圾", pageable); for (Article article : byContentOOrTitle) { System.out.println(article); } } //本地查询 @Test public void nativeQuery() throws Exception{ NativeSearchQuery query =new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("java不是垃圾回收").defaultField("title")) .withPageable(PageRequest.of(0,30 )) .build(); List<Article> articles = template.queryForList(query, Article.class); for (Article article : articles) { System.out.println(article); } } }