public class Tutorial implements Serializable{
private Long id;
private String name;//教程名称
//setters and getters
//toString
}
public class Author implements Serializable{
/**
* 作者id
*/
private Long id;
/**
* 作者姓名
*/
private String name;
/**
* 作者简介
*/
private String remark;
//setters and getters
//toString
}
public class Article implements Serializable{
private Long id;
/**标题*/
private String title;
/**摘要*/
private String abstracts;
/**内容*/
private String content;
/**发表时间*/
private Date postTime;
/**点击率*/
private Long clickCount;
/**作者*/
private Author author;
/**所属教程*/
private Tutorial tutorial;
//setters and getters
//toString
}
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.3.0.RELEASE </version>
</parent>
<dependencies>
<!-- 添加 web 应用的依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<!-- 添加 spring-data-elasticsearch的依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-data-elasticsearch </artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId> spring-boot-starter-test </artifactId>
</dependency>
</dependencies>
spring:
data:
elasticsearch:
cluster-name: #默认为elasticsearch
cluster-nodes: #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
properties:
path:
logs: ./elasticsearch/log #elasticsearch日志存储目录
data: ./elasticsearch/data #elasticsearch数据存储目录
@Document(indexName="projectname",type="article",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1")
public class Article implements Serializable{
....
}
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.tianshouzhi.springbootstudy.domain.Article;
//泛型的参数分别是实体类型和主键类型
public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long>{
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ElasticSearchTest {
@Autowired
private ArticleSearchRepository articleSearchRepository;
@Test
public void testSaveArticleIndex(){
Author author=new Author();
author.setId(1L);
author.setName("tianshouzhi");
author.setRemark("java developer");
Tutorial tutorial=new Tutorial();
tutorial.setId(1L);
tutorial.setName("elastic search");
Article article =new Article();
article.setId(1L);
article.setTitle("springboot integreate elasticsearch");
article.setAbstracts("springboot integreate elasticsearch is very easy");
article.setTutorial(tutorial);
article.setAuthor(author);
article.setContent("elasticsearch based on lucene,"
+ "spring-data-elastichsearch based on elaticsearch"
+ ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");
article.setPostTime(new Date());
article.setClickCount(1L);
articleSearchRepository.save(article);
}
}
@Test
public void testSearch(){
String queryString="springboot";//搜索关键字
QueryStringQueryBuilder builder=new QueryStringQueryBuilder(queryString);
Iterable<Article> searchResult = articleSearchRepository.search(builder);
Iterator<Article> iterator = searchResult.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
spring:
data:
elasticsearch:
cluster-nodes:115.28.65.149:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有