elasticsearch从入门到入门系列(二)---快速入门A

本文详细介绍了Elasticsearch的核心概念,如索引、类型、映射、文档和字段,并讲解了如何使用RESTful风格API进行操作,包括创建、读取、更新和删除文档,以及索引的管理和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.核心概念介绍

  • 索引(index):一个索引可以理解成一个关系型数据库
  • 类型(type):
    • 一种type就像一个类表,比如user表,order表
    • 注意
      • ES 5.X中一个index可以有多种type
      • ES 6.X中一个index只有一种type
      • ES 7.X中已经移除了type的概念
  • 映射(mapping):mapping定义了每个字段的类型等信息,相当于关系型数据库中的表结构
  • 文档(document):一个document相当于关系型数据库中的一行记录
  • 字段(field):相当于关系型数据库表的字段
  • 集群(cluster):集群由一个或多个节点组成,一个集群由一个默认的名称"elasticsearch"
  • 节点(node):集群的节点,一台机器或者一个进程
  • 分片和副本(chard)
    • 副本是分片的副本,分片有主分片(primary Shard)和副分片(replica Shard)之分
    • 一个index数据在物理上被分布在多个主分片中,每个主分片只存放部分数据
    • 每个主分片可以有多个副本,也叫副本分片,是主分片的复制

2.RESTful风格介绍

  • 介绍
    • RESTful是⼀一种架构的规范与约束、原则,符合这种规范的架构就是RESTful架构。

    • 先看REST是什什么意思,英⽂文Representational state transfer 表述性状态转移,其实就是对 资源的表述性状态转移,即通过HTTP动词来实现资源的状态扭转

    • 资源是REST系统的核⼼心概念。 所有的设计都是以资源为中⼼心

    • elasticsearch使⽤用RESTful⻛风格api来设计的

  • 方法

action

描述

HEAD

只获取某个资源的头部信息

GET

获取资源

POST

创建或更更新资源

PUT

创建或更更新资源

DELETE

删除资源

 
GET /user:列列出所有的⽤用户
POST /user:新建⼀一个⽤用户
PUT /user:更更新某个指定⽤用户的信息 DELETE /user/ID:删除指定⽤用户

3.简单操作,使用curl工具

  • 获取elasticsearch状态:
curl -X GET "http://localhost:9200"
  • 新增一个文档
curl -X PUT "localhost:9200/doudou/_doc/1" -H 'Content-Type:
application/json' -d'
{
    "user" : "louis",
    "message" : "louis is good"
}
  • 删除一个文档
curl -X DELETE "localhost:9200/doudou/_doc/1"

4.索引的使用

  • 新增一个索引
请求:
    curl -X PUT "localhost:9200/nba"
响应:
         
    {
      "acknowledged": true,
      "shards_acknowledged": true,
      "index": "nba"
    }
  • 获取索引:
请求:
    curl -X GET "localhost:9200/nba" 
响应:
     
{
    "nba": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1563078001824",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "P-kmcRGlRECcBxAI_8mgaw",
                "version": {
                    "created": "7020099"
                },
                "provided_name": "nba"
            }
         }
   }
}
  • 删除索引:
请求:
    curl -X DELETE "localhost:9200/nba"
响应:
   {
    "acknowledged": true
   }
  • 批量获取
请求:
    curl -x GET "localhost:9200/nba,cba"
响应:
    {
    "cba": {
        "aliases": { }, 
        "mappings": { }, 
        "settings": { }
    }, 
    "index": {
        "creation_date": "1563078437245", 
        "number_of_shards": "1", 
        "number_of_replicas": "1", 
        "uuid": "3rGtXSv_QTK-GU_xHKRvmw", 
        "version": {
            "created": "7020099"
        }, 
        "provided_name": "cba"
    }, 
    "nba": {
        "aliases": { }, 
        "mappings": { }, 
        "settings": {
            "index": {
                "creation_date": "1563078431931", 
                "number_of_shards": "1", 
                "number_of_replicas": "1", 
                "uuid": "bP6CZH5jSTGlhrTDzlq0bw", 
                "version": {
                    "created": "7020099"
                }, 
                "provided_name": "nba"
            }
        }
    }
}
  • 获取所有索引
请求(一):
    curl -X GET "localhost:9200/_all"
响应:
{
    "cba": {
        "aliases": { }, 
        "mappings": { }, 
        "settings": {
            "index": {
                "creation_date": "1563078437245", 
                "number_of_shards": "1", 
                "number_of_replicas": "1", 
                "uuid": "3rGtXSv_QTK-GU_xHKRvmw", 
                "version": {
                    "created": "7020099"
                }, 
                "provided_name": "cba"
            }
        }
    }, 
    "nba": {
        "aliases": { }, 
        "mappings": { }, 
        "settings": {
            "index": {
                "creation_date": "1563078431931", 
                "number_of_shards": "1", 
                "number_of_replicas": "1", 
                "uuid": "bP6CZH5jSTGlhrTDzlq0bw", 
                "version": {
                    "created": "7020099"
                }, 
                "provided_name": "nba"
            }
        }
    }
}

请求(二):
    curl -X GET "http://localhost:9200/_cat/indices?v"
响应:
???
  • 索引是否存在
请求:
    
    curl -I "localhost:9200/nba"

响应:
    200 ok
  • 索引关闭
请求:
    curl -X POST "localhost:9200/nba/_close"
响应:
     
    {
        "acknowledged": true,
        "shards_acknowledged": true
    }
  • 打开索引:
请求:
    curl -X POST "localhost:9200/nba/_open" 
响应:
     
    {
        "acknowledged": true,
        "shards_acknowledged": true
    }
   

5.映射的使用

  • 新增映射
 请求:
 	curl -X PUT "localhost:9200/nba/_mapping" -H 'Content-Type:
	application/json' -d'
		{
			"properties": {
			    "name": {
			      "type": "text"
			    },
			     "team_name": {
			      "type": "text"
			    },
			    "position": {
			      "type": "keyword"
			    },
			    "play_year": {
			      "type": "keyword"
			    },
			     "jerse_no": {
			      "type": "keyword"
			    }
			} 
		}
	'
响应:
    {
        "acknowledged":true
    }
  • 获取映射
请求:
    curl -X GET "localhost:9200/xdclass/_mapping"
响应:
	{
	    "nba": {
	        "mappings": {
	            "properties": {
	                "jerse_no": {
	                    "type": "keyword"
	                }, 
	                "name": {
	                    "type": "text"
	                }, 
	                "play_year": {
	                    "type": "keyword"
	                }, 
	                "position": {
	                    "type": "keyword"
	                }, 
	                "team_name": {
	                    "type": "text"
	                }
	            }
	        }
	    }
	}
  • 批量获取映射
请求:
    curl -X GET "localhost:9200/nba,cba/mapping"
响应:
	{
	    "nba": {
	        "mappings": {
	            "properties": {
	                "jerse_no": {
	                    "type": "keyword"
	                }, 
	                "name": {
	                    "type": "text"
	                }, 
	                "play_year": {
	                    "type": "keyword"
	                }, 
	                "position": {
	                    "type": "keyword"
	                }, 
	                "team_name": {
	                    "type": "text"
	                }
	            }
	        }
	    }, 
	    "cba": {
	        "mappings": { }
	    }
	}
  • 修改映射
请求:
	curl -X PUT "localhost:9200/nba/_mapping" -H 'Content-Type:
		application/json' -d'
			{
			  "properties": {
			    "name": {
			      "type": "text"
			    },
			     "team_name": {
			      "type": "text"
			    },
			    "position": {
			      "type": "keyword"
			    },
			    "play_year": {
			      "type": "keyword"
			    },
			     "jerse_no": {
			      "type": "keyword"
			    },
			     "country": {
			      "type": "keyword"
				} 
			}
		}
	'
响应:
    {
        "acknowledge":true
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值