Basic operation of Elasticsearch

Basic operation

Some differences between ES and MySQL definitions

ESMySQL
IndexLibrary
Typestable
DocumentsRow
mappingTable Structure

In Elasticsearch 6.X, an index contains a Type. In
Elasticsearch 7.X, the concept of Type has been removed

index

Index, a database similar to a relational database

ScenesprotocolURL
Create indexPUThttp://172.176.21.220:9200/shopping
View all indexesGEThttp://172.176.21.220:9200/_cat/indices?v
View a single indexGEThttp://172.176.21.220:9200/shopping
Delete indexDELETEhttp://172.176.21.220:9200/shopping
  • shopping is the index name
  • The POST protocol is selected because the PUT protocol has idempotent check, and POST does not have idempotent check
Create index
  • Create index database
创建索引~~Response
{
 "acknowledged"【响应结果】: true, # true 操作成功
 "shards_acknowledged"【分片结果】: true, # 分片操作成功
 "index"【索引名称】: "shopping"
}
View all indexes

_cat means view; indexes means index. v means detailed display

The three means to view all the index details in the current ES server

Response

Insert picture description here
View a single index

Response

Insert picture description here
Delete index

Response
{ "acknowledged" [response result]: true, # true operation succeeded }

Documentation

Documents can be compared to table data in a relational database, and the added data format is JSON format.

ScenesprotocolURL
Add documentPOSThttp://172.176.21.220:9200/shopping/_doc
Add a document with the specified IdPOSThttp://172.176.21.220:9200/shopping/_doc/20210527
View a documentGEThttp://172.176.21.220:9200/shopping/_doc/20210527
View all documentsGEThttp://172.176.21.220:9200/shopping/_search
Modify a document globallyPUThttp://172.176.21.220:9200/shopping/_doc/20210527
Partially modify a documentPOSThttp://172.176.21.220:9200/shopping/_update/20210527
Delete a documentDELETEhttp://172.176.21.220:9200/shopping/_doc/20210527
Conditionally delete documentsPOSThttp://172.176.21.223:9200/shopping/_delete_by_query
Add documents to the index
  • _doc means adding documents to the index, you can use _create instead
  • The ES server will randomly generate a unique Id
  • _doc/xxxxx add a document with the specified Id as XXXXX

Response

Insert picture description here
View document
  • _search View all documents in the index library
  • _doc/xxxx View a single document in the index library

Response

Insert picture description here
Update documentation
  • Cover a document in the index library, using the PUT protocol;
  • _doc
全局更新文档,同新增文档请求体
{
 "title":"小米手机",
 "category":"小米",
 "images":"http://www.gulixueyuan.com/xm.jpg",
 "price":7999.00
}
  • Partial modification of a document in the index library, using the POST protocol;
  • _update
局部修改文档
{ 
 "doc": {
 "title":"小米PLUS"
 } 
}
Delete document
  • _delete protocol deletes a document in the index library
Conditionally delete documents
  • POST protocol
  • _delete_by_query
{
 	"query":{
 		"match":{
 			"price":4000.00
 		}
	 }
 }

Response

Insert picture description here

Mapping

  • Index mapping, similar to the table structure of a relational database
  • To create a database table, you need to set the field name, type, length, constraints, etc.;
  • The same is true for the index library. You need to know which fields under this index library and what constraint information each field has. This is called mapping.
ScenesprotocolURL
Create mappingPUThttp://172.176.21.223:9200/shopping/_mapping
View mapGEThttp://172.176.21.223:9200/shopping/_mapping
Index mapping associationPUThttp://172.176.21.223:9200/student
Create mapping
  • _mapping
  • PUT
{
 "properties": {
	 "name":{
		"type": "text",
		"index": true
	 },
	 "sex":{
		"type": "text",
		"index": false
	 },
	 "age":{
		"type": "long",
		"index": false
	 }
 }
}
View map
  • GET
Index mapping association
  • PUT
  • That is, when creating an index, even the mapping is created together
{
 "settings": {},
 "mappings": {
	 "properties": {
		"name":{
		 "type": "text",
		 "index": true
		},
		"sex":{
		 "type": "text",
		 "index": false
		},
		"age":{
		 "type": "long",
		 "index": false
		}
	 }
 }
}