- Springboot整合ElasticSearch
a、提前使用kibana创建索引和字段类型
// 记住类型要设置'text',且搜索引擎设置ik_smart,否则不会进行分词// 创建索引 PUT /tradeindex // 创建字段类型 POST /tradeindex/_mapping/trade { "trade": { "properties": { "name": { "type": "text", "analyzer": "ik_smart" }, "desc": { "type": "text", "analyzer": "ik_smart" }, "price": { "type": "double" }, "createTime": { "type": "long" }, "storeName": { "type": "keyword" }, "storeAddress": { "type": "keyword" } } } }
b、创建好数据
### 3、创建商品数据 POST /tradeindex/trade { "name": "白色耳机", "desc": "经典白色耳机", "price": "15.6", "createTime": 1567043300836, "storeName": "唯品会", "storeAddress": "安徽省合肥市" } POST /tradeindex/trade { "name": "蓝色耳机", "desc": "经典蓝色耳机", "price": "16.6", "createTime": 1567043400836, "storeName": "平多多", "storeAddress": "安徽省六安市" } POST /tradeindex/trade { "name": "红色蓝牙耳机", "desc": "经典红色蓝牙耳机", "price": "17.6", "createTime": 1567043400836, "storeName": "美团", "storeAddress": "江西省九江市" } POST /tradeindex/trade { "name": "白色蓝牙耳机", "desc": "经典白色蓝牙耳机", "price": "18.6", "createTime": 1567043400836, "storeName": "滴滴", "storeAddress": "江西省九江市" } POST /tradeindex/trade { "name": "黑色蓝牙无线耳机", "desc": "经典黑色蓝牙无线耳机", "price": "18.6", "createTime": 1567043400836, "storeName": "京东", "storeAddress": "江西省赣州市" } POST /tradeindex/trade { "name": "白色蓝牙无线耳机", "desc": "经典白色蓝牙无线耳机", "price": "18.6", "createTime": 1567043400836, "storeName": "淘宝", "storeAddress": "江西省南昌市" } // 查询所有数据 GET /tradeindex/trade/_search
- 创建springboot项目,在pom.xml中引入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0-rc2</version> </dependency> <!-- springboot整合freemarker --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> </dependencies>
- application.yml配置如下
spring: data: elasticsearch: ####集群名称 cluster-name: mytest ####地址 cluster-nodes: 192.168.0.111:9300 freemarker: # 设置模板后缀名 suffix: .ftl # 设置文档类型 content-type: text/html # 设置页面编码格式 charset: UTF-8 # 设置页面缓存 cache: false # 设置ftl文件路径 template-loader-path: - classpath:/templates # 设置静态文件路径,js,css等 mvc: static-path-pattern: /static/**
- 定义三层架构--实体类TradeEntity.java
import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; /** * 创建商品实体类 * Create by wangxb * 2019-09-11 7:29 */ @Data @Document(indexName = "tradeindex", type = "trade") // 定义ES索引名称和类型 public class TradeEntity { @Id private String id; // 商品名称 private String name; // 商品描述 private String desc; // 商品价格 private Double price; // 创建时间时间 private Long createTime; // 商店名称 private String storeName; // 商店地址 private String storeAddress; }
- 定义三层架构-dao层TradeDao .java
import com.basic.entity.TradeEntity; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /** * Create by wangxb * 2019-09-11 7:32 */ public interface TradeDao extends ElasticsearchRepository<TradeEntity, String> { }
- 定义三层架构-控制层TradeController .java
import com.basic.entity.TradeEntity; import com.basic.repository.TradeDao; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.Optional; /** * Create by wangxb * 2019-09-11 7:36 */ @Controller public class TradeController { @Autowired private TradeDao tradeDao; // 根据id查询文档信息 @RequestMapping("/findById/{id}") @ResponseBody public Optional<TradeEntity> findById(@PathVariable String id) { return tradeDao.findById(id); } // 实现分页查询 @RequestMapping("/search") public String search(@RequestParam(value = "name",defaultValue = "") String name ,@RequestParam(value="desc", defaultValue = "") String desc ,@PageableDefault(page = 0, value = 2) Pageable pageable ,HttpServletRequest request) { Long startTime = System.currentTimeMillis(); // 1.创建查询对象, 查询所有数据 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); // 2、判断查询参数 if (!StringUtils.isEmpty(name)) { MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", name); boolQuery.must(matchQuery); } if (!StringUtils.isEmpty(desc)) { MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("desc", desc); boolQuery.must(matchQuery); } // 3、调用查询接口 Page<TradeEntity> page = tradeDao.search(boolQuery, pageable); // 4、返回参数给页面 request.setAttribute("page", page); request.setAttribute("total", page.getTotalElements()); request.setAttribute("name", name); request.setAttribute("desc", desc); // 5、计算分页总数 int totalPage = (int) ((page.getTotalElements() - 1) / pageable.getPageSize() + 1); request.setAttribute("totalPage", totalPage); Long endTime = System.currentTimeMillis(); System.out.println("查询总耗时:"+(endTime - startTime)); return "search"; } }
- 在resources/templates创建页面search.ftl
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>商品搜索</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script> </head> <body style="display: block; margin: 0 auto; width: 50%; " > <div class="bs-example" data-example-id="striped-table"> <table class="table table-bordered"> <thead> <tr> <th>商品名称</th> <th>商品描述</th> <th>价格</th> <th>地址</th> </tr> </thead> <tbody> <#list page.content as p> <tr > <th><#if name??>${p.name?replace(name, '<span style="color: red">${name}</span>')}<#else>${p.name}</#if></th> <th>${p.desc}</th> <th>${p.price}</th> <th>${p.storeAddress} </th> </tr> </#list> </tbody> </table> <div> <#list 1..totalPage as i> <#if name??> <a href="/search?name=${name}&desc=${desc}&page=${i-1}" >${i}</a> <#else> <a href="/search?page=${i-1}" >${i}</a> </#if> </#list> 页 </div> </div> </body> </html>
- 启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; /** * Create by wangxb * 2019-09-11 7:29 */ @SpringBootApplication @EnableElasticsearchRepositories(basePackages = "com.basic.repository") public class AppTrade { public static void main(String[] args) { SpringApplication.run(AppTrade.class, args); } }
- 测试
http://127.0.0.1:8080/search?name=%E8%93%9D%E7%89%99&desc=&page=0