Elasticsearch 文档从安装到使用

一 概述

1.1 什么是Elasticsearch?

Elasticsearch,基于lucene,隐藏复杂性,提供简单易用的restful api接口、java api接口(还有其他语言的api接口)。

关于elasticsearch的一个传说,有一个程序员失业了,陪着自己老婆去英国伦敦学习厨师课程。程序员在失业期间想给老婆写一个菜谱搜索引擎,觉得lucene实在太复杂了,就开发了一个封装了lucene的开源项目,compass。后来程序员找到了工作,是做分布式的高性能项目的,觉得compass不够,就写了elasticsearch,让lucene变成分布式的系统。

Elasticsearch是一个实时分布式搜索和分析引擎。它用于全文搜索、结构化搜索、分析。

全文检索:将非结构化数据中的一部分信息提取出来,重新组织,使其变得有一定结构,然后对此有一定结构的数据进行搜索,从而达到搜索相对较快的目的。

结构化检索:我想搜索商品分类为日化用品的商品都有哪些,select * from products where category_id='日化用品'

数据分析:电商网站,最近7天牙膏这种商品销量排名前10的商家有哪些;新闻网站,最近1个月访问量排名前3的新闻版块是哪些

1.2 ES适用场景

1)维基百科,类似百度百科,牙膏,牙膏的维基百科,全文检索,高亮,搜索推荐。

2)The Guardian(国外新闻网站),类似搜狐新闻,用户行为日志(点击,浏览,收藏,评论)+ 社交网络数据(对某某新闻的相关看法),数据分析,给到每篇新闻文章的作者,让他知道他的文章的公众反馈(好,坏,热门,垃圾,鄙视,崇拜)。

3)Stack Overflow(国外的程序异常讨论论坛),IT问题,程序的报错,提交上去,有人会跟你讨论和回答,全文检索,搜索相关问题和答案,程序报错了,就会将报错信息粘贴到里面去,搜索有没有对应的答案

4)GitHub(开源代码管理),搜索上千亿行代码。

5)国内:站内搜索(电商,招聘,门户,等等),IT系统搜索(OA,CRM,ERP,等等),数据分析(ES热门的一个使用场景)。

1.3 ES 特点

1)可以作为一个大型分布式集群(数百台服务器)技术,处理PB级数据,服务大公司;也可以运行在单机上,服务小公司

2)Elasticsearch不是什么新技术,主要是将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的ES;lucene(全文检索),商用的数据分析软件(也是有的),分布式数据库(mycat)

3)对用户而言,是开箱即用的,非常简单,作为中小型的应用,直接3分钟部署一下ES,就可以作为生产环境的系统来使用了,数据量不大,操作不是太复杂

4)数据库的功能面对很多领域是不够用的(事务,还有各种联机事务型的操作);特殊的功能,比如全文检索,同义词处理,相关度排名,复杂数据分析,海量数据的近实时处理;Elasticsearch作为传统数据库的一个补充,提供了数据库所不能提供的很多功能

1.4 ES的核心概念

1..4.1 近实时

近实时,两个意思,从写入数据到数据可以被搜索到有一个小延迟(大概1秒);基于es执行搜索和分析可以达到秒级。

1.4.2  Cluster(集群)

集群包含多个节点,每个节点属于哪个集群是通过一个配置(集群名称,默认是elasticsearch)来决定的,对于中小型应用来说,刚开始一个集群就一个节点很正常

1.4.3 Node(节点)

集群中的一个节点,节点也有一个名称(默认是随机分配的),节点名称很重要(在执行运维管理操作的时候),默认节点会去加入一个名称为“elasticsearch”的集群,如果直接启动一堆节点,那么它们会自动组成一个elasticsearch集群,当然一个节点也可以组成一个elasticsearch集群。

1.4.4 Index(索引-数据库)

索引包含一堆有相似结构的文档数据,比如可以有一个客户索引,商品分类索引,订单索引,索引有一个名称。一个index包含很多document,一个index就代表了一类类似的或者相同的document。比如说建立一个product index,商品索引,里面可能就存放了所有的商品数据,所有的商品document。

1.4.5 Type(类型——表)

每个索引里都可以有一个或多个type,type是index中的一个逻辑数据分类,一个type下的document,都有相同的field,比如博客系统,有一个索引,可以定义用户数据type,博客数据type,评论数据type。

商品index,里面存放了所有的商品数据,商品document

但是商品分很多种类,每个种类的document的field可能不太一样,比如说电器商品,可能还包含一些诸如售后时间范围这样的特殊field;生鲜商品,还包含一些诸如生鲜保质期之类的特殊field

type,日化商品type,电器商品type,生鲜商品type

日化商品type:product_id,product_name,product_desc,category_id,category_name

电器商品type:product_id,product_name,product_desc,category_id,category_name,service_period

生鲜商品type:product_id,product_name,product_desc,category_id,category_name,eat_period

每一个type里面,都会包含一堆document

{

  "product_id": "1",

  "product_name": "长虹电视机",

  "product_desc": "4k高清",

  "category_id": "3",

  "category_name": "电器",

  "service_period": "1年"

}

1.4.6 Document(文档——行)

文档是es中的最小数据单元,一个document可以是一条客户数据,一条商品分类数据,一条订单数据,通常用JSON数据结构表示,每个index下的type中,都可以去存储多个document。

1.4.7 Field(字段—列)

Field是Elasticsearch的最小单位。一个document里面有多个field,每个field就是一个数据字段。

product document

{

  "product_id": "1",

  "product_name": "高露洁牙膏",

  "product_desc": "高效美白",

  "category_id": "2",

  "category_name": "日化用品"

}

1.7.8 mapping(映射——约束)

数据如何存放到索引对象上,需要有一个映射配置,包括:数据类型、是否存储、是否分词等。

这样就创建了一个名为blog的Index。Type不用单独创建,在创建Mapping 时指定就可以。Mapping用来定义Document中每个字段的类型,即所使用的 analyzer、是否索引等属性,非常关键等。创建Mapping 的代码示例如下:

client.indices.putMapping({

    index : 'blog',

    type : 'article',

    body : {

        article: {

            properties: {

                id: {

                    type: 'string',

                    analyzer: 'ik',

                    store: 'yes',

                },

                title: {

                    type: 'string',

                    analyzer: 'ik',

                    store: 'no',

                },

                content: {

                    type: 'string',

                    analyzer: 'ik',

                    store: 'yes',

                }

            }

        }

    }

});

 

1.7.9 ES与数据库的对比

 

关系型数据库(比如Mysql)

非关系型数据库(Elasticsearch)

数据库Database

索引Index

表Table

类型Type

数据行Row

文档Document

数据列Column

字段Field

约束 Schema

映射Mapping

二 快速入门

2.1 安装包下载

(1)Elasticsearch官网: https://www.elastic.co/products/elasticsearch

选择5.2.2 版本下载,下载完成后,上传至阿里云进入到目录解压/usr目录下:

 tar -zvxf elasticsearch-5.2.2.tar.gz -C /usr/

在/usr/elasticsearch-5.2.2 目录下,创建data  和log  文件夹

mkdir data
mkdir log

修改配置文件:/usr/elasticsearch-5.2.2/config/elasticsearch.yml

vim /usr/elasticsearch-5.2.2/config/elasticsearch.yml

需要修改设置的属性如下:

# ---------------------------------- Cluster -----------------------------------

# 集群名称

cluster.name: my-application

# ------------------------------------ Node ------------------------------------

# 节点名称 不能重复

node.name: node-102

# ----------------------------------- Paths ------------------------------------

# 数据路径

path.data: /opt/module/elasticsearch-5.2.2/data

# 日志路径

path.logs: /opt/module/elasticsearch-5.2.2/logs

# ----------------------------------- Memory -----------------------------------

#启动时锁定内存

bootstrap.memory_lock: false

bootstrap.system_call_filter: false

# ---------------------------------- Network -----------------------------------

network.host: 192.168.1.102

# --------------------------------- Discovery ----------------------------------

discovery.zen.ping.unicast.hosts: ["hadoop102"]

附一份完整的配置

# ======================== 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-miaomiao
#
# 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: /usr/elasticsearch-5.2.2/data
#
# Path to log files:
#
path.logs: /usr/elasticsearch-5.2.2/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: false
#
# 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: 你的ip 
#
# Set a custom port for HTTP:
#
#http.port: 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: ["miaomiao"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 3
#
# 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

注意:elasticsearch不支持root用户进行启动,所以我们需要在重新添加一个普通用户并赋予相应等权限,命令如下:

useradd elasticUser

chown -R elasticUser:elasticUser /usr/elasticsearch-5.2.2

出现问题1:max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

切换到root用户    编辑limits.conf     vim /etc/security/limits.conf 添加类似如下内容

* soft nofile 65536

* hard nofile 131072

* soft nproc 2048

* hard nproc 4096


切换到root用户,进入limits.d目录下修改配置文件。

vim /etc/security/limits.d/20-nproc.conf

修改如下内容:

* soft nproc 1024

#修改为

* soft nproc 2048


切换到root用户,修改配置sysctl.conf.         

 vim /etc/sysctl.conf 

添加下面配置:

vm.max_map_count=655360

并执行命令:sysctl -p

之后就可以启动了,注意不能使用root用户,请使用一个·普通·用户启动!

使用 curl 命令测试(忘记开端口号了,只能通过本机了囧囧囧)

三 head插件安装

3.1 下载插件

https://github.com/mobz/elasticsearch-head

elasticsearch-head-master.zip

3.2  nodejs官网下载安装包

https://nodejs.org/dist/

node-v6.9.2-linux-x64.tar.xz

3.3  将node-v6.9.2-linux-x64.tar.xz和elasticsearch-head-master.zip通过sftp上传至阿里云服务器

3.4 安装nodejs

tar -zxvf node-v6.9.2-linux-x64.tar.gz -C /usr/

3.5 配置node环境变量

export NODE_HOME=/usr/node-v6.9.2-linux-x64
export PATH=$PATH:$NODE_HOME/bin

查看node   和npm  版本

由于npm下载包需要翻墙,需要使用国内镜像,需配置成淘宝镜像,注意:此点很重要!!!!

npm install -g cnpm --registry=https://registry.npm.taobao.org

3.7 解压head插件到/usr/目录下

unzip elasticsearch-head-master.zip -d /usr/

3.8 查看head目录下是否有node_modules/grunt目录,没有则创建

使用npm 安装  

npm install grunt --save

3.9 安装grunt

npm install -g grunt-cli

3.10 编辑Gruntfile.js——vim Gruntfile.js

文件93行添加hostname:'0.0.0.0'

options: {

        hostname:'0.0.0.0',

        port: 9100,

        base: '.',

        keepalive: true

      }

3.11 检查head根目录下是否存在base文件夹

没有:将 _site下的base文件夹及其内容复制到head根目录下

mkdir base

cp -r  _site/* /usr/elasticsearch-head-master/base/

3.12 启动grunt server   grunt server -d

提示缺少模块,如果未出现下图错误,恭喜大功告成!

解决办法: 执行以下命令

npm install grunt-contrib-clean -registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat -registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-connect -registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy -registry=https://registry.npm.taobao.org 
npm install grunt-contrib-jasmine -registry=https://registry.npm.taobao.org
最后一个模块可能安装不成功,但是不影响使用。

3.13 执行3.12步骤,成功!

3.14 启动集群插件后,发现集群并未连接。

修改config下的配置文件elasticsearch.yml      vim /usr/elasticsearch-5.2.2/config/elasticsearch.yml

//运行跨越
http.cors.enabled: true
http.cors.allow-origin: "*"

之后重新启动elasticsearch和head插件。即可

猜你喜欢

转载自blog.csdn.net/qq_29308413/article/details/84895214