Kibana 和 Elasticsearch 使用
1. 文档地址
- Elasticsearch 下载地址:https://www.elastic.co/cn/downloads/elasticsearch
- Elasticsearch 英文文档 :https://www.elastic.co/guide/en/elasticsearch/reference/master/index.html
- Elasticsearch 中文文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/running-elasticsearch.html
- Kibana 下载文档: https://www.elastic.co/cn/downloads/kibana
- Kibana 用户指南 : https://www.elastic.co/guide/cn/kibana/current/introduction.html
2. Elasticsearch 的安装
-
Elasticsearch 是一个分布式文件存储系统,存储数据的方式和 MongoDB一样,是面向文档存储; -
从指定网站下载 WINDOWS 版本压缩包到本地; -
解压打开 /bin 文件夹 运行 elasticsearch.bat 启动成功 -
注意 Elasticsearch 是基于java开发的,所以本地要有 java环境; -
验证启动是否成功: cmd 环境下 访问 curl http://localhost:9200/?prett -
如果启动成功,应该会得到以下响应 {
"name" : "DESKTOP-N23GA7R",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "8PYuCc5ATvymKw5brgP2qA",
"version" : {
"number" : "7.14.0",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
"build_date" : "2021-07-29T20:49:32.864135063Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
3. Kibana 的安装
-
Kibana 是操作 Elasticsearch 数据的可视化 Web 端 -
Kibana 是一个开源分析和可视化平台,旨在与 Elasticsearch 配合使用。您可以使用 Kibana 搜索、查看存储在 Elasticsearch 索引中的数据并与之交互。您可以轻松地执行高级数据分析并在各种图表、表格和地图中可视化您的数据。 -
从指定网站下载 WINDOWS 版本压缩包到本地; -
解压打开 /bin 文件夹 运行 kibana.bat 启动成功 -
访问 http://localhost:5601/ 显示 Kibana Web端 -
打开 Dev Tools - Elastic 访问 Elasticsearch 操作控制端,进行Elasticsearch 数据操作; [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RJ6YHKHg-1628674785691)(C:\Users\C\Desktop\1628238374194.png)]
4. Elasticsearch 命令使用
-
索引(名词): 如前所述,一个 索引 类似于传统关系数据库中的一个 数据库 ,是一个存储关系型文档的地方。 索引 (index) 的复数词为 indices 或 indexes 索引(动词): 索引一个文档 就是存储一个文档到一个 索引 (名词)中以便被检索和查询。这非常类似于 SQL 语句中的 INSERT 关键词,除了文档已存在时,新文档会替换旧文档情况之外。 -
使用 Kibana Dev Tools 操作命令; -
在 Dev Tools 中使用的命令为简化命令,例如 // 计算集群中完整的文档数量,完整写法如下:
curl -XGET 'localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}'
// Dev Tools 中使用缩写格式来展示这些 curl 示例,所谓的缩写格式就是省略请求中所有相同的部分,例如主机名、端口号以及 curl 命令本身
// 缩略格式
GET /_count
{
"query": {
"match_all": {}
}
}
-
常用命令如下 -
PUT : 新增数据/修改数据 -
GET: 可以用来检索文档 -
DELETE: 删除文档 -
HEAD : 检查文档是否存在
GET /_count?pretty
{
"query": {
"match_all": {}
}
}
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
GET /megacorp/employee/1
GET /megacorp/employee/_search?pretty
GET /megacorp/employee/_search?q=last_name:Smith
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
文档中含有查询条件中的任何一个元素就能进行相似性匹配。
{
...
"hits": {
"total": 2,
"max_score": 0.16273327,
"hits": [
{
...
"_score": 0.16273327,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
...
"_score": 0.016878016,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
{
...
"hits": {
"total": 1,
"max_score": 0.23013961,
"hits": [
{
...
"_score": 0.23013961,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
},
"highlight": {
"about": [
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [interests] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 中间省略
"status" : 400
}
PUT /megacorp/_mapping
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
GET _cat/indices?v
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}
GET /megacorp/employee/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
|