当前位置: 首页 > news >正文

尚品汇-创建ES索引库(二十七)

目录:

(1)商品检索功能介绍

(2)根据业务搭建数据结构

(3)nested 介绍

(4)搭建service-list服务

(5)构建实体与es mapping建立映射关系

(6)初始化mapping结构到es中

(1)商品检索功能介绍

根据用户输入的检索条件,查询出对应的商品

检索两个入口

首页的分类:

搜索栏:

检索列表展示页面

(2)根据业务搭建数据结构

建立mapping!

这时我们要思考三个问题:

  1. 哪些字段需要分词
    1.  例如:商品名称
  2. 我们用哪些字段进行过滤
    1. 平台属性值
    2. 分类Id
  3. 哪些字段我们需要通过搜索查询出来。
    1. 商品名称,价格,图片等。

以上分析的所有显示,以及分词,过滤的字段都应该在es中出现。Es中如何保存这些数据呢?

“根据上述的字段描述,应该建立一个mappings对应的存上上述字段描述的信息!”

根据以上制定出如下结构:mappings

Index:goods

type:_doc

document: properties - rows

field: id,price,title…

Es中index默认是true。

注意:ik_max_word 中文词库必须有!

attrs:平台属性值的集合,主要用于平台属性值过滤。

(3)nested 介绍

nested:类型是一种特殊的对象object数据类型(specialised version of the object datatype ),允许对象数组彼此独立地进行索引和查询。

demo: 建立一个普通的index

如果linux 中有这个my_index 先删除!DELETE /my_index

步骤1:建立一个index

PUT my_index/_doc/1

{

  "group" : "fans",

  "user" : [

    {

      "first" : "John",

      "last" :  "Smith"

    },

    {

      "first" : "Alice",

      "last" :  "White"

    }

  ]

}

步骤2 : 执行查询

GET my_index/_search

{

  "query": {

    "bool": {

      "must": [

        { "match": { "user.first": "Alice" }},

        { "match": { "user.last":  "Smith" }}

      ]

    }

  }

}

查询结果:

能够查询出数据的原因是因为:建立my_index 的时候,它默认的数据类型是Object

 

 

{user.first:”John ,Alice”}

{user.last:”Smith,White”}

 

实际上:从业务的角度出发应该没有数据: 因为

User1 {John,Smith}

User2 {Alice,White}

是两个对象 而 {Alice,Smith} 在当前的user 中不存在!

步骤3:删除当前索引

DELETE /my_index

步骤4:建立一个nested 类型的

PUT my_index

{

  "mappings": {

      "properties": {

        "user": {

          "type": "nested"

        }

    }

  }

}

user字段映射为nested类型,而不是默认的object类型

重新执行步骤1,使用nested 查询

GET /my_index/_search

{

  "query": {

    "nested": {

      "path": "user",

      "query": {

        "bool": {

          "must": [

            {"match": {"user.first": "Alice"}},

            {"match": {"user.last": "Smith"}}

          ]

        }

      }

    }

  }

}

此查询得不到匹配,是因为AliceSmith不在同一个嵌套对象中。

尽管查询的对象是一个,但是是nested存储,普通的查询不能查出来 

 

{"match": {"user.last": "White"}} 此时就会有数据:

(4)搭建service-list服务

在service模块下搭建,搭建方式如service-item

修改配置pom.xml

添加依赖:

 <dependencies><dependency><groupId>com.atguigu.gmall</groupId><artifactId>service-product-client</artifactId><version>1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency></dependencies>

说明:

  1. 引入service-product-client模块
  2. 引入spring-boot-starter-data-elasticsearch依赖

添加配置文件

bootstrap.properties:

说明:添加es配置

spring.application.name=service-listspring.profiles.active=devspring.cloud.nacos.discovery.server-addr=192.168.200.129:8848spring.cloud.nacos.config.server-addr=192.168.200.129:8848spring.cloud.nacos.config.prefix=${spring.application.name}spring.cloud.nacos.config.file-extension=yamlspring.cloud.nacos.config.shared-configs[0].data-id=common.yaml

 

添加主启动类

没有用到mysql,这里需要用注解排除数据源

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan({"com.atguigu.gmall"})
@EnableDiscoveryClient
@EnableFeignClients(basePackages= {"com.atguigu.gmall"})
public class ServiceListApplication {public static void main(String[] args) {SpringApplication.run(ServiceListApplication.class,args);}
}

(5)构建实体与es mapping建立映射关系

 

 

package com.atguigu.gmall.model.list;@Document(indexName = "goods", shards = 3, replicas = 1)
@Data
public class Goods {@Idprivate Long id;@Field(type = FieldType.Keyword, index = false)private String defaultImg;@Field(type = FieldType.Text, analyzer = "ik_max_word")private String title;@Field(type = FieldType.Double)private Double price;@Field(type = FieldType.Date,format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")private Date createTime; // 新品@Field(type = FieldType.Long)private Long tmId;@Field(type = FieldType.Keyword)private String tmName;@Field(type = FieldType.Keyword)private String tmLogoUrl;@Field(type = FieldType.Long)private Long category1Id;@Field(type = FieldType.Keyword)private String category1Name;@Field(type = FieldType.Long)private Long category2Id;@Field(type = FieldType.Keyword)private String category2Name;@Field(type = FieldType.Long)private Long category3Id;@Field(type = FieldType.Keyword)private String category3Name;@Field(type = FieldType.Long)private Long hotScore = 0L;@Field(type = FieldType.Nested)private List<SearchAttr> attrs;}

平台属性-平台属性值 

@Data
public class SearchAttr {@Field(type = FieldType.Long)private Long attrId;@Field(type = FieldType.Keyword)private String attrName;@Field(type = FieldType.Keyword)private String attrValue;
}

(6)初始化mapping结构到es中

package com.atguigu.gmall.list.controller;
@RestController
@RequestMapping("api/list")
public class ListApiController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;/*** @return*/@GetMapping("inner/createIndex")public Result createIndex() {//创建索引库restTemplate.createIndex(Goods.class);//建立mappingrestTemplate.putMapping(Goods.class);return Result.ok();}
}

 

在浏览器运行:

http://localhost:8203/api/list/inner/createIndex

通过kibana查看mapping

http://www.lryc.cn/news/416699.html

相关文章:

  • 设计模式-六大原则
  • MyBatis搭建和增删改查
  • 【一图学技术】6.反向代理 vs API网关 vs 负载均衡的原理和使用场景
  • python爬虫番外篇 | Reuqests库高级用法(1)
  • 【链表OJ】常见面试题 3
  • Linux学习笔记9(Linux包管理)
  • 论文阅读《Geometric deep learning of RNA structure》
  • 宏集方案 | 传统建筑智能化改造,迈向物联新时代
  • 如果服务器更改Web端口会减少被攻击的风险吗?
  • vim列编辑模式
  • 如何实现pxe安装部署
  • 机器学习常见模型
  • 【python案例】基于Python 爬虫的房地产数据可视化分析设计与实现
  • 如何在Python中诊断和解决内存溢出问题
  • 什么是爬虫软件?这两个爬虫神器你必须要试试
  • 记录|MVS和VM软件使用记录
  • 算法通关:014_1:用栈实现队列
  • 【C#】Random
  • MongoDB简介及其在Java中的应用
  • JSON-LD上下文将属性映射到RDF IRIs示例
  • Spring的监听机制详解
  • Cache结构
  • 国产版Sora复现——智谱AI开源CogVideoX-2b 本地部署复现实践教程
  • 怎么读取FRM、MYD、MYI数据文件
  • Leetcode3226. 使两个整数相等的位更改次数
  • Linux笔记-3()
  • Apache漏洞复现CVE-2021-41773
  • GIT如何将远程指定分支的指定提交拉回到本地分支
  • 鸿蒙图形开发【3D引擎接口示例】
  • C#实现数据采集系统-系统优化服务封装