Record, a crud operation of elasticsearch from creating an index to inserting data.
1. Create an index
curl -XPUT "http://192.168.99.1:9200/productindex" -d' { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "product" : { "properties": { "name" : { "type": "text", "fielddata": true }, "price" : { "type": "long" } } } } }'
2. Insert data
1. If the data to be inserted already exists, perform the replacement operation, and if it does not exist, perform the insert operation
Note: 1. Here is the value of the manually specified id
2. If the data productindex/product/1 exists, if there is only the name field in the request body, then the price field of this row of data will be deleted, that is, the replacement operation is performed instead of the update operation
curl -XPUT "http://192.168.99.1:9200/productindex/product/1" -d' { "name" : "pen", "price" : 2 }'
2. If the data already exists, then an error is reported, and if it does not exist, the insert operation is performed
Note: You only need to add _create or add op_type=create here
curl -XPUT "http://192.168.99.1:9200/productindex/product/1?op_type=create" -d' { "name" : "new pen", "price" : 2 }'
or
curl -XPUT "http://192.168.99.1:9200/productindex/product/1/_create" -d' { "name" : "new pen", "price" : 2 }'
3. Automatically generate the value of id when inserting data
Note: You need to use the post request here instead of the put request.
curl -XPOST "http://192.168.99.1:9200/productindex/product" -d' { "name" : "pen", "price" : 2 }'
3. Modify the data
1. Use _update or op_type=update to specify the modification, and there is no error in the data
curl -XPOST "http://192.168.99.1:9200/productindex/product/1/_update" -d' { "doc": { "name" : "update new name" } }'
2. Use optimistic lock version to control the modification ( to prevent data modification errors in the case of concurrency )
The value of the version in the back is the current version number of the data. If the version number of the data in es changes, the modification fails.
curl -XPOST "http://192.168.99.1:9200/productindex/product/1/_update?version=7" -d' { "doc": { "name" : "update new name" } }'
3. Use the upsert operation, if the data does not exist, perform the upsert part ( insert operation ), otherwise perform the update operation
curl -XPOST "http://192.168.99.1:9200/productindex/product/12/_update" -d' { "doc":{ "name":"update new value" }, "upsert" : { "name" : "The data does not exist to perform the insert operation", "price" : 1 } }'
Or do the following (using doc_as_upsert, if the document does not exist, the doc part is used as the upsert part)
curl -XPOST "http://192.168.99.1:9200/productindex/product/13/_update" -d' { "doc":{ "name":"update new value" }, "doc_as_upsert" : true }'
4. Highlight query
1. Insert a new data
curl -XPUT "http://192.168.99.1:9200/productindex/product/29" -d' { "name" : "new name", "desc" : "this is a desc field" }'
2. Match the value of has name in the name field or the value of desc in the desc field, and highlight the matched value
curl -XGET "http://192.168.99.1:9200/productindex/product/_search" -d' { "query": { "bool": { "should": [ { "match": { "name": "has name" } }, { "term": { "desc": { "value": "desc" } } } ] } }, "highlight": { "pre_tags": "<span style=\"color:red\">", "post_tags": "</span>", "fields": { "name" : { "pre_tags": "<span style=\"color:blue\">", "post_tags": "</span>" }, "*" : { } } } }'
5. Delete data
1. Delete the data with id=1
curl -XDELETE "http://192.168.99.1:9200/productindex/product/1"