ElasticSearch基础4之springboot整合实现商品简单搜索

  • Springboot整合ElasticSearch
     
     a、提前使用kibana创建索引和字段类型
    // 创建索引
    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"
              }
            }
          }
    }
    ElasticSearch基础4之springboot整合实现商品简单搜索_第1张图片 // 记住类型要设置'text',且搜索引擎设置ik_smart,否则不会进行分词
     
      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
  1. 创建springboot项目,在pom.xml中引入依赖
     
        
            org.springframework.boot
            spring-boot-starter-parent
            2.1.5.RELEASE
             
        
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.boot
                spring-boot-starter-data-elasticsearch
            
            
                org.projectlombok
                lombok
            
    
            
                com.google.collections
                google-collections
                1.0-rc2
            
            
            
                org.springframework.boot
                spring-boot-starter-freemarker
            
            
                javax.servlet
                javax.servlet-api
            
    
        
  2. 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/**
    
  3. 定义三层架构--实体类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;
    
    }
  4. 定义三层架构-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 {
    }
  5. 定义三层架构-控制层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 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 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";
        }
    
    }
  6. 在resources/templates创建页面search.ftl 
     
    
    
    
        
        商品搜索
        
        
    
    
          
    <#list page.content as p>
    商品名称 商品描述 价格 地址
    <#if name??>${p.name?replace(name, '${name}')}<#else>${p.name} ${p.desc} ${p.price} ${p.storeAddress}
    <#list 1..totalPage as i> <#if name??> ${i} <#else> ${i}
  7. 启动类
     
    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);
        }
    }
  8. 测试
     http://127.0.0.1:8080/search?name=%E8%93%9D%E7%89%99&desc=&page=0
    ElasticSearch基础4之springboot整合实现商品简单搜索_第2张图片 
     
     

你可能感兴趣的:(ElasticSearch基础4之springboot整合实现商品简单搜索)