ElasticSearch API基本格式
http://<ip>:<port>/<索引>/<类型>/<文档id>
创建索引
方法一:打开网址 http://localhost:9100 创建索引,索引名称必须小写,不能有中划线
方法二:使用postman工具创建索引
# 创建一个people索引
put 127.0.0.1:9200/people
{
# 指定索引配置
"settings“: {
# 指定分片数
”number_of_shards“: 3,
# 指定备份数
"number_of_replicas": 1
},
# 映射定义
"mappings": {
”man“:{
# 属性定义
"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"}
}
},
”woman“:{
}
}
}
插入数据
1)指定文档id插入
使用postman工具插入数据
put 127.0.0.1:9200/people/man/1
{
"name": "瓦力",
"country": "China",
"age": "30",
"date": "1987-03-07"
}
2)自动产生文档id插入
post 127.0.0.1:9200/people/man/
{
"name": "超重瓦力",
"country": "China",
"age": "40",
"date": "1977-03-07"
}
修改数据
1)直接修改文档
使用postman工具修改数据
post 127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name": ”谁是瓦力“
}
}
2)脚本修改文档
post 127.0.0.1:9200/people/man/1/_update
{
"script":{
# 指定脚本语言
"lang": ”painless“,
# 指定脚本内容(年龄加10)
”inline“: "ctx._source.age += 10"
}
}
{
"script":{
# 指定脚本语言
"lang": ”painless“,
# 指定脚本内容(年龄指定为100)
”inline“: "ctx._source.age = params.age",
"params": {
"age": 100
}
}
}
删除文档
使用postman工具删除文档
delete 127.0.0.1:9200/people/man/1
删除索引
方法一:使用网页删除 http://localhost:9200 动作 --> 删除
方法二:使用postman工具删除索引
delete 127.0.0.1:9200/people
简单查询
使用postman工具查询
# 查询id为1的文档
get 127.0.0.1:9200/people/man/1
# 条件查询
post 127.0.0.1:9200/people/_search
{
"query":{
# 条件,不输入则查询所有
# "match_all": {}
# 根据关键词查询
"match": {
"name": "ElasticSearch"
}
},
# 排序
”sort“: {
{"date": {"order": "desc"}}
},
# 从哪里返回数据
"from": 1,
# 返回几条数据
"size: 1
}
聚合查询
使用postman工具查询
post 127.0.0.1:9200/people/_search
{
# 聚合查询关键词
”aggs“: {
# 给聚合条件起名字
"group_by_age": {
"terms":{
# 指定聚合字段
"field": "age"
}
},
"group_by_date": {
"terms":{
"field": "date"
}
}
}
}
最大 最小 平均 取和 计数函数
post 127.0.0.1:9200/people/_search
{
”aggs“: {
”grades_word_count“: {
# 统计计算
# "stats": {
# 取最小值
”min“: {
# 指定给那个字段进行计算
field: "age"
}
}
}
}