ElasticSearch入门2: 基本用法
转自大神的文章:https://www.cnblogs.com/liuxiaoming123/p/8087092.html
基本用法:
一、索引创建
(启动集群和索引请看上一篇文章:http://www.cnblogs.com/liuxiaoming123/p/8081883.html)
1.打开浏览器,输入请求:http://localhost:9100
2.点击后搜索book
3.点击OK 显示创建成功
4.在概览中查看
5.点击索引信息,查看结构化和非结构化索引信息
1.点击 复合查询
1.1 加入:book/novel/_mappers
1.2加入:
{ "novel": { "properties": { "title": { "type": "text" } } } }
1.3 勾选易读
1.4点击 验证JSON
1.5点击 提交请求
2.点击概览,刷新页面
2.1 点击索引信息
2.2 mappings 中 不为空则 表示 结构化索引创建成功
注:上面是结构化索引的创建在head插件中实现的,其中json编写较为繁琐,下面采用postman方式进行创建
1. 在postman中编写json字符串(采取put提交方式提交)
2. 请求路径:127.0.0.1:9200/people
3. json字符串
{ "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" } } } } }
4.json字符串的各字段介绍:
settings: 设置
number_of_shards: 分片数
number_of_replicas:备份数
mappings: 索引的映射
man: 映射名称,此处的映射名称只能有一个不能出现多个映射(这是一个设计失误,后面的版本将不再支持。官方给出解释是:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html )
properties:属性的集合 :(它下面的为各个属性,都是"属性名":{"(类型)type":"对应的类型"})注:其中Data属性有format设置日期格式
5.编写完毕,postman发送请求
6.浏览器刷新请求:http://localhost:9100
7.点击 索引信息
8.查看结构化索引:
二、插入
1.在postman中输入请求:127.0.0.1:9200/people/man/1 (PUT请求)
2.插入json 数据:
{ "name":"晓明", "country":"china", "age":26, "date":"1992-08-08" }
注:people 是索引, man是类型, 1是文档ID
3.点击send发送
4.查看浏览器 http://localhost:9100
未刷新之前:
刷新之后
5.点击数据浏览查看所插入的数据:此时ID是手动指定的ID
1.浏览器输入请求:127.0.0.1:9200/people/man/ (post请求) 点击send发送
json字符串:
{ "name":"Auto晓明", "country":"Autochina", "age":26, "date":"1992-08-08" }
2.查看浏览器 :http://localhost:9100
3.点击数据浏览:此时新生成的为自动生成的ID
三、修改
1.postman提交请求:127.0.0.1:9200/people/man/1/_update
2.请求json串为:
{ "doc":{ "name":"修改晓明" } }
4.查看浏览器:http://localhost:9100
第一种方式:
1. postman输入请求:127.0.0.1:9200/people/man/1/_update(post请求)
2.json字符串:
{
"script":{
"lang":"painless",
"inline":"ctx._source.age += 10"
}
}
注:script 是脚本标识
lang 是脚本语言
painless 是内置脚本语言
inline 是指定脚本内容
ctx 是脚本上下文
_source 为当前文档
3.浏览器查看:http://localhost:9100
第二种方式:
1. postman输入请求:127.0.0.1:9200/people/man/1/_update(post请求)
2.json字符串:
{ "script":{ "lang":"painless", "inline":"ctx._source.age = params.age", "params":{ "age":100 } } }
3.查看浏览器:http://localhost:9100
四、删除
1.postman输入请求: 127.0.0.1:9200/people/man/1 (DELETE方式)
2.查看浏览器:http://localhost:9200/ :ID等于1的已经成功删除
第一种:通过head插件直接删除索引
1.查看浏览器 :http://localhost:9100 删除book索引
2.输入 :删除 点击 “好”
3.显示删除成功
4.点击关闭
第二种:通过postman删除索引 删除people
1.postman输入请求:127.0.0.1:9200/people(DELETE方式) 点击send发送请求
2.查看浏览器:http://localhost:9100
五、查询
1.准备:
1.1 post创建机构化索引:
1.1.1 请求:127.0.0.1:9200/people (PUT方式)
1.1.2 json字符串:
1.2 postman插入数据:
1.2.1 请求:127.0.0.1:9200/book/novel/1 (post方式)
1.2.2 json 数据
{ "name":"晓明1", "country":"china1", "age":26, "date":"1992-08-08" }
1.postman请求:127.0.0.1:9200/book/novel/1(get方式)
1.postman请求:127.0.0.1:9200/book/_search(post请求)
2.请求json串:
2.1 全部查询
{ "query":{ "match_all":{} }, "from":1, "size":1 }
2.2 按关键字查询(含有按指定字段排序)
{ "query":{ "match":{ "name":"晓明" } }, "sort":[ {"_id":"desc"} ] }
3.解释请求json串:
match_all 是全部查询
query 是查询关键字
from 是从哪里查
size 是显示多少条数据
sort 是排序设置
(多组聚合)
1.postman请求:127.0.0.1:9200/book/_search(post请求)
2.请求json字符串:
{
"aggs":{
"group_by_name":{
"terms":{
"field":"name"
}
},
"group_by_country":{
"terms":{
"field":"country"
}
},
"group_by_date":{
"terms":{
"field":"date"
}
}
}
}
3.解析请求json字符串:
aggs:聚合查询关键字
group_by_word_count :聚合条件的名字(名字可自定义)
terms 关键词
field 是指定字段
4.点击send发送得到如下查询信息:
{ "took": 10, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1, "hits": [ { "_index": "book", "_type": "novel", "_id": "5", "_score": 1, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 1, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 1, "_source": { "name": "晓明9", "country": "china9", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 1, "_source": { "name": "晓明1", "country": "china1", "age": 26, "date": "1992-08-08" } } ] }, "aggregations": { "group_by_date": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 713232000000, "key_as_string": "1992-08-08T00:00:00.000Z", "doc_count": 4 } ] }, "group_by_country": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "china", "doc_count": 2 }, { "key": "china1", "doc_count": 1 }, { "key": "china9", "doc_count": 1 } ] }, "group_by_name": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "明", "doc_count": 4 }, { "key": "晓", "doc_count": 4 }, { "key": "1", "doc_count": 1 }, { "key": "9", "doc_count": 1 } ] } } }
注: 若聚合查询报错 请参考:http://www.cnblogs.com/liuxiaoming123/p/8117786.html
PUT 127.0.0.1:9200/book/_mapping/novel/ { "properties": { "name": { "type": "text", "fielddata": true }, "country": { "type": "text", "fielddata": true } } }
注:聚合查询其他用法:
计算年龄的统计 总条数、最大值、最小值、平均值、总和 若:stats改成min则只显示最小值 请求URL: POST 127.0.0.1:9200/book/_search 请求json: { "aggs":{ "group_by_age":{ "stats":{ "field":"age" } } } } 返回结果: { "took": 33, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1, "hits": [ { "_index": "book", "_type": "novel", "_id": "5", "_score": 1, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "8", "_score": 1, "_source": { "name": "晓明", "country": "china", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "9", "_score": 1, "_source": { "name": "晓明9", "country": "china9", "age": 26, "date": "1992-08-08" } }, { "_index": "book", "_type": "novel", "_id": "1", "_score": 1, "_source": { "name": "晓明1", "country": "china1", "age": 26, "date": "1992-08-08" } } ] }, "aggregations": { "group_by_age": { "count": 4, "min": 26, "max": 26, "avg": 26, "sum": 104 } } }