SpringCloudAlibaba云商场-海量数据搜索实现(二)

86 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第26天,点击查看活动详情

每日英语:

Beware the barrenness of a busy life.

翻译:要当心忙碌的生活带来内心的荒芜。

1. 商品搜索java环境搭建

1.1 api工程搭建

1)工程搭建

创建搜索工程的api工程mall-search-api,创建在mall-api下,坐标如下:

<groupId>com.xz</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>mall-search-api</artifactId>

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>mall-api</artifactId>
        <groupId>com.xz</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>
        全文搜索引擎工程
    </description>
    <artifactId>mall-search-api</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--ElasticSearch-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    </dependencies>

</project>

2)映射设置

为了与数据库的实体类解耦,我们需要单独创建为Es服务的实体类。

es映射配置.jpg 创建一个新的JavaBean对象com.xz.mall.search.model.SkuEs,代码如下:

@Data
@Document(indexName = "shopsearch", type = "skues")
public class SkuEs {

    @Id
    private String id;
    //分词
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String name;
    private Integer price;
    private Integer num;
    private String image;
    private String images;
    private Date createTime;
    private Date updateTime;
    private String spuId;
    private Integer categoryId;
    //Keyword:不分词
    @Field(type= FieldType.Keyword)
    private String categoryName;
    private Integer brandId;
    @Field(type=FieldType.Keyword)
    private String brandName;
    private String skuAttribute;
    private Integer status;

    //属性映射
    private Map<String,String> attrMap;

}

1.2 搜索工程搭建

搭建搜索工程,坐标如下:

<groupId>com.xz</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>mall-search-service</artifactId>

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>mall-service</artifactId>
        <groupId>com.xz</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mall-search-service</artifactId>

    <description>
        全文搜索服务
    </description>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xz</groupId>
            <artifactId>mall-search-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

bootstrap.yml 如下:

server:
  port: 8084
spring:
  application:
    name: mall-search
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: xx.xx.xx.xx:8848
      discovery:
        #Nacos的注册地址
        server-addr: xx.xx.xx.xx:8848
  #Elasticsearch服务配置 6.8.12
  elasticsearch:
    rest:
      uris: http://xx.xx.xx.xx:9200
#日志配置
logging:
  pattern:
    console: "%msg%n"

创建启动类com.xz.mall.MallSearchApplication,代码如下:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MallSearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallSearchApplication.class, args);
    }

}

总结:本篇主要介绍了一下商品搜索java搭建的api工程和service工程的基础搭建。下一篇会介绍一下代码的具体操作。