大数据015——Elasticsearch基础

1. Elasticsearch 简介

Elasticsearch是一个基于Lucene的实时的分布式搜索和分析 引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。基于RESTful接口。

1.1 Lucene与ES关系

1)Lucene只是一个库。想要使用它,你必须使用Java来作为开发语言并将其直接集成到你的应用中,更糟糕的是,Lucene非常复杂,你需要深入了解检索的相关知识来理解它是如何工作的。

2)Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTfulAPI来隐藏Lucene的复杂性,从而让全文搜索变得简单。

1.2 Elasticsearch与solr对比

  • 当单纯的对已有数据进行搜索时,Solr更快。
  • 当实时建立索引时, Solr会产生io阻塞,查询性能较差, Elasticsearch具有明显的优势。
  • 随着数据量的增加,Solr的搜索效率会变得更低,而Elasticsearch却没有明显的变化。

1.4 Elasticsearch与关系型数据库对比

ElasticSearch与关系型数据库的相似:

database(数据库)index(索引库)
table(表)type(类型)
row(行)document(文档)
column(列)field(字段)
  1. 一个ES集群可以包含多个索引(数据库),每个索引又包含了很多类型(表),类型中包含了很多文档(行),每个文档又包含了很多字段(列)。
  2. 传统数据库为特定列增加一个索引,例如B-Tree索引来加速检索。Elasticsearch和Lucene使用一种叫做倒排索引(inverted index)的数据结构来达到相同目的。
  3. 倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inverted index)。

2. Elasticsearch部署与启动

1)、自行从 elastic 的官网 elastic.co/downloads/elasticsearch 获取最新版本的 Elasticsearch。

2)、启动zookeeper集群 zkServer.sh start

3)、解压tar包,在根目录的bin目录下执行启动命令:

标准方式: ./elasticsearch 控制台占用命令行,Ctrl+C退出时程序终止;

规范方式: ./elasticsearch -d 控制台后台启动,不占用命令行;

通用的方式:./elasticsearch & 控制台占用命令行,Ctrl+C退出时程序依旧运行;

这里应该会报错,提示不能使用root用户启动该服务:

[root@node02 bin]# ./elasticsearch
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
	at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:94)
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:160)
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286)
	at org.elasticsearch.bootstrap.Elast

4)、然后你可以新建一个用户 es,并设置 elasticsearch 解压后的所有文件权限为777,在使用 su 命令切换用户:

useradd es
passwd es
chmod -R 777 elasticsearch
su es

再次启动:

[es@node01 bin]$ ./elasticsearch
...
[2019-01-25 22:00:25,182][WARN ][discovery                ] [node01] waited for 30s and no initial state was set by the discovery
[2019-01-25 22:00:25,192][INFO ][http                     ] [node01] publish_address {192.168.150.101:9200}, bound_addresses {192.168.150.101:9200}
[2019-01-25 22:00:25,192][INFO ][node                     ] [node01] started

5)、访问http://node01:9200,浏览器出现 Json 串:

// 20190126190803
// http://node01:9200/

{
  "name": "node01",
  "cluster_name": "myes",
  "cluster_uuid": "-tg11ItrTSC5TevAEznS2g",
  "version": {
    "number": "2.4.5",
    "build_hash": "c849dd13904f53e63e88efc33b2ceeda0b6a1276",
    "build_timestamp": "2017-04-24T16:18:17Z",
    "build_snapshot": false,
    "lucene_version": "5.5.4"
  },
  "tagline": "You Know, for Search"
}

3. REST 简介

REST 全称是 Resource Representational State Transfer,通俗来讲就是:资源在网络中以某种表现形式进行状态转移。分解开来:

  • Resource:资源,即数据。比如 news,friends等;
  • Representational:某种表现形式,比如用JSON,XML,JPEG等;
  • State Transfer:状态变化。通过HTTP动词实现。

它一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

3.1 Rest操作

REST的操作分为以下几种:

– GET:获取对象的当前状态;

– PUT:改变对象的状态;

– POST:创建对象;

– DELETE:删除对象;

– HEAD:获取头信息。

3.2 ES内置的REST接口

/_aliases获取或者操作索引的别名
/index/_search搜索指定索引下的数据
/index/查看指定所引导详细信息
/index/type/创建或者操作 类型(表)
/index/_mapping创建或者操作 mapping
/index/_settings创建或者操作 设置(number_of_shards是不可更改的)
/index/_open打开指定被关闭的索引
/index/_close关闭指定索引
/index/_refresh刷新索引-不保证写入,使得新增内容对搜索可见
/index/_flush刷新索引-会出发lucene提交

4. CURL语句

4.1 索引库的创建与删除

创建索引库:

curl -XPUT http://node01:9200/mydoc/

PUT/POST都可以

删除索引库:

curl -XDELETE http://node01:9200/mydoc/

4.2 创建document

如在 mydoc 索引库下创建索引 employee ,并制定ID为1:

[root@node01 ~]# curl -XPOST http://node01:9200/mydoc/employee/1 -d '{ 
> "first_name" : "john", 
> "last_name" : "smith", 
> "age" : 25, 
> "about" : "I love to go rock climbing"
> }'
{"_index":"mydoc","_type":"employee","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
[root@node01 ~]# 

4.3 更新document

[root@node01 ~]# curl -XPUT http://node01:9200/mydoc/employee/1 -d '
> {
>  "first_name" : "god bin",
>  "last_name" : "pang",
>  "about" : "I love to go rock play",
>  "address": "shanghai"
> }'
{"_index":"mydoc","_type":"employee","_id":"1","_version":2,"_shards":{"total":2,"successful":2,"failed":0},"created":false}
[root@node01 ~]# 

更新单条索引时可以新添加字段,原有字段的数据类型无法修改如:不能为’age’字段设置字符串值。

4.4 普通查询索引

1)、根据 id 查询(参数pretty表示es可以得到易于识别的json结果):

curl -XGET http://node01:9200/mydoc/employee/1?pretty:

[root@node01 ~]# curl -XGET http://node01:9200/mydoc/employee/1?pretty
{
  "_index" : "mydoc",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "first_name" : "god bin",
    "last_name" : "pang",
    "about" : "I love to go rock play",
    "address" : "shanghai"
  }
}

2)、curl后添加-i 参数,这样你就能得到反馈头文件

curl -i XGET http://node01:9200/mydoc/employee/1?pretty

3)、如果只需要source的数据 :

curl -XGET http://node01:9200/mydoc/employee/1?pretty_source?pretty

4)、检索文档中的一部分,如果只需要显示指定字段:

curl -XGET http://node01:9200/mydoc/employee/1?_source=name,age

5)、查询所有

curl -XGET http://node01:9200/mydoc/employee/1?pretty

6)、根据条件进行查询

curl -XGET http://node01:9200/mydoc/employee/_search?q=last_name:smith

4.5 DSL查询

DSL查询 Domain Specific Language ——领域特定语言 ,例如:

1)、匹配查询

curl -XGET http://node01:9200/mydoc/employee/_search?pretty -d '{
"query":
{"match":
    {"last_name":"smith"}
    }
}'

2)、对多个field发起查询:multi_match

curl -XGET http://node01:9200/mydoc/employee/_search?pretty -d '
{
 "query":
  {"multi_match":
   {
    "query":"bin",
    "fields":["last_name","first_name"]
   }
  }
}'

3)、复合查询,must,must_not, should

must: AND

must_not:NOT

should:OR

4)、范围查询

curl -XGET http://node01:9200/mydoc/employee/_search -d '
{
 "query":
  {"bool" :
   {
   "must" :
    {"term" : 
     { "first_name" : "bin" }
    }
   ,
   "must" : 
    {"range":
     {"age" : { "from" : 30, "to" : 40 }
    }
   }
   }
  }
}'

4.6 删除 document

curl -XDELETE http://node01:9200/shsxt/employee/1?pretty

  • 如果文档存在,es会返回200 ok的状态码,found属性值为 true,_version属性的值+1 ;
  • found属性值为false,但是_version属性的值依然会+1,这个就是内部管理的一部分,它保证了我们在多个节点间的不同操作的顺序都被正确标记了 ;
  • 注意:删除一个文档也不会立即生效,它只是被标记成已删除。 Elasticsearch将会在你之后添加更多索引的时候才会在后台进行删除内容的清理。

5. Elasticsearch 插件安装

5.1 head 插件

head 是在学习和使用Elasticsearch的过程中,必不可少需要通过可视化工具查看es的运行状态以及数据。

[root@node01 bin]# ./plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ............................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /home/elasticsearch-2.4.5/plugins/head

安装后重新启动elasticsearch,访问http://node01:9200/_plugin/head:

000

5.2 Kibana & Marvel

Kibana 是一个基于浏览器页面的ES前端展示工具,是为ES提供日志分析的web接口,可用它对日志进行高效的搜索、可视化、分析等操作。Marvel插件可以帮助使用者监控elasticsearch的运行状态,不过这个插件需要license。

1)、自行下载 kibanna 安装包,解压安装,然后修改配置文件 config/kibana.yml 的 elasticsearch.url 属性为es集群中的一个节点即可;

2)、安装 Marvel:

Es_home/bin/plugin install license

[root@node01 bin]# ./plugin install license
-> Installing license...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.4.5/license-2.4.5.zip ...
ERROR: failed to download out of all possible locations..., use --verbose to get detailed information
[root@node01 bin]# ./plugin install license
-> Installing license...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.4.5/license-2.4.5.zip ...
Downloading .......................DONE
Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/license/2.4.5/license-2.4.5.zip checksums if available ...
Downloading .DONE
Installed license into /home/elasticsearch-2.4.5/plugins/license

Es_home/bin/plugin install marvel-agent

[root@node01 bin]# ./plugin install marvel-agent
-> Installing marvel-agent...
Trying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/marvel-agent/2.4.5/marvel-agent-2.4.5.zip ...
Downloading .............................DONE
Verifying https://download.elastic.co/elasticsearch/release/org/elasticsearch/plugin/marvel-agent/2.4.5/marvel-agent-2.4.5.zip checksums if available ...
Downloading .DONE
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.lang.RuntimePermission setFactory
* javax.net.ssl.SSLPermission setHostnameVerifier
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
Installed marvel-agent into /home/elasticsearch-2.4.5/plugins/marvel-agent

Kibana_home/bin/kibana plugin --install elasticsearch/marvel/latest

[root@node01 bin]# ./kibana plugin  --install elasticsearch/marvel/latest
Installing marvel
Attempting to transfer from https://download.elastic.co/elasticsearch/marvel/marvel-latest.tar.gz
Transferring 2400332 bytes....................
Transfer complete
Extracting plugin archive
Extraction complete
Optimizing and caching browser bundles...
Plugin installation complete

3)、启动 kinaba、并重新启动 elasticsearch;

[root@node01 bin]# ./kibana 
  log   [10:24:22.261] [info][status][plugin:kibana@1.0.0] Status changed from uninitialized to green - Ready
  log   [10:24:22.289] [info][status][plugin:elasticsearch@1.0.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
......
  log   [10:24:31.482] [info][status][plugin:elasticsearch@1.0.0] Status changed from yellow to green - Kibana index ready

4)、浏览器访问 http://node01:5601/app/marvel

0001

5.3 分词器集成

5.1 下载

从地址 https://github.com/medcl/elasticsearch-analysis-ik 下载 elasticsearch 中文分词器;

5.2 安装插件

在安装好的elasticsearch中在plugins目录下新建ik目录,将此zip包拷贝到ik目录下,将权限修改为elasticsearch启动用户的权限,通过unzip命令解压缩:

[root@node01 plugins]# mkdir ik
[root@node01 plugins]# chmd -R 777 ik
[root@node01 plugins]# mv ../elasticsearch-analysis-ik-1.10.5.zip ik
# 未使用过 unzip 命令的需要安装unzip
[root@node01 plugins]# yum -y install unzip
[root@node01 plugins]# unzip elasticsearch-analysis-ik-1.10.5.zip

安装完成,需要为每台 es 节点完成相同操作;

5.3 重启 es 集群
5.4 分词器测试

1)、创建索引库

curl -XPUT http://node01:9200/ik

2)、设置 mapping

curl -XPOST http://node01:9200/ik/ikType/_mapping -d'{
 "properties": {
			 "content": {
				   "type": "string",
                   "index":"analyzed",
                   "analyzer": "ik_max_word",
                   "search_analyzer": "ik_max_word"
           }
      }
}'

3)、插入数据

curl -XPOST http://node01:9200/ik/ikType/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}'

curl -XPOST http://node01:9200/ik/ikType/2 -d'
{"content":"公安部:各地校车将享最高路权"}'

curl -XPOST http://node01:9200/ik/ikType/3 -d
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'

curl -XPOST http://node01:9200/ik/ikType/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'

4)、查询

curl -XGET http://node01:9200/ik/ikType/_search?pretty  -d'{
   "query" : { "term" : { "content" : "中国" }}
 }'

返回结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.26516503,
        "hits": [
        {
            "_index": "ik",
            "_type": "ikType",
            "_id": "3",
            "_score": 0.26516503,
            "_source": {
            "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
        }
        }
        ,
        {
            "_index": "ik",
            "_type": "ikType",
            "_id": "4",
            "_score": 0.1875,
            "_source": {
            "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
        }
        }
        ]
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值