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" ...
随机推荐
- 多线程合集(三)---异步的那些事之自定义AsyncTaskMethodBuilder
引言 之前在上一篇文章中多线程合集(二)---异步的那些事,async和await原理抛析,我们从源码去分析了async和await如何运行,以及将编译后的IL代码写成了c#代码,以及实现自定义的Aw ...
- 4、数据库:MySQL部署 - 系统部署系列文章
MySQL数据库在其它博文中有介绍,包括学习规划系列.今天就讲讲MySQL的部署事情. 一.先下载MySQL数据库: 到下面这个网址去下载数据库,这里下载的社区版: https://dev.mysql ...
- 如何使用Stable Diffusion生成艺术二维码?
硬件准备 物理内存:至少16G(8G直接安装阶段就卡死) N卡:此处我使用GTX 1660 6G (2019年双12购买) 操作系统 windows 11 软件准备 网络要通畅 git: https: ...
- 消失的死锁:从 JSF 线程池满到 JVM 初始化原理剖析
一.问题描述 在一次上线时,按照正常流程上线后,观察了线上报文.接口可用率十分钟以上,未出现异常情况,结果在上线一小时后突然收到jsf线程池耗尽的报警,并且该应用一共有30台机器,只有一台机器出现该问 ...
- 前端分页组件简单好用列表分页page组件
快速实现 简单好用列表分页组件, 分页器组件,用于展示页码.请求数据等 ,包含翻页. 详情请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12 ...
- 信创优选,国产开源,Solon v2.3.6 发布
Solon 是什么开源项目? 一个,Java 新的生态型应用开发框架.它从零开始构建,有自己的标准规范与开放生态(历时五年,已有全球第二级别的生态).与其他框架相比,它解决了两个重要的痛点:启动慢,费 ...
- 未来的编程语言「GitHub 热点速览」
又一个编程语言火了,不算新,因为它已经开发了一段时间.不过在本周 Hacker News 上风头十足,DreamBerd 除了有点意思的改 ; 分隔符为 !,之外,它还能让你用问号来标注一段你也不确定 ...
- 基于DSP的设备振动信号的采集技术方案综述
前记 在能源领域,由于很多地方都是无人值守,设备故障检测是一个必须面对的问题.笔者通过几个行业案例了解到,由于很多设备发生故障时候会产生特定频谱的声音,所以该行业对振动监测的需求特别强烈,由于涉及到 ...
- SQL Sever 各版本的适用环境
很多用visual studio做开发的朋友经常会用到sqlserver数据库,但是往往在选择的时候就不知道该使用哪个版本了,今天就给大家分享一下sqlserver各个版本之间的区别,以及各个版本之间 ...
- selenium元素定位防踩坑---StaleElementReferenceException解决方法
1.异常原因 执行调试报错:selenium.common.exceptions.StaleElementReferenceException: Message: stale element refe ...