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" ...
随机推荐
- < Python全景系列-7 > 提升Python编程效率:模块与包全面解读
欢迎来到我们的系列博客<Python全景系列>!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题,帮助你掌握这门强大而灵活的编程语法.无论你是编程新手,还是有一 ...
- 【模型部署 01】C++实现分类模型(以GoogLeNet为例)在OpenCV DNN、ONNXRuntime、TensorRT、OpenVINO上的推理部署
深度学习领域常用的基于CPU/GPU的推理方式有OpenCV DNN.ONNXRuntime.TensorRT以及OpenVINO.这几种方式的推理过程可以统一用下图来概述.整体可分为模型初始化部分和 ...
- 逍遥自在学C语言 | 揭开while循环的神秘面纱
前言 循环是一种重要的控制结构,可以使程序重复执行一段代码,直到满足特定条件为止. 在C语言中,while和do-while是两种常用的循环结构,本文将详细介绍这两种循环的用法. 一.人物简介 第一位 ...
- Spring Boot 通用对象列表比较和去重
1.前言 在Excel批量导入数据时,常常需要与数据库中已存在数据的比较,并且需要考虑导入数据重复的可能性. 导入的行数据,一般有一个实体类(对象)与之对应,往往这个实体类在数据库中的字段要比 ...
- 通过redis学网络(2)-redis网络模型
本系列主要是为了对redis的网络模型和集群原理进行学习,我会用golang实现一个reactor网络模型,并实现对redis协议的解析. 系列源码已经上传github https://github. ...
- Gazebo、Rviz 结合 ORB-SLAM3 实现运动规划及定位
博客地址:https://www.cnblogs.com/zylyehuo/ 基于以下内容,详见博客链接 基于机器人自主移动实现SLAM建图 - zylyehuo - 博客园 (cnblogs.com ...
- 记一次DNS问题排查
一.问题:域名flow.nzkong.com解析很慢: 排查过程 抓包分析:tcpdump -i eth0 -n -s 500 port domain 1 14:40:44.548553 IP 10. ...
- Lamada List 去重及其它操作示例
import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util. ...
- 推荐一款.NET开源跨平台的开箱即用的DNS服务器软件
前言 今天要给大家推荐一款.NET开源跨平台的开箱即用的DNS服务器软件(用于提供 DNS 解析服务):Technitium DNS Server. 项目介绍 Technitium DNS Serve ...
- JVM中的编译器
JVM中集成了两种编译器,Client Compiler和Server Compiler,它们的作用也不同.Client Compiler注重启动速度和局部的优化,Server Compiler则更加 ...