官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
1.创建people索引
put http://localhost:9200/people
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
}
返回
{"acknowledged":true,"shards_acknowledged":true,"index":"people"}
2.添加一条记录
POST http://localhost:9200/people/_doc
{
"name":"guoy",
"country":"china",
"age":28,
"date":"2019-04-19"
}
查询
POST http://localhost:9200/people/_doc/1/
{
"name":"niurj",
"country":"china",
"age":28,
"date":"2019-04-19"
}
或者
http://localhost:9200/book/_search
{
"query": {
"match": {
"author": "小花"
}
}
}
3.更新
post http://localhost:9200/people/_doc/1/_update
{
"script":{
"lang":"painless",
"inline":"ctx._source.age += 10"
}
}
或者
{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age",
"params":{
"age":99
}
}
}
4.删除一条数据
delete http://localhost:9200/people/_doc/1
返回
{"_index":"people","_type":"_doc","_id":"1","_version":6,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":5,"_primary_term":1}
或者 查询删除
POST http://localhost:9200/book/_delete_by_query
{
"query": {
"match": {
"author": "小葡萄"
}
}
}
5.删除一个索引
delete http://localhost:9200/people
返回
{"acknowledged":true}
6.高级查询
6.1创建索引book
put http://localhost:9200/book
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"properties":{
"word_count":{
"type":"integer"
},
"author":{
"type":"keyword"
},
"title":{
"type":"text"
},
"publish_date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
}
插入一条数据
PUT http://localhost:9200/book/_doc/1
{
"word_count":1000,
"author":"小葡萄",
"title":"调皮捣蛋",
"public_date":"2020-01-20"
}
6.2请求体查询
获取集群中所有的索引
get http://localhost:9200/_all
老版本:_search
返回
{
"book": {
"aliases": {},
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"public_date": {
"type": "date"
},
"publish_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"title": {
"type": "text"
},
"word_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"creation_date": "1579511120979",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "kGNK0FFFRyu1CjvsDXa7wQ",
"version": {
"created": "7050199"
},
"provided_name": "book"
}
}
},
"mc": {
"aliases": {},
"mappings": {
"properties": {
"age": {
"type": "long"
},
"interests": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1577696612860",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "vHQOchJQTlWdW-fcqmDwAQ",
"version": {
"created": "7050199"
},
"provided_name": "mc"
}
}
},
"people": {
"aliases": {},
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"country": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"name": {
"type": "text"
}
}
},
"settings": {
"index": {
"creation_date": "1579510580371",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "-X8sxwteRMmXfhLQ17wiWA",
"version": {
"created": "7050199"
},
"provided_name": "people"
}
}
}
}
查看多个索引以逗号分隔支持表达式
GET http://localhost:9200/book,p*
返回 可以看到返回了book索引和people索引
{
"book": {
"aliases": {},
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"public_date": {
"type": "date"
},
"publish_date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"title": {
"type": "text"
},
"word_count": {
"type": "integer"
}
}
},
"settings": {
"index": {
"creation_date": "1579511120979",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "kGNK0FFFRyu1CjvsDXa7wQ",
"version": {
"created": "7050199"
},
"provided_name": "book"
}
}
},
"people": {
"aliases": {},
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"country": {
"type": "keyword"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"name": {
"type": "text"
}
}
},
"settings": {
"index": {
"creation_date": "1579510580371",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "-X8sxwteRMmXfhLQ17wiWA",
"version": {
"created": "7050199"
},
"provided_name": "people"
}
}
}
}