什么是 Elasticsearch
Elasticsearch is a distributed RESTful search engine built for the cloud. Features include:
- Distributed and Highly Available Search Engine.
Each index is fully sharded with a configurable number of shards.
Each shard can have one or more replicas.
Read / Search operations performed on any of the replica shards. - Multi Tenant.
Support for more than one index.
Index level configuration (number of shards, index storage, …). - Various set of APIs
HTTP RESTful API
Native Java API.
All APIs perform automatic node operation rerouting. - Document oriented
No need for upfront schema definition.
Schema can be defined for customization of the indexing process. - Reliable, Asynchronous Write Behind for long term persistency.
- (Near) Real Time Search.
- Built on top of Lucene
Each shard is a fully functional Lucene index
All the power of Lucene easily exposed through simple configuration / plugins. - Per operation consistency
Single document level operations are atomic, consistent, isolated and durable. - Open Source under the Apache License, version 2 (“ALv2”)
下载
[root@ubuntu ~]# wget -c https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.1.0.rpm
安装 Java
yum -y install java
安装
[root@ubuntu ~]# rpm -ivh elasticsearch-6.1.0.rpm
警告:elasticsearch-6.1.0.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
准备中... ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
正在升级/安装...
1:elasticsearch-0:6.1.0-1 ################################# [100%]
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service
执行
[root@ubuntu ~]# sudo systemctl daemon-reload
[root@ubuntu ~]# sudo systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@ubuntu ~]# sudo systemctl start elasticsearch.service
[root@ubuntu ~]# sudo systemctl status elasticsearch.service
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2017-12-14 20:26:12 CST; 12s ago
Docs: http://www.elastic.co
Main PID: 9712 (java)
CGroup: /system.slice/elasticsearch.service
└─9712 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTo...
12月 14 20:26:12 ubuntu.novalocal systemd[1]: Started Elasticsearch.
12月 14 20:26:12 ubuntu.novalocal systemd[1]: Starting Elasticsearch...
配置文件
[root@ubuntu ~]# vim /etc/elasticsearch/elasticsearch.yml
[root@ubuntu ~]# cat /etc/elasticsearch/elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application # 集群名称
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1 # 节点信息
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch # 数据存储路径
#
# Path to log files:
#
path.logs: /var/log/elasticsearch # 日志路径
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200 # 端口,默认9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
插入数据
[root@ubuntu ~]# curl -XPUT 'http://localhost:9200/twitter/doc/1?pretty' -H 'Content-Type: application/json' -d '
> {
> "user": "kimchy",
> "post_date": "2009-11-15T13:12:00",
> "message": "Trying out Elasticsearch, so far so good?"
> }'
{
"_index" : "twitter",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
获取数据
[root@ubuntu ~]# curl -XGET 'http://localhost:9200/twitter/doc/1?pretty=true'
{
"_index" : "twitter",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T13:12:00",
"message" : "Trying out Elasticsearch, so far so good?"
}
}
搜索数据
[root@ubuntu ~]# curl -XGET 'http://localhost:9200/twitter/_search?q=user:kimchy&pretty=true'
{
"took" : 93,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "twitter",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "Another tweet, will it be indexed?"
}
},
{
"_index" : "twitter",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T13:12:00",
"message" : "Trying out Elasticsearch, so far so good?"
}
}
]
}
}