elasticsearch 1.7.6 学习

来源:互联网 发布:皮带轮怎样数控车编程 编辑:程序博客网 时间:2024/06/05 08:26

搜索引擎

     但凡大多数web站点,只要数据量大,都需要搜索引擎。搜索引擎一般由两个部分组成。一,索引链。二,搜索组件。

   索引链的功能是收集数据的,检索原始内容,把原始内容分析构建文档,切词。这个索引叫做倒排索引。就是对关键词出现在哪些文档中。构建索引。
   1.获取原始内容-->2.构建文档-->3.文档分析-->4.创建索引
开源软件中,能完美提供上述功能的工具叫做lucene。lucene只是提供搜索,分析,创建索引,但是第一步去获取文档,第二,没有前端页面。

   搜索组件
   功能构建查询,读取结果。ElasticSearch是一个基于Lucene的开源,分布式,Restful的全文本搜索引擎。它提供了一个分布式实时文档存储,其中每个文档的每个field均是被索引的数据。
   并且也是一个带实时分析功能的搜索引擎,能够扩展至数以百计的节点的实时处理PB级的数据。
   
      elasticsearch的官方站点。
https://www.elastic.co/cn/  
    


我本来选择的是elasticsearch 5.4 ,但是版本太新了,搭建过程全是坑,所以无奈选择了一个版本很低的1.7

安装Elasticsearch

环境三台主机
192.168.30.130   node1.example.com  centos7.2  
192.168.30.129   node2.example.com  centos7.2
192.168.30.128   node3.example.com  centos7.2

elasticsearch 版本 1.7.6
获取路径
# wget https://www.elastic.co/downloads/past-releases/elasticsearch-1-7-6/elasticsearch-1.7.6.noarch.rpm


安装java环境。
[root@node1 ~]# yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64  java-1.8.0-openjdk-headless.x86_64

查看
[root@node1 ~]# java -versionopenjdk version "1.8.0_131"OpenJDK Runtime Environment (build 1.8.0_131-b11)OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)[root@node1 ~]# ls -l /usr/bin/javalrwxrwxrwx. 1 root root 22 May 21 16:46 /usr/bin/java -> /etc/alternatives/java[root@node1 ~]# ls -l /etc/alternatives/javalrwxrwxrwx. 1 root root 73 May 21 16:46 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64/jre/bin/java

配置java家目录。
[root@node1 ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr[root@node1 ~]# . /etc/profile.d/java.sh 

安装elasticsearch
# yum -y install elasticsearch-1.7.6.noarch.rpm 

在启动之前,先改配置文件。
[root@node1 elasticsearch]# pwd
/etc/elasticsearch
[root@node1 elasticsearch]# vim elasticsearch.yml 


修改节点名称。
node.name: "node1.example.com"              每个节点改成自己的主机名

9200 是接收查询的端口
9300 是参与集群事务

启动elasticsearch。
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# systemctl start elasticsearch

查看是否启动正常
[root@node1 ~]# tcpdump  -i eno16777736 -vvnn tcp port 9300tcpdump: listening on eno16777736, link-type EN10MB (Ethernet), capture size 65535 bytes17:24:19.630572 IP (tos 0x0, ttl 64, id 60804, offset 0, flags [DF], proto TCP (6), length 170)    192.168.30.133.52499 > 192.168.30.131.9300: Flags [P.], cksum 0xda72 (correct), seq 3018421775:3018421893, ack 457870399, win 229, options [nop,nop,TS val 3124034 ecr 3134684], length 11817:24:19.630998 IP (tos 0x0, ttl 64, id 33875, offset 0, flags [DF], proto TCP (6), length 72)    192.168.30.131.9300 > 192.168.30.133.52499: Flags [P.], cksum 0xbe93 (incorrect -> 0xad67), seq 1:21, ack 118, win 227, options [nop,nop,TS val 3135689 ecr 3124034], length 20

9300端口已经开启,并且开始互相通信。
需要使用curl命令去交互,restful风格的API。
Restful:
(1) 检查集群,节点,索引等健康与否,以及获取其相应状态
(2) 管理集群,节点,索引及元数据
(3) 执行CRUD操作
(4) 执行高级操作,例如paging,filtering等

curl -X <VERB> '<PROTOCOL>://HOST:PORT/<PATH>?<QUERY_STRING>'  -d  '<BODY>'
VERB: GET PUT DELETE 等
PROTOCOL:http,https
QUERY_STRING:查询参数 pretty。易读的
BODY:请求主体

例子

[root@node1 ~]# curl -X GET 'http://node2.example.com:9200/?pretty'{  "status" : 200,  "name" : "node2.example.com",  "cluster_name" : "elasticsearch",  "version" : {    "number" : "1.7.6",    "build_hash" : "c730b59357f8ebc555286794dcd90b3411f517c9",    "build_timestamp" : "2016-11-18T15:21:16Z",    "build_snapshot" : false,    "lucene_version" : "4.10.4"  },  "tagline" : "You Know, for Search"}

使用_cat
[root@node1 ~]# curl -X GET 'http://node2.example.com:9200/_cat'=^.^=/_cat/allocation/_cat/shards/_cat/shards/{index}/_cat/master/_cat/nodes/_cat/indices/_cat/indices/{index}/_cat/segments/_cat/segments/{index}/_cat/count/_cat/count/{index}/_cat/recovery/_cat/recovery/{index}/_cat/health/_cat/pending_tasks/_cat/aliases/_cat/aliases/{alias}/_cat/thread_pool/_cat/plugins/_cat/fielddata/_cat/fielddata/{fields}

查询集群
[root@node1 ~]# curl -X GET 'http://node2.example.com:9200/_cat/nodes?v'host              ip             heap.percent ram.percent load node.role master name              node1.example.com 192.168.30.131            4          40 0.02 d         *      node1.example.com node2.example.com 192.168.30.132            4          40 0.01 d         m      node2.example.com node3.example.com 192.168.30.133            4          42 0.01 d         m      node3.example.com 

健康查询

[root@node1 ~]# curl -X GET 'http://node2:9200/_cat/health?v'epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks 1495360000 17:46:40  elasticsearch green           3         3      0   0    0    0        0             0 


集群健康查询

[root@node2 ~]# curl  -X GET 'http://node2:9200/_cluster/health?pretty'{  "cluster_name" : "elasticsearch",  "status" : "green",  "timed_out" : false,  "number_of_nodes" : 3,  "number_of_data_nodes" : 3,  "active_primary_shards" : 0,  "active_shards" : 0,  "relocating_shards" : 0,  "initializing_shards" : 0,  "unassigned_shards" : 0,  "delayed_unassigned_shards" : 0,  "number_of_pending_tasks" : 0,  "number_of_in_flight_fetch" : 0}

详细的内容可以去官网去查看。

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html



ES的使用

引索组件:Lucene

搜索组件:ES

数据获取组件:solr,Nutch,Grub,Apeture


ES:索引是文档的容器

   索引(index),类型(type),文件(document),映射(mapping)

Plugins可以扩展ES的功能。

比如添加自定义映射类型,本地脚本,自定义发现方式。

安装:

  直接将插件放置于plugins目录就好。或者plugin命令去安装。

[root@node2 ~]# /usr/share/elasticsearch/bin/plugin -l
Installed plugins:
    - No plugin detected in /usr/share/elasticsearch/plugins

  

安装插件

首先介绍一些常见插件:

[head插件]

介绍:

head插件是ES的一个可视化插件,head插件用来浏览,与ES数据进行交互的Web前端展示插件,是一个用来监视ES状态的客户端插件。

安装:

[root@node1 bin]# ./plugin install mobz/elasticsearch-head

测试:



[marvel插件]

介绍:

主要功能是监控,也可以用来当console来使用,它包含一个叫做Sense的交互式控制平台。使用户方便的通过浏览器去交互。

安装:

[root@node1 elasticsearch]# ./bin/plugin -i elasticsearch/marvel/latest


[bigdesk插件] 

介绍:
web页面实时显示一些图标和统计信息
安装:
[root@node1 elasticsearch]# ./bin/plugin -install lukas-vlcek/bigdesk/2.4.0


[elasticsearch-kopf插件]
介绍:
提供在集群上执行常见任务的简单方法。
安装:
[root@node1 elasticsearch]# bin/plugin install lmenezes/elasticsearch-kopf/

现在查看已经装好的插件
[root@node1 elasticsearch]# ./bin/plugin -lInstalled plugins:    - head    - license    - bigdesk    - kopf    - marvel

全部安装完成之后,重启elasticsearch
systemctl restart elasticsearch
输入url访问。




创建索引
方法:
[root@node1 elasticsearch]# curl -XPUT 'localhost:9200/students/class/1?pretty' -d '> {>   "first_name":"Rong",>   "last_name":"Huang",>   "age":25,>   "gender":"Male"> }'{  "_index" : "students",  "_type" : "class",  "_id" : "1",  "_version" : 1,  "created" : true}[root@node1 elasticsearch]# curl -XPUT 'localhost:9200/students/class/2?pretty' -d '{  "first_name":"Jing",  "last_name":"Guo",  "age":26,  "gender":"Female"}'{  "_index" : "students",  "_type" : "class",  "_id" : "2",  "_version" : 1,  "created" : true}


获取文档:PUT改成GET
更新:PUT覆盖原有文档 POST
删除:DELETE

使用_search的API来实现查询
单索引
/INDEX_NAME/_search
[root@node1 elasticsearch]# curl -XGET 'localhost:9200/students/_search?pretty'
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
。。。

多索引
/INDEX_NAME1,INDEX_NAME2/_saerch

或者
[root@node1 elasticsearch]# curl -XGET 'localhost:9200/_search?q='Guo'&pretty'{  "took" : 10,  "timed_out" : false,  "_shards" : {    "total" : 7,    "successful" : 7,    "failed" : 0  },  "hits" : {    "total" : 1,    "max_score" : 0.15342641,    "hits" : [ {      "_index" : "students",      "_type" : "class",      "_id" : "2",      "_score" : 0.15342641,      "_source":{  "first_name":"Jing",  "last_name":"Guo",  "age":26,  "gender":"Female"}    } ]  }}


# curl -XGET 'localhost:9200/_search?q=last_name='Guo%20Jing'&pretty'         %20 是空格的意思






原创粉丝点击