本篇所有操作都在 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之索引简单应用的更多相关文章

  1. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  2. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  3. ELK学习笔记之ElasticSearch的索引详解

    0x00 ElasticSearch的索引和MySQL的索引方式对比 Elasticsearch是通过Lucene的倒排索引技术实现比关系型数据库更快的过滤.特别是它对多条件的过滤支持非常好,比如年龄 ...

  4. Spring Boot + Elasticsearch 实现索引的日常维护

    全文检索的应用越来越广泛,几乎成了互联网应用的标配,商品搜索.日志分析.历史数据归档等等,各种场景都会涉及到大批量的数据,在全文检索方面,方案无外乎Lucene.Solr.Elasticsearch三 ...

  5. Spring Boot + Elasticsearch 实现索引批量写入

    在使用Eleasticsearch进行索引维护的过程中,如果你的应用场景需要频繁的大批量的索引写入,再使用上篇中提到的维护方法的话显然效率是低下的,此时推荐使用bulkIndex来提升效率.批写入数据 ...

  6. lucene创建索引简单示例

    利用空闲时间写了一个使用lucene创建索引简单示例, 1.使用maven创建的项目 2.需要用到的jar如下: 废话不多说,直接贴代码如下: 1.创建索引的类(HelloLucene): packa ...

  7. ES 10 - Elasticsearch的索引别名和索引模板

    目录 1 索引模板概述 1.1 什么是索引模板 1.2 索引模板中的内容 1.3 索引模板的用途 2 创建索引模板 3 查看索引模板 4 删除索引模板 5 模板的使用建议 5.1 一个index中不能 ...

  8. elasticsearch的索引操作和文档操作总结

    参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...

  9. elasticsearch的索引自动清理及自定义清理

    近发现elasticsearch近期索引文件大的吓人,清理了下之前的索引文件,发现服务器性能大大的减轻了一半,想一直保留近一个月的索引文件,但是又不想每个月手动清楚,在此写了一个小脚本 查询索引: c ...

  10. elasticsearch删除索引报错【原】

    如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...

随机推荐

  1. Bracket Sequence

    F. Bracket Sequence time limit per test 0.5 seconds memory limit per test 256 megabytes input standa ...

  2. adb server version (31) doesn't match this client (41); killing...

    使用用 adb devices 提示如下错误 C:\Users\Lenovo>adb devices adb server version (31) doesn't match this cli ...

  3. FreeSWITCH添加自定义endpoint

    操作系统 :CentOS 7.6_x64      FreeSWITCH版本 :1.10.9   日常开发过程中会遇到需要扩展FreeSWITCH对接其它系统的情况,这里记录下编写FreeSWITCH ...

  4. 之江实验室: 如何基于 JuiceFS 为超异构算力集群构建存储层 ?

    今天,高性能计算结合人工智能技术正在推动科研创新.例如通过破解水稻基因密码推动作物育种从"试验选优"向"计算选优"发展,在医药领域快速分析分子与蛋白之间的相互作 ...

  5. 页面status:500,报错 server encountered an internal error that prevented it from fulfilling this request.

    The server encountered an internal error that prevented it from fulfilling this request.服务器遇到了一个内部错误 ...

  6. SQL Server中的NULL值处理:判断与解决方案

    摘要: 在SQL Server数据库中,NULL是表示缺少数据或未知值的特殊标记.处理NULL值是SQL开发人员经常遇到的问题之一.本文将介绍SQL Server中判断和处理NULL值的不同方法,以及 ...

  7. 【IDEA】 远程调试

    远程调试 使用特定JVM参数运行服务端代码 要让远程服务器运行的代码支持远程调试,则启动的时候必须加上特定的JVM参数,这些参数是: -Xdebug -Xrunjdwp:transport=dt_so ...

  8. 【技术积累】Mysql中的SQL语言【技术篇】【三】

    聚合函数 SUM函数 在MySQL中,SUM函数是用于计算数值列的总和的聚合函数.它接受一个数值列作为参数,并返回该列中所有值的总和. 以下是一个使用SUM函数的示例: 假设我们有一个名为" ...

  9. 【WebGL系列-02】创建program上下文

    WebGL程序program对象的创建 program对象由顶点着色器对象和片元着色器对象构成,因此,创建program对象包含了两部分,一个是着色器对象的创建,一个是program对象的创建. 总体 ...

  10. TCP 链接关闭 -- 客户端为什么需要60秒的time_wait状态

    客户端主动关闭连接时( FIN-> ACK<- FIN<- ACK->),在发送最后一个ack后会进入TIME_WAIT状态,停留2个MSL时间,进入CLOSED状态 MSL就 ...