Elasticsearch之索引简单应用
本篇所有操作都在 Kibana 上执行
创建第一个索引
PUT product
{
// 索引设置
"settings": {
// 分片数量
"number_of_shards": 3,
// 副本数量
"number_of_replicas": 1
},
// 索引字段映射
"mappings": {
// 字段属性
"properties": {
// 商品名称
"name":{
// 字段类型为文本
"type":"text"
},
// 商品标签
"label":{
"type":"keyword"
},
"price":{
"type": "scaled_float",
// 比例因子设置为100 在ES中会按分存储
// 注意:scaling_factor属性是只针对scaled_float这个数据类型才有
"scaling_factor": 100
},
// 商品状态
"status":{
"type":"integer"
},
// 创建日期
"create_date":{
"type":"date"
}
}
}
}
执行命令,我们会得到如下返回信息,表示创建成功
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "product"
}
查看指定索引结构
获取索引全部信息:
## 命令格式 GET {索引名称}
GET product
返回结果:
{
"product": {
"aliases": {},
"mappings": {
"properties": {
"create_date": {
"type": "date"
},
"label": {
"type": "keyword"
},
"name": {
"type": "text"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
},
"status": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "3",
"provided_name": "product",
"creation_date": "1693981437123",
"number_of_replicas": "1",
"uuid": "awfrhothQaeoL2bHvufN5w",
"version": {
"created": "8090199"
}
}
}
}
}
查看索引 Mapping 信息
GET product/_mapping
返回结果:
{
"product": {
"mappings": {
"properties": {
"create_date": {
"type": "date"
},
"label": {
"type": "keyword"
},
"name": {
"type": "text"
},
"price": {
"type": "scaled_float",
"scaling_factor": 100
},
"status": {
"type": "integer"
}
}
}
}
}
查看索引 settings 信息
GET product/_settings
返回结果:
{
"product": {
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "3",
"provided_name": "product",
"creation_date": "1693981437123",
"number_of_replicas": "1",
"uuid": "awfrhothQaeoL2bHvufN5w",
"version": {
"created": "8090199"
}
}
}
}
查看索引 aliases 信息
GET product/_alias
返回结果:
{
"product": {
"aliases": {}
}
}
目前我们没设置别名,所以返回为空
新增索引数据--文档
单个新增
语法:
PUT {索引名}/_doc/{文档id}
{
{字段名}:{字段值/]
}
实例:
PUT product/_doc/1
{
"name":"篮球",
"label":["运动","球类"],
"price":33.5,
"status":0,
"create_date":"2023-08-08T13:00:00"
}
执行成功将返回:
{
"_index": "product",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
批量新增
语法:
POST _bulk
{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body }
注:bulk对 JSON 串的有着严格的要求。每个 JSON 串不能换行,只能放在同一行,同时,相邻的 JSON 串之间必须要有换行(delete语法除外).
实例:
POST _bulk
{"create":{"_index":"product","_id":2}}
{"name":"足球","label":["运动","球类"],"price":60.3,"status":0,"create_date":"2023-08-09T13:00:00"}
{"create":{"_index":"product","_id":3}}
{"name":"华为手机","label":["数码","手机"],"price":6999,"status":0,"create_date":"2023-08-31T13:00:00"}
{"create":{"_index":"product","_id":4}}
{"name":"苹果","label":["数码","手机"],"price":9999,"status":1,"create_date":"2023-08-31T13:00:00"}
返回结果:
{
"took": 6,
"errors": false,
"items": [
{
"create": {
"_index": "product",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"create": {
"_index": "product",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 201
}
},
{
"create": {
"_index": "product",
"_id": "4",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1,
"status": 201
}
}
]
}
查询索引数据--文档
查询所有
GET product/_search
{
"query": {
"match_all": {}
}
}
返回
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "足球",
"label": [
"运动",
"球类"
],
"price": 60.3,
"status": 0,
"create_date": "2023-08-09T13:00:00"
}
},
{
"_index": "product",
"_id": "3",
"_score": 1,
"_source": {
"name": "华为手机",
"label": [
"数码",
"手机"
],
"price": 6999,
"status": 0,
"create_date": "2023-08-31T13:00:00"
}
},
{
"_index": "product",
"_id": "4",
"_score": 1,
"_source": {
"name": "苹果手机",
"label": [
"数码",
"手机"
],
"price": 9999,
"status": 1,
"create_date": "2023-09-01T13:00:00"
}
},
{
"_index": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "篮球",
"label": [
"运动",
"球类"
],
"price": 33.5,
"status": 0,
"create_date": "2023-08-08T13:00:00"
}
}
]
}
}
响应的数据结果分为两部分:
{
----------------first part:分片副本信息--------------------
"took": 9,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
---------------second part:查询的数据集---------------------
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [{}]
}
}
查询标签带手机的商品
GET product/_search
{
"query": {
"match": {
"label": "手机"
}
}
}
返回:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.5908618,
"hits": [
{
"_index": "product",
"_id": "3",
"_score": 0.5908618,
"_source": {
"name": "华为手机",
"label": [
"数码",
"手机"
],
"price": 6999,
"status": 0,
"create_date": "2023-08-31T13:00:00"
}
},
{
"_index": "product",
"_id": "4",
"_score": 0.5908618,
"_source": {
"name": "苹果手机",
"label": [
"数码",
"手机"
],
"price": 9999,
"status": 1,
"create_date": "2023-09-01T13:00:00"
}
}
]
}
}
过滤状态为0的
GET product/_search
{
"query": {
"bool": {
"must": {
"match": {
"label": "手机"
}
},
"filter": {
"term": {
"status": 0
}
}
}
}
}
返回结果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5908618,
"hits": [
{
"_index": "product",
"_id": "3",
"_score": 0.5908618,
"_source": {
"name": "华为手机",
"label": [
"数码",
"手机"
],
"price": 6999,
"status": 0,
"create_date": "2023-08-31T13:00:00"
}
}
]
}
}
查询结果里面都有一个 _score字段,一般 Elasticsearch 根据相关评分排序,相关评分是根据文档与语句的匹配度来得出, _score值越高说明匹配度越高。相关性(relevance)概念在Elasticsearch中非常重要,而这也是它与传统关系型数据库中记录只有匹配和不匹配概念最大的不同。
Elasticsearch之索引简单应用的更多相关文章
- ElasticSearch+Kibana 索引操作
ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...
- 《读书报告 -- Elasticsearch入门 》--简单使用(2)
<读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...
- ELK学习笔记之ElasticSearch的索引详解
0x00 ElasticSearch的索引和MySQL的索引方式对比 Elasticsearch是通过Lucene的倒排索引技术实现比关系型数据库更快的过滤.特别是它对多条件的过滤支持非常好,比如年龄 ...
- Spring Boot + Elasticsearch 实现索引的日常维护
全文检索的应用越来越广泛,几乎成了互联网应用的标配,商品搜索.日志分析.历史数据归档等等,各种场景都会涉及到大批量的数据,在全文检索方面,方案无外乎Lucene.Solr.Elasticsearch三 ...
- Spring Boot + Elasticsearch 实现索引批量写入
在使用Eleasticsearch进行索引维护的过程中,如果你的应用场景需要频繁的大批量的索引写入,再使用上篇中提到的维护方法的话显然效率是低下的,此时推荐使用bulkIndex来提升效率.批写入数据 ...
- lucene创建索引简单示例
利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...
- ES 10 - Elasticsearch的索引别名和索引模板
目录 1 索引模板概述 1.1 什么是索引模板 1.2 索引模板中的内容 1.3 索引模板的用途 2 创建索引模板 3 查看索引模板 4 删除索引模板 5 模板的使用建议 5.1 一个index中不能 ...
- elasticsearch的索引操作和文档操作总结
参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...
- elasticsearch的索引自动清理及自定义清理
近发现elasticsearch近期索引文件大的吓人,清理了下之前的索引文件,发现服务器性能大大的减轻了一半,想一直保留近一个月的索引文件,但是又不想每个月手动清楚,在此写了一个小脚本 查询索引: c ...
- elasticsearch删除索引报错【原】
如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...
随机推荐
- 对promise的简单理解
随着ES6的推行它的许多新特性受到了广大开发者的好评,比如promise,为什么使用这个promise呢,他具体能帮我们做些啥? 其实从字面意思上来理解promise就是承诺,比如:你命令你的手下本月 ...
- 基于Quartz的可视化UI操作组件GZY.Quartz.MUI更新说明(附:在ABP中集成GZY.Quartz.MUI可视化操作组件)
前言 时隔2年.(PS:其实陆陆续续在优化,不过没发博客).. .本组件又迎来了新的更新... 很久没更新博客了.生了娃,换了工作单位,太忙了..实在抱歉 NET Core 基于Quartz的UI可视 ...
- 源码解析:数据批量导入bukl_crete()原理
在Django中需要向数据库中插入多条数据(list).使用如下方法,每次save()的时候都会访问一次数据库.导致性能问题: for i in resultlist: p = Account(nam ...
- ChatGPT 问答
Win32GUI编程时,创建窗口触发消息的顺序 在Win32 GUI编程中,创建窗口并显示到屏幕上时,系统会触发一系列的消息,这些消息可以用来完成窗口的初始化和其他相关的工作.下面是创建窗口触发消息的 ...
- 野火指南者(STM32F103VET6)应用:实现USB虚拟串口(CDC_VPC)
MCU:STM32F103VET6 开发环境:STM32CubeMX+MDK5 实现USB的虚拟串口不需要去理解USB的底层驱动,只需要STM32CubeMX去配置生成工程即可.在野火的指南者中,是没 ...
- Python获取系统当前时间并进行类型转换
# 1.使用Python获取系统当前时间print('---------下面是方法1-----------')import timeprint(time.strftime('%Y-%m-%d %H:% ...
- 体验了一把 MiniGPT-4,一言难尽
最近看到一个好玩的开源项目:MiniGPT-4. 看名字像 GPT-4 的小老弟,其实没啥关系. 简单说,它可以识别图像,基于图像你可以和它对话,它能生成图片描述.网站.诗歌. 先看看官方给出的例子截 ...
- TVM Deploy Runtime[施工中]
本文地址:https://www.cnblogs.com/wanger-sjtu/p/17291070.html tvm 中在部署时有多个选择,最开始的graph exectuor runtime . ...
- 如何在docker中创建postgresql数据库并使用prisma进行连接
在docker中创建postgresql # docker 创建 postgres docker pull postgres # 创建容器 # ~/study/db/postgres 是本地的文件映射 ...
- ZEGO全新语音聊天室方案,2小时复刻 Clubhouse
真的火了! 新晋带货王马斯克在 Clubhouse"开房"之后,直接让 Clubhouse 爆火出圈,据说,Clubhouse 平台邀请码现在在ebay上已经卖到了快200刀一个. ...