本文共 5397 字,大约阅读时间需要 17 分钟。
4.0.0 org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE pers.zhang sb_elasticsearch 0.0.1-SNAPSHOT sb_elasticsearch Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-data-elasticsearch org.springframework.boot spring-boot-starter-test test junit junit 4.12 org.springframework.boot spring-boot-maven-plugin
application.yml
spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 127.0.0.1:9300 repositories: enabled: true
@SpringBootApplication@EnableElasticsearchRepositories(basePackages = "pers.zhang")public class SbElasticsearchApplication { public static void main(String[] args) { SpringApplication.run(SbElasticsearchApplication.class, args); }}
package pers.zhang.sb_elasticsearch.entity;import org.springframework.data.annotation.Id;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;/** * @Author: acton_zhang * @Date: 2020/3/14 3:30 下午 * @Version 1.0 */@Document(indexName = "index-article", 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; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + '}'; }}
public interface ArticleRepository extends ElasticsearchRepository{ //根据标题进行查询 List findByTitle(String title); List findByTitleOrContent(String title, String context); List findByTitleOrContent(String title, String context, Pageable pageable);}
@SpringBootTest(classes = SbElasticsearchApplication.class)@RunWith(SpringJUnit4ClassRunner.class)public class SbElasticsearchApplicationTests { @Autowired ArticleRepository articleRepository; @Autowired ElasticsearchTemplate elasticsearchTemplate; @Test public void createIndex() { elasticsearchTemplate.createIndex(Article.class); } @Test public void addDocument () { Article article = new Article(); article.setId(3l); article.setTitle("Maven对象模型"); article.setContent("佛教圣地六块腹肌塑料袋放假了撒"); articleRepository.save(article); } @Test //查询所有 public void findAll () { Iterableall = articleRepository.findAll(); all.forEach( a -> System.out.println(a)); } @Test //根据id查询 public void findById () { Optional byId = articleRepository.findById(1l); System.out.println(byId.get()); } @Test public void findByTitle () { List list = articleRepository.findByTitle("对象"); list.forEach( a -> System.out.println(a)); } @Test //多条件查询 public void findByTitleOrContent () { List list = articleRepository.findByTitleOrContent("ddd", "放假"); list.forEach( a -> System.out.println(a)); } @Test //分页查询 public void findByPage () { Pageable pageable = PageRequest.of(0, 2); List list = articleRepository.findByTitleOrContent("ddd", "放假", pageable); list.forEach( a -> System.out.println(a)); } //原生查询 @Test public void testNativeSearchQuery () { NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("佛放假").defaultField("content")) .withPageable(PageRequest.of(0, 5)) .build(); List articles = elasticsearchTemplate.queryForList(query, Article.class); articles.forEach(a -> System.out.println(a)); } @Test public void deleteById () { articleRepository.deleteById(1l); }}
转载地址:http://pspqb.baihongyu.com/