记录一下elasticsearch的基础用法,高级用法后续再说。
话不多说,开始。
1、springboot 版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
注:为了spring boot和elasticsearch兼容,这里使用1.5.6
2、elasticsearch 版本:2.4.5
2.1、官网下载elasticsearch对应版本,解压,进入bin文件夹,执行 sh elasticsearch命令启动elasticsearch,可以看到两个地址
如图所示:9300端口下文将会在application.yml中用到,9200端口可以在浏览器或命令行中用到,是elasticsearch提供的rest接口。
2.2、下载elasticsearch-head,解压,进入目录,执行npm start,可以在浏览器中查看elasticsearch保存的数据,在启动前需要在elasticsearch.yml中配置两句话来解决跨域问题。
# allow origin
http.cors.enabled: true
http.cors.allow-origin: "*"
执行npm start启动elasticsearch-head插件,可以看见
3、pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
4、配置application.yml
spring:
data:
elasticsearch:
clusterName: spring-boot
# 此处为elasticsearch提供的java接口
cluster-nodes: 127.0.0.1:9300
properties:
path:
# 配置工程与外部elasticsearch链接
home: /Users/CrazyMouse/Desktop/open-source/elasticsearch-2.4.5
# logs: ./elasticsearch/log
# data: ./elasticsearch/data
5、创建3个POJO
5.1、Article.java
//指定Article的索引和类型,相当于关系型数据库中的DB和TABLE
@Document(indexName = "project-name", type = "article")
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;
//getter...
//setter...
//toString...
5.2、Author.java
public class Author implements Serializable {
private Long id;
private String name;
private String remark;
//getter...
//setter...
//toString...
5.3、Tutorial.java
public class Tutorial implements Serializable {
private Long id;
private String name;
//getter...
//setter...
//toString...
6、创建Repository,只需集成ElasticsearchRepository即可
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
}
7、测试
@RestController
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
@GetMapping("/article/{id}")
public Article getArticle(@PathVariable Long id) {
return articleRepository.findOne(id);
}
@GetMapping("/article")
public List<Article> getArticles(@RequestParam String author_name) {
QueryStringQueryBuilder builder = new QueryStringQueryBuilder(author_name);
Iterable<Article> result = articleRepository.search(builder);
Iterator<Article> it = result.iterator();
List<Article> data = new LinkedList<>();
while (it.hasNext()) {
data.add(it.next());
}
return data;
}
@PostMapping("/article")
public Article saveArticle(@RequestBody Article article) {
article.setPostTime(new Date());
articleRepository.save(article);
return article;
}
@DeleteMapping("/article/{id}")
public int deleteArticle(@PathVariable Long id) {
articleRepository.delete(id);
return 1;
}
}
测试结果可以在elasticsearch-head插件中查看。