ElasticSearch(一)集群入门

基本概念

Node 与 Cluster

Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。
单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

Index

Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。
所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。

下面的命令可以查看当前节点的所有 Index。

$ curl -X GET 'http://localhost:9200/_cat/indices?v'
Document

Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。

Document 使用 JSON 格式表示,下面是一个例子。

{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}

同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。


二、下载与安装

Elasticsearch 依赖 java,在安装 ES 之前首先要配好 java,这个默认我们的电 脑已经完成。Elasticsearch要求jdk最低版本为1.7。
首先从 elasticsearch官网下载安装包,我们是 linux 系统,下载 tar 包比较方便。

下载完成之后解压tar文件:

tar -zxvf elasticsearch-2.3.3.tar.gz 

三、运行ElasticSearch

启动ElasticSearch命令:

./elasticsearch-2.3.3/bin/elasticsearch

默认情况下, Elasticsearch 使用 9200 来提供对其 REST API 的访问。
访问http://127.0.0.1:9200/,浏览器会输出如下信息:

{
  "name" : "Obliterator",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.3",
    "build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
    "build_timestamp" : "2016-05-17T15:40:04Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

如果想通过服务器ip访问es,打开elasticsearch-2.3.3/config/elasticsearch.yml,找到Network部分:

# ---------------------------------- 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
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html>
#

把network.host: 192.168.0.1前的注释去掉并修改为network.host: 0.0.0.0
重启es上面返回的信息中 http 部分也会变成真实本机地址,可以通过本机真实ip:9200 访问es。


四、关闭ElasticSearch

需要对ES节点进行重新启动或正常关机的时候,有三种方法可以关闭ES:


五、插件安装

4.1安装head 插件

安装命令:

./elasticsearch-2.3.3/bin/plugin install mobz/elasticsearch-head

安装好后,在浏览器输入地址:http://localhost:9200/_plugin/head/ 即可调用 head 插件 查看集群状态、节点信息、做查询等等。

4.2安装IK分词器

(a).首先使用git clone命令下载IK分词器源码

git clone https://github.com/medcl/elasticsearch-analysis-ik.git

也可以直接访问github地址(https://github.com/medcl/elasticsearch-analysis-ik)点击右侧Clone or download按钮,然后Download ZIP直接下载.

(b.)解压下载的elasticsearch-analysis-ik-master.zip.

unzip elasticsearch-analysis-ik-master.zip

(c.)使用maven打包
确保系统已经安装maven,使用mvn -version命令查看是否已经安装maven.如果没有安装,可以根据系统选择安装方法,比如mac OS系统可以使用brew install maven命令完成安装.
进入ik分词器的下载目录,运行命令:

mvn package

打包完成以后可以看到根目录下多出一个target文件夹.
(d.) 配置Ik插件
在elasticsearch-2.3.3/plugins/目录下新建名为ik的文件夹.把elasticsearch-analysis-ik-master/target/releases
/elasticsearch-analysis-ik-1.9.3.zip解压,把解压后的所有文件拷贝到elasticsearch-2.3.3/plugins/ik/目录下.
重新启动es,如果配置正确,不会有异常信息输出。
(e.)ik 分词测试
1.首先创建一个索引用于测试:

curl -XPUT localhost:9200/index

2.为索引index创建mapping:

curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik"
        },
        "properties": {
            "content": {
                "type" : "string",
                "boost" : 8.0,
                "term_vector" : "with_positions_offsets",
                "analyzer" : "ik",
                "include_in_all" : true
            }
        }
    }
}'

3.测试:

curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d 

{
"text":"中国有13亿人口"
}'

显示结果如下:

{
  "tokens" : [ {
    "token" : "中国",
    "start_offset" : 0,
    "end_offset" : 2,
    "type" : "CN_WORD",
    "position" : 0
  }, {
    "token" : "国有",
    "start_offset" : 1,
    "end_offset" : 3,
    "type" : "CN_WORD",
    "position" : 1
  }, {
    "token" : "13",
    "start_offset" : 3,
    "end_offset" : 5,
    "type" : "ARABIC",
    "position" : 2
  }, {
    "token" : "亿",
    "start_offset" : 5,
    "end_offset" : 6,
    "type" : "CN_WORD",
    "position" : 3
  }, {
    "token" : "人口",
    "start_offset" : 6,
    "end_offset" : 8,
    "type" : "CN_WORD",
    "position" : 4
  } ]
}

六、文档的CRUD

6.1 创建文档

以博客内容管理为例,索引名为blog,类型为article,新加一个文档:

curl -XPUT http://localhost:9200/blog/article/1 -d '
{
      "id": "1",
      "title": "New version of Elasticsearch released!",
      "content": "Version 1.0 released today!",
      "priority": 10,
      "tags": ["announce", "elasticsearch", "release"]
}'
6.2 检索文档
http://localhost:9200/blog/article/1?pretty
6.3 更新文档
curl -XPOST http://localhost:9200/blog/article/1/_update -d '{
  "script": "ctx._source.content = \"new content\""
}'
6.4 删除文档
curl -XDELETE http://localhost:9200/blog/article/1 

七、相关概念

7.1 节点与集群

ElasticSearch是一个分布式全文搜索引擎,既可以做为一个独立的搜索服务器工作,也可以使用多台服务器同时运行,这样就构成了一个集群(cluster),集群的每一个服务器称为一个节点(node).

7.2 分片

当数据量比较大的时候,受RAM、硬盘容量的限制,同时一个节点的计算能力有限。可以将数据切分,每部分是一个单独的lucene索引,成为分片(shard)。每个分片可以被存储在集群的不同节点上。当需要查询由多个分片构成的索引时,ElasticSearch将查询发送到每个相关的分片,之后将查询结果合并。过程对应用透明,无须知道分片的存在。

7.3 副本

副本是对原始分片的一个精确拷贝,原始分片成为主分片。对索引的所有操作都直接作用在主分片上,每个主分片可以有零个或多个副分片。主分片丢失,集群可以将一个副分片提升为主的新分片。

猜你喜欢

转载自blog.csdn.net/lwl2014100338/article/details/81046469