ElaticSearch使用总结

在分布式报警系统中,使用ES有一段时间了。为了方便后续参考,对ES的用法做个简单的总结和概括。

ElaticsSearch安装

在服务器上,安装ElasticSearch 部署非常方便,可以去官网下载。

ES官网提供了跨平台版本在不同的环境下安装,安装过程也比较简单。因为ES每个发布大版本更新,接口和数据结构改动还的比较大,如果要搭建跟生产环境一致的版本,版本要和生产环境一致。

下载

#Download the Elasticsearch archive for your OS:

#Linux: [elasticsearch-7.6.2-linux-x86_64.tar.gz](https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz)

$curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz

#macOS: [elasticsearch-7.6.2-darwin-x86_64.tar.gz](https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-darwin-x86_64.tar.gz)

$curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-darwin-x86_64.tar.gz

#Windows: [elasticsearch-7.6.2-windows-x86_64.zip]
下载版本链接
(https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-windows-x86_64.zip)

解压:

#Linux:

$tar -xvf elasticsearch-7.6.2-linux-x86_64.tar.gz

#macOS:

$tar -xvf elasticsearch-7.6.2-darwin-x86_64.tar.gz

#Windows PowerShell:

$Expand-Archive elasticsearch-7.6.2-windows-x86_64.zip

**从bin目录启动 Elasticsearch :**

Linux and macOS:

$cd elasticsearch-7.6.2/bin./elasticsearch

Windows:

$cd elasticsearch-7.6.2\bin.\elasticsearch.bat

这样启动完成后,就是单个节点的 Elasticsearch cluster 。 如果想要在本地运行多个实例并加入到这个集群,你需要在某个node节点上指定确定的不同数据路径和目录路径。

#Linux and macOS:

$./elasticsearch -Epath.data=data2 -Epath.logs=log2./elasticsearch -Epath.data=data3 -Epath.logs=log3

#Windows:

$.\elasticsearch.bat -E path.data=data2 -E path.logs=log2.\elasticsearch.bat -E path.data=data3 -E path.logs=log3

要新增加节点,要指定每个新节点的唯一的ID。 所以在跨节点时,需要在某个实例里配置其它节点的实例ID,实例ID唯一且不能重复。 指定相同的集群名字。Elasticsearch 会自动把其它节点加入到集群中。

启动完成后通过 curl 来发起GET请求来验证:

$curl -XGET  http://127.0.0.1:9200
{
  "name" : "543N5TN",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "nwKqQ7lUQ3aSNtXQT2vgIQ",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

ElaticsSearch优化建议

JVM设置

  • 修改JVM - config/jvm.options, 7.1下载的默认设置为1GB。

配置的建议

  • Xms和 Xmx 设置成一样
  • Xmx 不要超过机器内存的50%
  • 设置不要超过 30GB

ElaticsSearch插件安装

ElaticSearch 插件比较丰富,可以通过插件对系统进行扩展,包括服务发现、数据分析、安全、备份等功能
进入ElaticsSearch 后,通过 bin/elasticsearch-plugin 来管理插件

bin/elasticsearch-plugin list  # 列出插件
bin/elasticsearch-plugin install analysis-icu # 安装分词软件

Kibana的安装及管理

Kibana 非常方便的对ElaticSearch 进行友好的界面管理。安装也是简单,开箱即用的。

有两个问题需要注意:

  • Kibana 软件包版本要和连接的 ElaticSearch版本 匹配,至少大版本要匹配;
  • Kibana 启动前,一定要确保 管理的ElaticSearch 已经启动,并能正常连上。

下载完Kibana 软件包后,直接通过 bin/kibana 就能启动,注意,启动之前一定要确保端口是,
通过 http://127.0.0.1:5601 端口就可以访问。

开发环境构建

在本地开发过程中,我们要快速的构建一个单实例的ElaticSearch 并集成Kibana 功能。 建议用docker 部署,用docker-compose 一键启动。
我这里贴出来两个6和7版本的的yml 。

  • 7.0.0 的docker-compose.yml 配置:
version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.0.0
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
      - 9300:9300
  kibana:
    image: docker.elastic.co/kibana/kibana:7.0.0
    ports:
      - 5601:5601
  • 6.4.3 的docker-compose.yml 配置
version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.4.3
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
      - 9300:9300
  kibana:
    image: docker.elastic.co/kibana/kibana:6.4.3
    ports:
      - 5601:5601

通过下面的命令来启动

$docker-compose -f docke-compose.yml up
  • 验证ES
curl -XGET http://127.0.0.1:9300
  • Kibana
    浏览器访问 http://127.0.0.1:5601/

申展知识
进入Kibana 主要用到 Dev Tool 来完成 一些语句的构建。
快捷键的用法

  • cmd + / (查看API 帮助文档)
  • cmd + option + l
  • cmd + option + o
  • cmd + option + shitf + o

操作语句基于Kibana

构建和查询语句可以通过curl 命令发送,也可以在Kibana里输入。
http://127.0.0.1:5601/app/kibana#/dev_tools/console?_g=()

基本集群管理

#查看集群信息
GET /_cat/health?v
#查看索引信息
GET  /_cat/indices 
#对文档个数排序
GET /_cat/indices?v&s=docs.count:desc

建立index 索引文件并添加数据

POST  /index-name/_doc
{
           "category": "infrastructure",
           "host_ipv4": "10.10.10.10",
          "host_name": "service-mgmt-monitor-bbc688fd4-tdmt2",
          "percent-idle": 0.0495321933667832,
          "percent-nice": 0.000063788671137482,
          "percent-softirq": 0.0023466242849049706,
          "percent-steal": 0,
          "percent-system": 0.05236409891722033,
          "percent-used": 100,
          "percent-user": 0.0948436883906097,
          "percent-wait": 0.0008496063693443324,
          "resource": "10",
           "service": "service-mgmt-monitor",
           "timestamp": 1583551220281,
          "type": "cpu-usage"
}

建立空index 索引文件并添加数据

#PUT 建立空索引
PUT  /index-name?pretty
# POST 添加数据
POST /index-name/_doc
{
           "category": "infrastructure",
           "host_ipv4": "10.10.10.10",
          "host_name": "service-mgmt-monitor-bbc688fd4-tdmt2",
          "percent-idle": 0.0495321933667832,
}

查看索引的文件

#查看索引数据格式
GET  /index-name
#查看索引文件的数据
GET /index-name/_search
#查看带有过滤条件的索引文件
GET /index-name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "from": 1583539220281,
              "to": 1583564420000
            }
          }
        },
        {
          "range": {
            "percent-used": {
              "gte": 0.12
            }
          }
        },
        {
          "term": {
            "service": "service-mgmt-monitor"
          }
        }
      ]
    }
  },
  "aggs": {
    "instance_aggr": {
      "terms": {
        "field": "host_ipv4.keyword"
      }
    }
  },
  "size": "0"
}

删除索引

DELETE  /index-name

查看索引设置信息

GET /index-name/_settings

修改索引最大返回值

PUT /index-name/_settings
{
  "index": {
    "max_result_window": 2147483647
  }
}

新增模板字段

PUT _template/template_indexname_base
{
    "order": 1,
    "version": 100,
    "index_patterns": [
      "indexname-*"
    ],
    "settings": {
      "index": {
        "number_of_shards": "1",
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "_doc": {
        "dynamic": true,
        "properties": {
          "timestamp": {
            "type": "date",
            "format": "epoch_millis"
          },
          "category": {
            "type": "keyword",
            "index": true
          },
          "host_name": {
            "type": "keyword",
            "index": true
          },
          "host_ipv4": {
            "type": "ip",
            "index": true
          },
          "system": {
            "type": "keyword",
            "index": true
          },
          "type": {
            "type": "keyword",
            "index": true
          },
          "service": {
            "type": "keyword",
            "index": true
          },
          "name": {
            "type": "keyword",
            "index": true
          }
        }
      }
    },
    "aliases": {}
  }

_bulk API 和_mget 和_msearch操作

这三种操作都是批量操作方式,主要应用于不同的使用场景。

_bulk 支持在一次API请求中,对不同的索引进行批量操作。 一次操作,批量更新操作。每条操作,批量返回结果。
_mget:根据一些列的文档ID进行查询,只要一次API call 就能获取批量的数据。
_msearch:
例如

POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

转化为具体的语句为

{
   "took": 30,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_version": 1,
            "result": "created",
            "_shards": {
               "total": 2,
               "successful": 1,
               "failed": 0
            },
            "status": 201,
            "_seq_no" : 0,
            "_primary_term": 1
         }
      },
      {
         "delete": {
            "_index": "test",
            "_type": "_doc",
            "_id": "2",
            "_version": 1,
            "result": "not_found",
            "_shards": {
               "total": 2,
               "successful": 1,
               "failed": 0
            },
            "status": 404,
            "_seq_no" : 1,
            "_primary_term" : 2
         }
      },
      {
         "create": {
            "_index": "test",
            "_type": "_doc",
            "_id": "3",
            "_version": 1,
            "result": "created",
            "_shards": {
               "total": 2,
               "successful": 1,
               "failed": 0
            },
            "status": 201,
            "_seq_no" : 2,
            "_primary_term" : 3
         }
      },
      {
         "update": {
            "_index": "test",
            "_type": "_doc",
            "_id": "1",
            "_version": 2,
            "result": "updated",
            "_shards": {
                "total": 2,
                "successful": 1,
                "failed": 0
            },
            "status": 200,
            "_seq_no" : 3,
            "_primary_term" : 4
         }
      }
   ]
}

_msearch 批量查询例子

$ cat requests
{"index" : "test"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{"index" : "test", "search_type" : "dfs_query_then_fetch"}
{"query" : {"match_all" : {}}}
{}
{"query" : {"match_all" : {}}}

{"query" : {"match_all" : {}}}
{"search_type" : "dfs_query_then_fetch"}
{"query" : {"match_all" : {}}}

$ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo

高阶

用curl 来实现对 ES是 操作。
用curl 语句 直接对ES 进行操作和变更,前提是,确定有ES的操作权限。
-d 可以为json 文件的格式的数据或者 文件都可以。

#新建空索引
curl  -H 'Content-Type: application/json'  -X PUT http://127.0.0.1:9200/student-index?pretty

#插入带数据索引
curl  -H 'Content-Type: application/json'  -X POST http://127.0.0.1:9200/student-index/_doc -d '{"name":"kevin","age": 7}'

#指定ID插入数据(id=100)
#如果不指定ID,默认就是随机生成一个ID号。
curl  -H 'Content-Type: application/json'  -X POST http://127.0.0.1:9200//student-index/_doc/100 -d '{ "name": "kevin","age": 7, "school":"ChangPing The Frist Middle School"}'


#增加字段
curl  -H 'Content-Type: application/json'  -X POST http://127.0.0.1:9200/student-index/_update/100 -d '{"school":"ChangPing The Frist middle School"}'

#指定ID修改内容(原来的数据被删除)
PUT /student-index/_doc/100
{
  "name":"Tom"
}

#删除数据(删除ID为100的数据)
DELETE /student-index/_doc/100

Index 内容 补充

每个index 都有元数据,这么几大块组成:

{
        "_index": "student-index",  //index 名字
        "_type": "_doc", //文档类型名称
        "_id": "100", // 文档数据唯一ID
        "_score": 1, //  打分
        "_source": {  //原始文档的JSON 数据
          "name": "kevin",
          "age": 7,
          "school": "ChangPing The First Middle School"
        }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容