1. Add documentation

Instructions for

  • The document can be likened to table data in a relational database, and the added data format is JSON
  • Note that you need to add _doc after the index to indicate the action document
  • When no ID generation is specified, a new document is generated each time a POST is performed
  • If the index does not exist, it will be created by default

Order sample

POST es/_doc
{
	"title": "Mi Phone"."category": "Millet"."images": "http://www.gulixueyuan.com/xm.jpg"."price": 3999
}
Copy the code

Return the sample

{
    "_index"[Index] :"es"."_type"[Type - documents] :"_doc"."_id"[Unique identifier] :"DBeE23kBo6bwxxzx9qzl", # can be likened to MySQL primary key, random generation"_version"[Version] :1."result"【 Results 】:"created", # create here indicates successful creation"_shards"[fragment] : {"total"[Shard-total] :2."successful"[Sharding - success] :1."failed"[Sharding - failed] :0
    },
    "_seq_no": 0."_primary_term": 1
}
Copy the code

Mistakes show

  1. The PUT submission is not allowed. An error will be reported
{
	"error": "Incorrect HTTP method for uri [/es/_doc] and method [GET], allowed: [POST]"."status": 405
}
Copy the code
  1. You cannot forget _doc, you will get an error
{
	"error": "Incorrect HTTP method for uri [/es] and method [POST], allowed: [GET, DELETE, PUT, HEAD]"."status": 405
}
Copy the code

Advanced operation

  1. Specify the generate ID
    • After the above data is created, the ES server will randomly generate one by default because the data unique ID is not specified
    • If you want to customize the unique identity, you need to specify at creation time:http://{{ip:port}}/es/_doc/1
      • http://{{ip:port}}//_doc/
    • The version field is updated when the request is repeated, instead of creating a new document
{
	"_index": "es"."_type": "_doc"."_id"[id = id] :"1"."_version"[The version is updated every time] :4."result": "updated"."_shards": {
		"total": 2."successful": 1."failed": 0
	},
	"_seq_no": 7."_primary_term": 1
}
Copy the code
  1. If you specify an ID, you can use PUT to create or update the document, but the request mode changes, and other content remains unchanged

2. Update the document

2.1 Full update

Instructions for

  • Enter the same URL as the new document request. If the request body changes, the original data content will be overwritten

Order sample

POST es/_doc/1
{
	"title": "Huawei Phone"."category": "Huawei"."images": "http://www.gulixueyuan.com/hw.jpg"."price": 4999.0
}
Copy the code

Return the sample

{
	"_index": "es"."_type": "_doc"."_id": "1"."_version"[Version number +1 for each modification] :7."result"【 Updated 】:"updated"."_shards"[Fragment information] : {"total": 2."successful": 1."failed": 0
	},
	"_seq_no": 10."_primary_term": 1
}
Copy the code

Mistakes show

  1. Don’t forget _doc for updates, otherwise an error will be reported
    • http://{{ip:port}}/es/1
{
	"error": {
		"root_cause": [{"type": "illegal_argument_exception"."reason": "Rejecting mapping update to [es] as the final mapping would have more than 1 type: [_doc, 1]"}]."type": "illegal_argument_exception"."reason": "Rejecting mapping update to [es] as the final mapping would have more than 1 type: [_doc, 1]"
	},
	"status": 400
}
Copy the code

2.2 Incremental Update

Instructions for

  • The default is full update by specifying _doc. If you want to update a specified field, change _doc to _update**
  • Update {“doc”: {“key”: value}}

Order sample

POST es/_update/1
{
	"doc": {
	    "price": 1999}}Copy the code

Return the sample

{
	"_index": "es"."_type": "_doc"."_id": "1"."_version": 8."result": "updated"."_shards": {
		"total": 2."successful": 1."failed": 0
	},
	"_seq_no": 11."_primary_term": 1
}
Copy the code
  • Query results, see price has been updated
{
	"_index": "es"."_type": "_doc"."_id": "1"."_version": 8."_seq_no": 11."_primary_term": 1."found": true."_source": {
		"title": "Huawei Phone"."category": "Huawei"."images": "http://www.gulixueyuan.com/hw.jpg"."price": 1999}}Copy the code

Mistakes show

  1. Do not forget the doc in the request parameter
    • Request parameters
{
    "price": 1999
}
Copy the code
  • The error message
{
	"error": {
		"root_cause": [{"type": "x_content_parse_exception"."reason": "[2:3] [UpdateRequest] unknown field [price]"}]."type": "x_content_parse_exception"."reason": "[2:3] [UpdateRequest] unknown field [price]"
	},
	"status": 400
}
Copy the code

3. Query documents

3.1 Single Query

Instructions for

  • When viewing a document, you need to specify the unique identity of the document, similar to the primary key query of data in MySQL

Order sample

GET es/_doc/1
Copy the code

Return the sample

{
	"_index"[Index Name] :"es"."_type"[Index type] :"_doc"."_id"[ID Information] :"1"."_version"[Current version] :4."_seq_no": 7."_primary_term": 1."found"[Query result, true indicates that the search is found, false indicates that the search is not found] :true."_source"[represents raw data information] : {"title": "Mi Phone"."category": "Millet"."images": "http://www.gulixueyuan.com/xm.jpg"."price": 3999}}Copy the code

Mistakes show

  1. Found is false if the document does not exist
{
	"_index": "es"."_type": "_doc"."_id": "12"."found"[Not found] :false
}
Copy the code

3.2 Querying All Information

Instructions for

  • You can query only a single document
  • Replace **_doc/ with _search**

Order sample

GET es/_search
Copy the code

Return the sample

{
	"took": 1."timed_out": false."_shards": {
		"total": 1."successful": 1."skipped": 0."failed": 0
	},
	"hits"[Hit statistics] : {"total"[Hit total] : {"value": 1."relation": "eq"
		},
		"max_score": 1."hits"[Match information] : [{"_index": "es"."_type": "_doc"."_id": "GBe923kBo6bwxxzxoKxP"."_score": 1."_source": {
					"title": "Mi Phone"."category": "Millet"."images": "http://www.gulixueyuan.com/xm.jpg"."price": 3999}}}Copy the code

Delete the document

4.1 Deleting a Single Document

Instructions for

  • Deleting a document does not immediately remove it from disk, it is simply marked as deleted (logical deleted)

Order sample

DELETE es/_doc/1
Copy the code

Return the sample

{
	"_index": "es"."_type": "_doc"."_id": "1"."_version"[Delete will also update the version] :9."result"[Delete result] :"deleted"."_shards"[Fragment deletion] : {"total": 2."successful": 1."failed": 0
	},
	"_seq_no": 12."_primary_term": 1
}
Copy the code

Mistakes show

  1. Delete a document that does not exist
{
	"_index": "es"."_type": "_doc"."_id": "1"."_version": 1."result"[Document not found] :"not_found"."_shards": {
		"total": 2."successful": 1."failed": 0
	},
	"_seq_no": 13."_primary_term": 1
}
Copy the code

4.2 Conditionally Deleting a Document

Instructions for

  • Generally, data is deleted based on the unique identity of a document. In practice, multiple pieces of data can be deleted based on conditions
  • Add multiple pieces of data without specifying an ID, using the script used to create the document
{
	"title": "Mi Phone"."category": "Millet"."images": "http://www.gulixueyuan.com/xm.jpg"."price": 3999
}
Copy the code
  • Requirement for conditional queryhttp://{{ip:port}}/es/_delete_by_query
    • Replace _doc with **_delete_by_query** delete from query
  • The query request parameter needs to be represented by the query field, in which match is used to delete the matching information
  • Note: Conditional deletion requires POST

Order sample

POST es/_delete_by_query
{
	"query": {
		"match": {
			"price": 3999}}}Copy the code

Return the sample

{
	"took": 28."timed_out"[Timeout] :false."total"[Total quantity] :4."deleted"[Delete quantity] :4."batches"[Batch] :1."version_conflicts": 0."noops": 0."retries": {
		"bulk": 0."search": 0
	},
	"throttled_millis": 0."requests_per_second": - 1."throttled_until_millis": 0."failures": []}Copy the code

Mistakes show

  1. Delete using POST, not DELETE, otherwise an error will be reported
{
	"error": "Incorrect HTTP method for uri [/es/_delete_by_query] and method [DELETE], allowed: [POST]"."status": 405
}
Copy the code