您的位置:首页 > 其它

elasticsearch使用操作部分

2016-07-15 18:09 344 查看
本片文章记录了elasticsearch概念、特点、集群、插件、API使用方法。

1.elasticsearch的概念及特点。
概念:elasticsearch是一个基于lucene的搜索服务器。lucene是全文搜索的一个框架。
特点:

  - 分布式,可扩展,高可用。
  - 能够实时搜索分析数据。
  - 复杂的RESTful API。
总结:是一个采用RESTful API标准,实现分布式、可扩展以及高可用的实时数据存储分析的全文搜索工具。

2.elasticsearch涉及的相关概念。(关系非关系对比)
相关概念:
  - Node: 装有一个elasticsearch服务器的节点。
  - Cluster: 有一个或多个Node组成的集群,共同工作。
  - Document: 一个可被搜素的基础信息单元。
  - Index: 拥有相似特征的文档的集合。
  - Type: 一个索引中可以定义一种或多种类型。
  - Filed: 是elasticsearch的最小单位,相当于数据的某一列。
  - Shards: 索引分成若干份,每一份就是一个shard。默认5份。
  - Replics: 是索引的一份或者多份拷贝。
关系型与非关系型对比:
database ----> Index
table ----> Type
row ----> Document
column ----> Filed
内置字段和字段类型:
  - 内置字段:_uid,_id,_type,_source,_all,_analyzer,_boost,_parent,_routing,_index,_size,_timestamp,_ttl
  - 字段类型:string,integer/long,float/double,boolean,null,date

3.elasticsearch架构详解。



4.什么是倒排索引。
概念:倒排索引(英语:Inverted index),也常被称为反向索引、置入档案或反向档案。是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。

正向索引和倒排索引对比:
内容 --正向--> 关键字/词
内容 <--倒排-- 关键字/词

5.RESTful API以及curl。
概念:RESTful 表现层状态转化。
  - 表现层:图片,文字,视频等
  - 转化后:资源
API:应用程序接口。
RESTful API:一种资源操作的应用程序接口。
curl:一个利用URL语法在命令行下工作的文件传输工具,支持文件上传和下载。
curl常用参数:
  - I 获取头部信息
  - v 获取握手过程
  - o 保存文件
curl -o baidu.html www.baidu.com
curl -I -v www.baidu.com

6.elasticsearch单机/集群安装配置。
单机安装(node1上安装elasticsearch)
①.安装jdk1.8_65
[root@node1 ~]# rpm -ivh jdk-8u65-linux-x64.rpm
②.下载安装elasticsearch,并设置开启自启动。
[root@node1 ~]# wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.3.4/elasticsearch-2.3.4.rpm [root@node1 ~]# rpm -ivh elasticsearch-2.3.4.rpm
[root@node1 ~]# chkconfig --add elasticsearch
③.修改配置文件,并启动服务。
[root@node1 ~]# grep -Ev "^#|^$" /etc/elasticsearch/elasticsearch.yml
network.host: 192.168.3.1
http.port: 9200
[root@node1 ~]# service elasticsearch start
④.浏览器访问IP:9200查看结果。 http://192.168.3.1:9200
集群安装(node1|node2安装elasticsearch)
①.node2上安装,安装同单机node1一样。
②.修改node1和node2配置文件。
node1:
[root@node1 ~]# grep -Ev "^#|^$" /etc/elasticsearch/elasticsearch.yml
cluster.name: elk-xkops
node.name: node-1
network.host: 192.168.3.1
http.port: 9200
node2:
[root@node2 ~]# grep -Ev "^#|^$" /etc/elasticsearch/elasticsearch.yml
cluster.name: elk-xkops
node.name: node-2
network.host: 192.168.3.2
discovery.zen.ping.unicast.hosts: ["192.168.3.1"]

*注释:关于单播和多播,为了防止节点的任意加入,官方建议集群关闭多播,使用单播集群。单播列表只要能保证与集群中任一节点能通信即可,不需要列出所有集群节点。

7.elasticsearch常用插件。(head,kopf,bigdesk)
在线安装:
①.安装head插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
②.安装bigdesk插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install hlstudio/bigdesk
③.安装kopf插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
离线安装:
①.安装head插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install file:/elk/soft/elasticsearch-head-master.zip
②.安装bigdesk插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install file:/elk/soft/bigdesk-master.zip
③.安装kopf插件。
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install file:/elk/soft/elasticsearch-kopf-master.zip
浏览器端访问插件: http://192.168.3.1:9200/_plugin/head http://192.168.3.1:9200/_plugin/bigdesk http://192.168.3.1:9200/_plugin/kopf
8.使用RESTful API操作elasticsearch。
①.索引初始化配置:创建、获取、删除。
初始化配置school索引:

curl -XPUT 'http://192.168.3.1:9200/school/' -d '{
"settings":{
"index":{
"number_of_shards": 5,
"number_of_replicas": 1
}
}
}'


获取索引配置信息:

curl -XGET 'http://192.168.3.1:9200/school/_settings/'
curl -XGET 'http://192.168.3.1:9200/school,index_name/_settings/'
curl -XGET 'http://192.168.3.1:9200/_all/_settings/'


删除索引配置信息:

curl -XDELETE 'http://192.168.3.1:9200/school/'
curl -XDELETE 'http://192.168.3.1:9200/school,index_name/'
curl -XDELETE 'http://192.168.3.1:9200/_all/'


②.创建、更新、删除索引文档。
创建索引文档:

curl -XPUT 'http://192.168.3.1:9200/school/students/1' -d '{
"title": "devops",
"name":{
"first": "guzhang",
"last": "wu"
},
"age": 25
}'
*注释:school为索引名称,students为文档类型,1为文档ID(可省略)。


获取索引文档(通过source指定字段获取信息):

curl -XGET 'http://192.168.3.1:9200/school/students/1'
curl -XGET 'http://192.168.3.1:9200/school/students/1?_source=name,age'


更新索引文档:

  - 覆盖 意即重新创建
  - _update 使用API更新

curl -XPOST 'http://192.168.3.1:9200/school/students/1/_update' -d '{
"doc":{
"age": 30
}
}'


删除索引文档:

curl -XDELETE 'http://192.168.3.1:9200/school/students/1'


使用bulk批量创建文档:
官方元数据: https://www.elastic.co/guide/en/kibana/3.0/snippets/shakespeare.json https://raw.githubusercontent.com/bly2k/files/master/accounts.zip
导入官方元数据:
[root@node1 ~]# curl -XPOST 'http://192.168.3.1:9200/shakesapear/act/_bulk?pretty' --data-binary @shakespeare.json
[root@node1 ~]# curl -XPOST 'http://192.168.3.1:9200' --data-binary @account.json

使用mget批量获取文档:
mget可以快速的同时检索多个文档。mget API参数是一个docs数组,数组的每个节点定义一个文档的——index、_type、_id元数据。

获取多个索引下文档信息:

curl -XGET 'http://192.168.3.1:9200/_mget' -d '{
"docs":[
{
"_index": "bank",
"_type:": "account",
"_id": 1
},
{
"_index": "bank",
"_type:": "account",
"_id": 2
},
{
"_index": "shakespeare",
"_type:": "act",
"_id": 1
}
]
}'


获取相同索引下多个文档信息,并指定字段获取(_source)。

curl -XGET 'http://192.168.3.1:9200/bank/account/_mget' -d '{
"docs":[
{
"_id": 1,
"_source": "age"
},
{
"_id": 2,
"_source": "firstname"
}
]
}'


mapping:
映射:创建索引的时候,可以预先定义字段的类型及相关属性。
作用:这样会让索引建立得更加的细致和完善。
分类:静态映射和动态映射。

动态映射:自动根据数据进行相应的映射。
静态映射:自定义字段映射数据类型。

*提示:可以修改如下文件,共用mapping。
/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-output-elasticsearch-2.7.1-java/lib/logstash/outputs/elasticsearch/elasticsearch-template.json
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: