Elasticsearch安装和简单介绍

Elasticsearch安装和简单介绍

一、ES和kibana安装

elasticsearch-5.3.1

 curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.1.zip
 unzip elasticsearch-5.3.1.zip
 mv elasticsearch-5.3.1 elasticsearch

OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x000000008a660000, 1973026816, 0) failed; error=‘Cannot allocate memory’ (errno=12)

vim config/jvm.options
# 修改-Xms1g  -Xmx1g

配置systemd systemctl cat elasticsearch.service

# /usr/lib/systemd/system/elasticsearch.service
[Unit]
Description=elasticsearch.service
After=network.target

[Service]
EnvironmentFile=-/etc/elasticsearch/elasticsearch
ExecStart=/usr/local/elasticsearch/bin/elasticsearch
PIDFile=/usr/local/elasticsearch/run/elasticsearch.pid
StandardOutput=journal
StandardError=inherit
LimitNOFILE=65536
TimeoutStopSec=0
SendSIGKILL=no
SuccessExitStatus=143
User=centos
Restart=on-failure

[Install]
WantedBy=default.target

启动

systemctl start elasticsearch.service
systemctl enable elasticsearch.service
systemctl status elasticsearch.service

验证:

curl 'http://localhost:9200/?pretty'
{
  "name" : "vMSwjnc",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "f4wKVE07Qie3q0Ld_LSbtg",
  "version" : {
    "number" : "5.3.1",
    "build_hash" : "5f9cf58",
    "build_date" : "2017-04-17T15:52:53.846Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.2"
  },
  "tagline" : "You Know, for Search"
}

kibana

$ curl -L -O https://artifacts.elastic.co/downloads/kibana/kibana-5.3.1-linux-x86_64.tar.gz
$ tar zxvf kibana-5.3.1-linux-x86_64.tar.gz
# 修改 kibana/config/kibana.yml
server.host: "0.0.0.0"

同样也可以按照,用elasticsearch启动。启动完成后,在浏览器访问5672端口。找到Dev Tools菜单。

二、ES 基本概念

Elasticsearch是一个分布式,REST风格的搜索和分析引擎,能够解决越来越多的用例。 作为弹性堆栈的核心,它集中存储您的数据,以便分析搜索。

  • 分布式的实时文件存储,每个字段都被索引并可被搜索
  • 分布式的实时分析搜索引擎
  • 可以扩展到上百台服务器,处理PB级结构化或非结构化数据

Near Realtime (NRT) Elasticsearch 是一个接近实时的搜索平台。这意味着,从索引一个文档直到这个文档能够被搜索到有一个很小的延迟(通常是 1 秒)

集群(Cluster) 一个集群就是由一个或多个节点组织在一起, 它们共同持有你全部的数据, 并一起提供索引和搜索功能。 一个集群由一个唯一的名字标识, 这个名字默认就是“elasticsearch”。

节点(node) 一个节点是你集群中的一个服务器,作为集群的一部分,它存储你的数据,参与集群的索引和搜索功能。

索引(index) 索引是具有某种相似特征的文档的集合。一个索引由一个名字来 标识(必须全部是小写字母的),且当我们要对这个索引中的文档进行索引、搜索、更新和删除的时候,都要使用到这个名字。在单个集群中,您可以根据需要定义任意多的索引。

类型(Type) 一个索引可以有多个类型(type),就像一个数据库可以有多张表一样。类型是索引的分类/分区。通常,为具有一组相同字段的文档定义了一种类型。

文档Document 文档是可以索引的基本信息单元。例如,您可以有单个客户的文档,单个产品的文档,单个订单的文档,文档以JSON表示。

索引/类型中,想存储多少文档都行。请注意,尽管文档物理上位于索引中,但文档实际上必须被索引/赋予一个索引的type。

分片&复制 Elasticsearch提供了将索引划分成多片的能力,这些片叫做分片。当你创建一个索引的时候,你可以指定你想要的分片的数量。每个分片本身也是一个功能完善并且独立的“索引”,这个“索引” 可以被放置到集群中的任何节点上。

分片之所以重要,主要有两方面的原因:

  • 允许你水平分割/扩展你的内容容量
  • 允许你在分片(位于多个节点上)之上进行分布式的、并行的操作,进而提高性能/吞吐量

Elasticsearch允许你创建分片的一份或多份拷贝,这些拷贝叫做复制分片,或者直接叫复制。 复制之所以重要,有两个主要原因:

  • 在分片/节点失败的情况下,复制提供了高可用性。复制分片不与原/主要分片置于同一节点上是非常重要的。
  • 因为搜索可以在所有的复制上并行运行,复制可以扩展你的搜索量/吞吐量

三、简单RESTful API

下面的API都在kibana的Dev Tools工具测试:

# health
GET /_cat/health?v

# list node
GET /_cat/nodes?v


#######INDEX############

GET /_cat/indices?v

GET /_count?pretty

PUT /customer?pretty

GET /customer/_search

DELETE /customer?pretty

##########document###############
# create
PUT /customer/external/1?pretty
{
  "name": "John Doe"
}
# get
GET /customer/external/1?pretty

# update
POST /customer/external/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}

# update
POST /customer/external/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}

# _bulk
POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}



############## QUERY ######################
POST /bank/account/0?pretty
{"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}

POST /bank/account/1?pretty 
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

GET /bank/_search?q=*&sort=account_number:asc&pretty

GET /bank/_search
{
  "query": { "match_all": {} }
}
GET /bank/_search
{
  "query": { "match_all": {} },
  "size": 1
}
GET /bank/_search
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}

GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}
GET /bank/_search
{
  "query": { "match": { "account_number": 2 } }
}
GET /bank/_search
{
  "query": { "match": { "address": "244 Columbus Place" } }
}

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "244" } },
        { "match": { "address": "Place" } }
      ]
    }
  }
}
CONTENTS