ElasticSearch

1.ElasticSearch

1.1.业务分析

1.1.1.需求

假定有30万台设备在充电,每隔30秒上传充电进度

每秒上传 1万个充电进度数据

1天 24*60 * 60 * 1万 =86400w 大约8.6亿个数据

需要查询温度大于43度,告警,大于48度,必须停止充电

1.1.2.难点

数据量大

1.1.3.核心目标

高性能

1.2.技术解决方案

1.2.1.mysql模糊查询

正排索引:先找文档,再找词
从8亿行中查找温度大于48度的数据

1.2.2.Es 查询

分布式存储

在这里插入图片描述

分布式计算

案例:统计学生会主席票数
在这里插入图片描述

分词

字典:[华为,mate10,手机]
文本:华为mate10手机一台3300元
分词过程:华为,mate10

倒排索引

在这里插入图片描述
关键词列表
倒排索引是根据关键词找到内容,搜索速度快
正排索引是要的内容找到关键词

1.2.3.chatgpt小结

我是一名搜索开发工程师,用表格列出倒排索引和正排索引的区别,优点,缺点
告诉我elasticsearch的倒排索引包括包含那些内容

1.2.4.技术选型

充电进度数据特点是大量数据,必须用es存储

倒排索引,搜索速度快,。

1.3.实现

1.3.1.启动es

在这里插入图片描述

-1x删除虚拟机
在这里插入图片描述

检查es是否启动成功
http://192.168.64.140:9200
http://192.168.64.140:9200/_cat/nodes

1.3.2.ES-Head

\software\elasticSearch\es-head.crx.zip
解压缩
在 chrome 浏览器中选择“更多工具”–“扩展程序”
在“扩展程序”中确认开启了“开发者模式”
点击“加载已解压的扩展程序”
选择前面解压的插件目录
在浏览器中点击 elasticsearch-head 插件打开 head 界面,并连接 http://192.168.64.140:9200/
在这里插入图片描述

在这里插入图片描述

1.4.接口测试

1.4.1.查询分词

接口
_analyze

请求方法:POST

请求内容

{
"analyzer":"ik_smart",
"text":"华为mate10手机一台3300元"
}

在这里插入图片描述

1.4.2.创建索引

官方文档如下:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

请求方法:PUT

请求内容

{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
  }
}

在这里插入图片描述

1.4.3.查询索引

Es-header查看
刷新后可看到节点信息
在这里插入图片描述

1.4.4.删除索引

在这里插入图片描述

1.4.5.添加映射

一个映射mapping中包含多个字段field
相当于表中的列,对象的属性

和表的区别是有些字段需要加分词,有些字段如id,身份证号不用加分词

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html

接口
products/_mapping/

请求方法:PUT

请求内容

{
"properties": {
"id": {
"type": "long"
    },
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
    },
"category": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
    },
"price": {
"type": "float"
    },
"city": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
    },
"barcode": {
"type": "keyword"
    }
  }
}

在这里插入图片描述

1.4.6.查看映射

GET products/_mapping

在这里插入图片描述

1.4.7.添加文档(数据)
接口
/products/_doc/10035

请求方法:PUT

请求内容

{
"id": "10035",
"title": "BOSE SoundSport耳塞式运动耳机 重低音入耳式防脱降噪音乐耳机",
"category": "潮酷数码会场",
"price": "860.00",
"city": "浙江杭州",
"barcode": "526558749068"
}

在这里插入图片描述

提交一个新的数据

{
"id": "10036",
"title": "【送支架】Beats studio Wireless 2.0无线蓝牙录音师头戴式耳机",
"category": "潮酷数码会场",
"price": "2889.00",
"city": "上海",
"barcode": "37147009748"
}

1.4.8.查看文档

查看文档总数
在这里插入图片描述

查看所有文档内容
在这里插入图片描述

查看某个文档内容
GET /products/_doc/10036
在这里插入图片描述

1.4.9.查看分词结果

GET products/_doc/10035/_termvectors?fields=title
在这里插入图片描述

1.4.10.搜索

POST /products/_search

{
"query": {
"match": {
"title": "耳机"
    }
  }
}

修改title为无线蓝牙

1.4.11.chatgpt小结

列出elasticsearch每个数据类型的作用

1.5.搭建集群

1.5.1.快照流程图

在这里插入图片描述

在这里插入图片描述

1.5.2.用快照备份虚拟机

在这里插入图片描述

1.5.3.还原到logstash快照

在这里插入图片描述

1.5.4.启动三台es

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

1.5.5.查看资源占用

 docker stats

在这里插入图片描述

1.5.6.创建新闻索引

{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
  }
}

创建新闻索引如下图:

在这里插入图片描述

节点信息如下图:
在这里插入图片描述

1.5.7.还原快照

在这里插入图片描述

1.5.8.chatgpt小结

查看docker每个容器内存占用多少m的命令是什么
vmware中的快照有什么用

1.6.搜索

1.6.1.创建索引

PUT pditems

{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
  }
}

1.6.2.查看索引

在这里插入图片描述

1.6.3.创建映射

PUT pditems/_mapping

{
"properties": {
"id": {
"type": "long"
    },
"brand": {
"type": "text",
3"analyzer": "ik_smart"
    },
"title": {
"type": "text",
"analyzer": "ik_max_word"
    },
"sell_point": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
    },
"price": {
"type": "float"
    },
"image": {
"type": "keyword"
    },
"cid": {
"type": "long"
    },
"status": {
"type": "byte"
    },
"created": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
    },
"updated": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
    }
  }
}

1.6.4.导入数据

curl -XPOST 'localhost:9200/pditems/_bulk' \
    -H 'Content-Type:application/json' \
    --data-binary @pditems.json

1.6.5.查看数据

查看数据个数

在这里插入图片描述

查看详细数据

在这里插入图片描述

1.6.6.搜索所有

POST /pditems/_search

{
"query": {
"match_all": {}
  }
}

在这里插入图片描述

1.6.7.分页

POST /pditems/_search

{
"query": {
"match_all": {}
  },
"from": 0,
"size": 1
}

POST /pditems/_search

{
"query": {
"match_all": {}
  },
"from": 1,
"size": 1
}

1.6.8.关键字搜索

POST /pditems/_search

{
"query": {
"match": {
"title": "电脑"
    }
  }
}

在这里插入图片描述

Fiter
{
"query": {
"bool": {
"must": [
        {
"match": {
"title": "电脑"
          }
        }
      ],
"filter": [
        {
"range": {
"price": {
"gte": "6000"
            }
          }
        }
      ]
    }
  }
} 

在这里插入图片描述

1.6.9.高亮显示

POST pditems/_search

{
"query": {
"multi_match": {
"query": "电脑",
"fields": [
"title",
"sell_point"
      ]
    }
  },
"highlight": {
"pre_tags": "<span>",
"post_tags": "</span>",
"fields": {
"title": {},
"sell_point": {}
    }
  }
}

在这里插入图片描述

1.7.Java访问es

Spring-data官方文档
在这里插入图片描述
在这里插入图片描述

1.7.1.步骤分析

添加依赖
添加es配置
定义实体类
定义接口
单元测试

1.7.2.添加依赖

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

1.7.3.添加es配置

spring:
  # 连接ES搜索引擎地址
elasticsearch:
      uris: http://192.168.64.140:9200

1.7.4.定义实体类

@Document(indexName = "pditems")
public class PdItem {
    @Id
    private Long id;

    @Field(type = FieldType.Text)
    private String brand;

    @Field(type = FieldType.Text)
    private String title;

    @Field(type = FieldType.Text)
    private String sellPoint;

    @Field(type = FieldType.Float)
    private Float price;

    @Field(type = FieldType.Keyword)
    private String image;

    @Field(type = FieldType.Long)
    private Long cid;

    @Field(type = FieldType.Byte)
    private Byte status;

    @Field(type = FieldType.Text)
    private String created;

    @Field(type = FieldType.Text)
    private String updated;
}

1.7.5.定义接口

@Repository
public interface PdItemRepository extends ElasticsearchRepository<PdItem,String> {
  List<PdItem> findByTitle(String key);
}

1.7.6.单元测试

@SpringBootTest()
class EsqueryApplicationTests {
    @Autowired
    PdItemRepository pdItemRepository;

    @Test
    public void testFind() {
        List<PdItem> itemList = pdItemRepository.findByTitle("电脑");
        System.out.println(itemList);
    }

}

1.7.7.chatgpt小结

给出使用spring data查询elastic search的步骤
用一个表格告诉我elasticsearch数据类型和java类型的对应关系
根据上面elasticsearch的映射信息生成一个java实体类,给出java代码

1.8.温度查询

1.8.1.业务分析

1.8.2.步骤分析

添加依赖
添加es配置
创建索引,映射
定义实体类
定义接口
添加数据
查询数据
单元测试

1.8.3.创建索引charging

{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
  }
}

Es-header–>复合查询
在这里插入图片描述

1.8.4.查看索引

在这里插入图片描述

1.8.5.创建映射

PUT charging/_mapping

{
"properties": {
"id": {
"type": "long"
    },
"userId": {
"type": "long"
    },
"gunId": {
"type": "long"
    },
"temperature": {
"type": "long"
    }
  }
}

1.8.6.查看映射

GET charging/_mapping

1.8.7.添加依赖

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

1.8.8.添加es配置

不同springboot版本中uris写法不一样,不要拷贝。
在application.yml中输入 uris,由idea生成

spring:
  # 连接ES搜索引擎地址
  elasticsearch:
      uris: http://192.168.64.140:9200

1.8.9.添加实体类

@Document(indexName = "charging")
public class ChargingProgress {
    @Id
    private Long id;

    @Field(type =FieldType.Long)
    private Long userId;

    @Field(type=FieldType.Long)
    private Long gunId;

    @Field(type=FieldType.Long)
    private Long temperature;

Getter(),setter()
toString()
}

1.8.10.添加接口

创建es 包

@Repository
public interface ChargingProgressRepository  extends ElasticsearchRepository<ChargingProgress,Long> {

}

1.8.11.添加测试依赖,测试目录

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

在这里插入图片描述

在这里插入图片描述

1.8.12.添加数据

添加的数据的id要变。否则添加不进去。

package com.ruoyi;

import com.ruoyi.charge.domain.ChargingProgress;
import com.ruoyi.charge.es.ChargingProgressRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class ChargingProgressTests {
    @Autowired
    ChargingProgressRepository repository;

    @Test
    public void testSave(){
        ChargingProgress chargingProgress = new ChargingProgress();
        chargingProgress.setId(2L);
        chargingProgress.setUserId(2L);
        chargingProgress.setGunId(2L);
        chargingProgress.setTemperature(20L);

        repository.save(chargingProgress);
    }
}

在这里插入图片描述

1.8.13.查询数据

接口中添加方法

@Repository
public interface ChargingProgressRepository  extends ElasticsearchRepository<ChargingProgress,Long> {
    List<ChargingProgress> findByTemperatureGreaterThan(Long value);

}
@Test
public void testFind(){
    List<ChargingProgress> list = repository.findByTemperatureGreaterThan(48L);
    System.out.println(list);
}

1.8.14.Controller

1.8.15.chatgpt小结

{ “properties”: { “id”: { “type”: “long” }, “userId”: { “type”: “long” }, “gunId”: { “type”: “long” }, “temperature”: { “type”: “long” } } } 根据上面的elasticsearch映射配置,生成一个java实体类

在elasticsearch接口中,要查询值大于30的数据,方法名如何写

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值