目录
步骤 1 : 版本问题
步骤 2 : 启动 elasticsearch 2.4.2
步骤 3 : 先运行,看到效果,再学习
步骤 4 : 模仿和排错
步骤 5 : 创建 springboot 项目
步骤 6 : pom.xml
步骤 7 : Application.java
步骤 8 : Category
步骤 9 : CategoryDAO
步骤 10 : 控制类
步骤 11 : listCategory.jsp
步骤 12 : editCategory.jsp
步骤 13 : application.properties
步骤 14 : 启动并测试
步骤 15 : kibana
步骤 16 : 启动 kibana 并访问
步骤 17 : 选择索引
步骤 18 : 查看数据
步骤 1 : 版本问题
springboot 有一个 spring data 组件,可以用来连接各种数据源。 用来连接 elasticsearch 的是 spring-data-elasticsearch。 
 但是呢。。。 截止现在(2018-9-17),spring-data-elasticsearch 更新比较慢,其最高版本无法支持教程里的 elasticsearch的6.x 版本。 
 为了支持 6.x 版本,需要用要用奇怪的 transportclient 来连接, 这就不是 spring-data 系列里的内容了。
 考虑到将来,spring data 总会支持最新版本的 elasticsearch的,所以我们还是使用 spring-data 来进行链接。 只是,elasticsearch 的版本,我们换成了 2.4.2, kibana 版本,也换成了能够连接 elasticsearch 2.4.2 的 4.6.3 版本。
 这两个新的版本,都在下载区(点击进入)提供了下载。

步骤 2 : 启动 elasticsearch 2.4.2
首先关闭本系列教材可能启动的 elasticsearch 6.2.2。
 然后下载并解压下载区(点击进入)的 elasticsearch 2.4.2,运行其中的 elasticsearch.bat
 启动后,可以看到左上角的版本号。

步骤 3 : 先运行,看到效果,再学习
老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
 首先确保 启动 elasticsearch 2.4.2 这个步骤要做了,不然跑不起来。
 然后运行 Application.java 启动项目
 接着访问地址:
| http://127.0.0.1:8080/listCategory | 
 CRUD和分页效果都有。

 正在上传…重新上传取消
正在上传…重新上传取消

步骤 4 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 
 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 5 : 创建 springboot 项目
创建方式在 springboot 模块里, eclipse 和 idea 都有:
eclipse 方式创建 springboot 项目 
idea 方式创建 springboot 项目
步骤 6 : pom.xml
各种jar包,主要是 spring-boot-starter-data-elastisearch 这个 jar包
| <project xmlns="http:///POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd"> 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 | 
步骤 7 : Application.java
启动类
| package com.how2java.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } | 
步骤 8 : Category
Category 实体类,其中的 @Document就表明了要连接到 ElasticSearch 的哪个索引和哪个 type 上
| @Document(indexName = "how2java",type = "category") | 
 索引相当于就是数据库,type 相当于就是表
| package com.how2java.springboot.pojo; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "how2java",type = "category") public class Category { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { = name; } } | 
步骤 9 : CategoryDAO
继承了,就什么东西都有了~
| package com.how2java.springboot.dao; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import com.how2java.springboot.pojo.Category; public interface CategoryDAO extends ElasticsearchRepository } | 
步骤 10 : 控制类
控制类提供 CRUD 一套
| package com.how2java.springboot.web; import java.text.SimpleDateFormat; import java.util.Date; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.how2java.springboot.dao.CategoryDAO; import com.how2java.springboot.pojo.Category; @Controller public class CategoryController { @Autowired CategoryDAO categoryDAO; //每页数量 @GetMapping("/listCategory") public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size){ String query = "商品"; //查询条件,但是并未使用,放在这里,为的是将来使用,方便参考,知道如何用 SearchQuery searchQuery=getEntitySearchQuery(start,size,query); Page m.addAttribute("page", page); return "listCategory"; } private SearchQuery getEntitySearchQuery(int start, int size, String searchContent) { FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery() .add(QueryBuilders.matchAllQuery(), //查询所有 ScoreFunctionBuilders.weightFactorFunction(100)) // 查询条件,但是并未使用,放在这里,为的是将来使用,方便参考,知道如何用 // .add(QueryBuilders.matchPhraseQuery("name", searchContent), // ScoreFunctionBuilders.weightFactorFunction(100)) //设置权重分 求和模式 .scoreMode("sum") //设置权重分最低分 .setMinScore(10); // 设置分页 Sort sort = new Sort(Sort.Direction.DESC,"id"); Pageable pageable = new PageRequest(start, size,sort); return new NativeSearchQueryBuilder() .withPageable(pageable) .withQuery(functionScoreQueryBuilder).build(); } @RequestMapping("/addCategory") public String addCategory(Category c) throws Exception { int id = currentTime(); c.setId(id); categoryDAO.save(c); return "redirect:listCategory"; } private int currentTime() { SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss"); String time= sdf.format(new Date()); return Integer.parseInt(time); } @RequestMapping("/deleteCategory") public String deleteCategory(Category c) throws Exception { categoryDAO.delete(c); return "redirect:listCategory"; } @RequestMapping("/updateCategory") public String updateCategory(Category c) throws Exception { categoryDAO.save(c); return "redirect:listCategory"; } @RequestMapping("/editCategory") public String ediitCategory(int id,Model m) throws Exception { Category c= categoryDAO.findOne(id); m.addAttribute("c", c); return "editCategory"; } } | 
步骤 11 : listCategory.jsp
listCategory.jsp 文件,记得放在如图所示的位置。没有的目录,自己创建就好了。

| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | 
| id | 
| name | 
| 编辑 | 
| 删除 | 
| ${} | 
| ${} | 
| 编辑 | 
| 删除 | 
    
    
    name:
步骤 12 : editCategory.jsp
editCategory.jsp 做编辑工作
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> name: | 
步骤 13 : application.properties
配置 jsp 作为视图
 配置spring端口 为8080
 配置 elastic链接地址为 127.0.0.1:9300

| spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp server.port=8080 spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 | 
步骤 14 : 启动并测试
运行 Application.java 启动项目
 接着访问地址:
| http://127.0.0.1:8080/listCategory | 

步骤 15 : kibana
下面用 kibana 来查看数据。 和前面的 kibana 版本不同,要连接本教材的 elasticsearch 2.4.2 版本,需要用到下载区(点击进入)提供的 kibana 4.6.3 版本。
注: 如果启动了前面提供的 kibana 6.2.2 ,请记得关闭。

步骤 16 : 启动 kibana 并访问
启动之后,如图所示,是看不到版本信息的。。
 然后访问地址:
| http://127.0.0.1:5601 | 

步骤 17 : 选择索引
刚开始是没有选定索引的,所以要自己指定索引。
 1. 把默认勾选的 Index contians time-based evens 去掉
 2. 输入 how2java
 3. 点击 Create 按钮

步骤 18 : 查看数据
然后点击上面的Discover,就可以看到左边是当前的索引 :how2java. 右边就是数据了。。。

 
 
                     
            
        













 
                    

 
                 
                    