创建索引
-
创建非结构的索引:
通过postman软件,执行http请求。
put方法,
http://192.168.7.6:9200/people
body如下:
{
"settings": {
"number_of_shards":3,
"number_of_replicas":1
}
}
-
创建结构化的索引
put方法
http://192.168.7.6:9200/people
{
"settings": {
"number_of_shards":3,
"number_of_replicas":1
},
"mappings": {
"man":{
"properties":{
"name": {
"type": "text"
},
"country": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"data": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"woman": {
}
}
}
创建后,通过Elasticsearch Head观察,可以看到创建完成。
插入数据
-
指定ID插入
post方法:
http://192.168.7.6:9200/people1/man/1
插入id为1的数据到people1索引
body体如下:
{
"name": "wali",
"country": "Chian",
"age": 30,
"date": "1987-03-07"
}
插入后,返回结果如下:
{
"_index": "people1",
"_type": "1",
"_id": "AWpDDDpxi2_ZaFqwJNba",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
通过chrome的elasticsearch head插件可以看到插入的数据:
-
自动生成ID插入
post方法
http://192.168.7.6:9200/people1/man/
插入体:
{
"name": "wali_brother1",
"country": "china1",
"age": 401,
"date": "1987-08-07"
}
修改数据
post方法
http://192.168.7.6:9200/people1/man/1/_update
body体,修改name
{
"doc":{
"name": "whoami"
}
}
修改数据
删除数据
delete方法删除id为1的数据
http://192.168.7.6:9200/people1/man/1
删除索引
delete方法
http://192.168.7.6:9200/monitor2018_10
查询
简单查询
get方法,指定id
http://192.168.7.6:9200/people1/man/3
返回结果:
{
"_index": "people1",
"_type": "man",
"_id": "3",
"_version": 1,
"found": true,
"_source": {
"name": "wali_brother03",
"country": "china",
"age": 403,
"date": "1987-08-22"
}
}
条件查询
post方法
http://192.168.7.6:9200/people1/_search
body体:
{
"query": {
"match_all": {}
}
}
默认返回10条
指定返回条数
{
"query": {
"match_all": {}
},
"size": 2
}
排序:
{
"query": {
"match": {
"name": "wali"
}
},
"sort": [
{"age": {"order": "desc"}}
]
}