美文网首页开发设计
实现商品的搜索功能

实现商品的搜索功能

作者: 岳峙 | 来源:发表于2019-03-29 22:40 被阅读270次

新建ly-search
pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>leyou</artifactId>
        <groupId>com.leyou.parent</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ly-search</artifactId>

    <dependencies>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>com.leyou.service</groupId>
            <artifactId>ly-item-interface</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

配置文件
application.yml

server:
  port: 8083
spring:
  application:
    name: search-service
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 192.168.160.128:9300
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
  instance:
    lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
    lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
    prefer-ip-address: true
    ip-address: 127.0.0.1
    instance-id: ${spring.application.name}:${server.port}

然后是启动类

package com.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Author: Pandy
 * @Date: 2019/3/29 21:10
 * @Version 1.0
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LySearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(LySearchApplication.class);
    }
}

目前我们具有商品表的spu 和 sku 究竟为哪个建立索引?

新建pojo类

package com.leyou.search.pojo;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.Id;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Document(indexName = "goods", type = "docs", shards = 1, replicas = 0)
public class Goods {
    @Id
    private Long id; // spuId

    @Field(type = FieldType.text, analyzer = "ik_max_word")
    private String all; // 所有需要被搜索的信息,包含标题,分类,甚至品牌

    @Field(type = FieldType.keyword, index = false)
    private String subTitle;// 卖点

    private Long brandId;// 品牌id
    private Long cid1;// 1级分类id
    private Long cid2;// 2级分类id
    private Long cid3;// 3级分类id
    private Date createTime;// 创建时间
    private List<Long> price;// 价格

    @Field(type = FieldType.keyword, index = false)
    private String skus;// sku信息的json结构

    private Map<String, Object> specs;// 可搜索的规格参数,key是参数名,值是参数值
}

上面的fieldTypej后面的属性竟然是小写..

项目中的功能实现
根据多个商品的id 查询商品的集合

 /**
     * 根据多个商品分类的id  查询商品的集合
     * @return
     */
    @GetMapping("list/ids")
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(categoryService.queryByIds(ids));
    }

service层

 /**
     * 根据多个商品分类的id  查询商品分类的集合
     * @param ids
     * @return
     */
    public List<Category> queryByIds(List<Long> ids){
        List<Category> categories = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnums.CATEGORY_NOT_FOUND);
        }
        return categories;
    }

其中根据ids查询 底层还是不可见 因为是框架自身提供的方法
但是在控制台中可以看见相应的sql查询语句


image.png

测试发现


image.png

根据商品的id查询商品的集合成功

根据id查询对应的品牌

  /**
     * 根据id查询品牌
     * @param id
     * @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

根据商品id查询

 /**
     * 根据商品id查询
     * @param id
     * @return
     */
    public Brand queryById(Long id){
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null){
            throw new LyException(ExceptionEnums.BRAND_NOT_FOUND);
        }
        return brand;
    }

测试发现


image.png

测试是成功的只不过没有查到数据而已

控制台的语句


image.png

相关文章

  • Django开发教程(五)

    十六、实现商品搜索功能 16.1商品搜索功能的分析: 16.2全文搜索框架haystack和搜索引擎whoosh的...

  • 实现商品的搜索功能

    新建ly-searchpom.xml文件 配置文件application.yml 然后是启动类 目前我们具有商品表...

  • 商品搜索没有人干成功过,这次微信、今日头条想干?

    今日头条(现字节跳动)近日悄悄上线了商品搜索功能。 商品搜索功能,即可以从APP的搜索端入口直达电商端,从而实现内...

  • 通过Es实现商品搜索功能

    涉及微服务:changgou_web_search:存放静态资源,实现页面跳转。changgou_service_...

  • 利用Python爬取淘宝商品信息

    本文所实现的爬取淘宝商品信息将实现以下功能:对于某个类别的淘宝商品的页面 爬取这个商品名称,比如“手机”搜索结果下...

  • vue vue-router vuex element-ui a

    一、搜索功能的思路 在搜索框中输入商品名或商品名所包含的字符,就可以搜索出相关的商品列表 二、完成搜索功能的步骤 ...

  • 入门级爬虫-爬取京东商品评价

    实现功能 输入商品名称,并爬取第一页的商品的前100条评论 分析数据来源 爬取搜索页面 随便搜索一个商品,打开ne...

  • 基于es的商品搜索功能实现(上)

    公司的电商app需要做搜索功能,分析整理后列出如下需求点: 商品搜索(标题、描述全文检索) 搜索词汉字拼音自适应 ...

  • 商城功能

    前端功能 首页banner广告-最热商品-限时抢购-推荐商品-精品商品-分类-二级商品分类-搜索-宝贝搜索-店铺列...

  • web整合Solr

    1..Solr案例实战 1.1.需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字、分类、价格搜索商...

网友评论

    本文标题:实现商品的搜索功能

    本文链接:https://www.haomeiwen.com/subject/bdorbqtx.html