资料查找说明
田守枝java技术博客:/api/tutorials/springboot/101
fantasic_van的博客:/fantasic_van/article/details/79309665
天涯泪小武的博客:/tianyaleixiaowu/article/details/72833940
Spring Data Elasticsearch:
/spring-data/elasticsearch/docs/current/reference/html/#project
小盒子的博客:/m0_37044606/article/details/79793125
官方文档:/guide/en/elasticsearch/reference/current/
1 项目目录
2 整合ElasticSearch
2.1 model
案例背景:每个文章(Article)都要属于一个教程(Tutorial),而且每个文章都要有一个作者(Author)。
public class Tutorial implements Serializable{
private Long id; //教程id
private String name;//教程名称
//setters and getters
//toString
}
public class Author implements Serializable {
private Long id; //作者id
private String name; //作者姓名
private String remark; //作者简介
//setters and getters
//toString
}
@Document(indexName="ESDome",type="article",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1")
public class Article implements Serializable {
@Id
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
}
2.2 dao
public interface ArticleSearchRepository extends ElasticsearchRepository<Article, Long> {}
2.3 controller
简单实现一个新增文章和搜索关键字的功能:
@RestController
public class EsController {
@Autowired
private ArticleSearchRepository articleSearchRepository;
@RequestMapping("/add")
public void testSaveArticleIndex() {
Author author = new Author();
(1L);
("tianshouzhi");
("java developer");
Tutorial tutorial = new Tutorial();
(1L);
("elastic search");
Article article = new Article();
(1L);
("springboot integreate elasticsearch");
("springboot integreate elasticsearch is very easy");
(tutorial);
(author);
("elasticsearch based on lucene,"
+ "spring-data-elastichsearch based on elaticsearch"
+ ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");
(new Date());
(1L);
(article);
}
@RequestMapping("/query")
public void testSearch() {
String queryString = "springboot";//搜索关键字
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString);
Iterable<Article> searchResult = (builder);
Iterator<Article> iterator = ();
while (()) {
(());
}
}
}
报错如下:
failed to load elasticsearch nodes :
:
None of the configured nodes are available:
[{#transport#-1}{nO9E5fmjSoWVUxduQ_MZDg}{localhost}{127.0.0.1:9300}]
解决办法:将elasticsearch设置为windows系统服务,并开启
3 测试
3.1 add
在浏览器上输入URL:http://localhost:8080/add
使用chrome插件Sense查看结果:
3.2 查询
在浏览器上输入URL:http://localhost:8080/query
在控制台上显示:
4 增加service及其实现类
4.1 项目结构
把control类的内容迁移到service上
@RestController
public class EsController {
@Autowired
private EsService esServiceImpl;
@RequestMapping("/add")
public void testSaveArticleIndex() {
();
}
@RequestMapping("/query")
public void testSearch() {
();
}
}
public interface EsService {
void query();
void save();
void delete();
}
@Service("esServiceImpl")
public class EsServiceImpl implements EsService {
@Autowired
private EsDao esDao;
@Override
public void query() {
// TODO Auto-generated method stub
String queryString = "springboot";//搜索关键字
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(queryString);
Iterable<Article> searchResult = (builder);
Iterator<Article> iterator = ();
while (()) {
(());
}
("query");
}
@Override
public void save() {
// TODO Auto-generated method stub
Author author = new Author();
(1L);
("mobai");
("java developer");
Tutorial tutorial = new Tutorial();
(1L);
("elastic search");
Article article = new Article();
(1L);
("springboot integreate elasticsearch");
("springboot integreate elasticsearch is very easy");
(tutorial);
(author);
("elasticsearch based on lucene,"
+ "spring-data-elastichsearch based on elaticsearch"
+ ",this tutorial tell you how to integrete springboot with spring-data-elasticsearch");
(new Date());
(1L);
(article);
}
@Override
public void delete() {
// TODO Auto-generated method stub
("delete");
}
}
4.2 相关问题
在项目过程中,出现以下问题:
Description:
Field esServiceImpl in required a bean of type '' that could not be found.
Action:
Consider defining a bean of type '' in your configuration
通过网上教程,修改后,访问浏览器时,页面出现以下问题:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
……
There was an unexpected error (type=Not Found, status=404).
No message available
通过查阅文档等途径,在这个网址上得到启发:/p/6c3b456b0511
分别在以下两个类中添加注释:
@Component("esDao")
public interface EsDao extends ElasticsearchRepository<Article, Long> {}
@Service("esServiceImpl")
public class EsServiceImpl implements EsService{……}
4.3
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>ESdemo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>ESdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<>UTF-8</>
<>UTF-8</>
<>1.8</>
</properties>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId></groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>