1.什么是全文检索和Lucene
基于java环境,基于Lucene之上包装一层外壳 Lucene是一个java的搜索引擎库,操作非常繁琐
https://blog.csdn.net/sinat_35188997/article/details/83502920
2.Elasticsearch应用场景
1.搜索:电商,百科
2.高亮显示:githup
3.分析和数据挖掘
3.Elasticsearch特点
1.高性能,分布式
2.对运维友好,不需要会java语言,开箱即用
3.功能丰富
mysql和ES对比 库 索引 index 表 类型 type 字段 项 filter 行 文档 doc
4.Elasticsearch在电商搜索的实现
mysql: skuid name 1 狗粮100kg 2 猫粮50kg 3 猫罐头200g
ES: 聚合运算之后得到SKUID: 1 2
拿到ID之后,mysql就只需要简单地where查询即可 mysql: select xx from xxx where skuid 1
通俗的说就是es会在运算后得到一个值与mysql里面相对应,减少mysql的压力。
5.ES安装启动
1.下载软件
mkdir /data/soft
[root@db-01 /data/soft]# ll -h
total 268M
-rw-r--r-- 1 root root 109M Feb 25 2019 elasticsearch-6.6.0.rpm
-rw-r--r-- 1 root root 159M Sep 2 16:35 jdk-8u102-linux-x64.rpm
2.安装jdk
rpm -ivh jdk-8u102-linux-x64.rpm
[root@db-01 /data/soft]# java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)
3.安装ES
rpm -ivh elasticsearch-6.6.0.rpm
4.检查
netstat -lntup|grep 9200
systemctl status elasticsearch.service
[root@db-01 ~]# curl 127.0.0.1:9200
{
"name" : "Zr3WNj9",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "HR0ERydmTV2OH55subh7HQ",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
6.ES配置
1.查看ES有哪些配置
[root@db-01 ~]# rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml #ES主配置文件
/etc/elasticsearch/jvm.options #jvm配置
/etc/init.d/elasticsearch #init的启动文件
/etc/sysconfig/elasticsearch #ES环境变量的相关配置
/usr/lib/sysctl.d/elasticsearch.conf #ES环境变量的相关配置
/usr/lib/systemd/system/elasticsearch.service #systemcd启动文件
2.自定义配置文件
[root@db-01 ~]# grep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml
node.name: node-1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true
network.host: 10.0.0.51,127.0.0.1
http.port: 9200
3.重启服务后发现报错
systemctl restart elasticsearch
4.解决内存锁定失败:
查看日志发现提示内存锁定失败
tail -f /var/log/elasticsearch/elasticsearch.log
[2019-09-06T10:12:35,988][ERROR][o.e.b.Bootstrap ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
解决方案:
systemctl edit elasticsearch
[Service]
LimitMEMLOCK=infinity
systemctl daemon-reload
systemctl restart elasticsearch
7.ES交互方式
三种交互方式
curl命令:
最繁琐
最复杂
最容易出错
不需要安装任何软件,只需要有curl命令
es-head插件
查看数据方便
操作相对容易
需要node环境
kibana
查看数据以及报表格式丰富
操作很简单
需要java环境和安装配置kibana
8.es-head插件的安装
注意:需要修改配置文件添加允许跨域参数
修改ES配置文件支持跨域 http.cors.enabled: true http.cors.allow-origin: "*"
es-head 三种方式: 1.npm安装方式
需要nodejs环境
需要连接国外源
2.docker安装
3.google浏览器插件
修改文件名为zip后缀
解压目录
拓展程序-开发者模式-打开已解压的目录
连接地址修改为ES的IP地址
4.具体操作命令
Head插件在5.0以后安装方式发生了改变,需要nodejs环境支持,或者直接使用别人封装好的docker镜像 插件官方地址 https://github.com/mobz/elasticsearch-head
使用docker部署elasticsearch-head
docker pull alivv/elasticsearch-head
docker run --name es-head -p 9100:9100 -dit elivv/elasticsearch-head
使用nodejs编译安装elasticsearch-head
yum install nodejs npm openssl screen -y
node -v
npm -v
npm install -g cnpm --registry=https://registry.npm.taobao.org
cd /opt/
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head/
cnpm install
screen -S es-head
cnpm run start
Ctrl+A+D
9.kibana与ES交互
1.安装kibana
rpm -ivh kibana-6.6.0-x86_64.rpm
2.配置kibana
[root@db-01 /data/soft]# grep "^[a-Z]" /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.51"
elasticsearch.hosts: ["http://localhost:9200"]
kibana.index: ".kibana"
3.启动kibana
systemctl start kibana
4.操作ES
Dev Tools
10.插入数据
1.使用自定义的ID
PUT oldzhang/info/1
{
"name": "zhang",
"age": "29"
}
2.使用随机ID
POST oldzhang/info/
{
"name": "zhang",
"age": "29",
"pet": "xiaoqi"
}
3.和mysql对应关系建议单独列一个字段
POST oldzhang/info/
{
"uid": "1",
"name": "ya",
"age": "29"
}
curl -XPUT '10.0.0.51:9200/oldzhang/info/1?pretty' -H 'Content-Type: application/json' -d'
{
"uid": "1",
"name": "ya",
"age": "29"
}'
curl -XPUT '10.0.0.51:9200/oldzhang/info/2?pretty' -H 'Content-Type: application/json' -d'
{
"uid": "1",
"name": "zhang",
"age": "29"
}'
#创建测试语句
POST oldzhang/info/
{
"name": "zhang",
"age": "29",
"pet": "xiaoqi",
"job": "it"
}
POST oldzhang/info/
{
"name": "xiao1",
"age": "30",
"pet": "xiaoqi",
"job": "it"
}
POST oldzhang/info/
{
"name": "xiao2",
"age": "26",
"pet": "xiaoqi",
"job": "it"
}
POST oldzhang/info/
{
"name": "xiao4",
"age": "35",
"pet": "xiaoqi",
"job": "it"
}
POST oldzhang/info/
{
"name": "ya",
"age": "28",
"pet": "xiaomin",
"job": "it"
}
POST oldzhang/info/
{
"name": "xiaomin",
"age": "26",
"pet": "xiaowang",
"job": "SM"
}
POST oldzhang/info/
{
"name": "hemengfei",
"age": "38",
"pet": "xiaohe",
"job": "3P"
}
POST oldzhang/info/
{
"name": "xiaoyu",
"age": "28",
"pet": "bijiben",
"job": "fly"
}
#简单查询
GET oldzhang/_search/
#条件查询
GET oldzhang/_search
{
"query": {
"term": {
"name": {
"value": "xiaomin"
}
}
}
}
GET oldzhang/_search
{
"query": {
"term": {
"job": {
"value": "it"
}
}
}
}
#多条件查询
GET /oldzhang/_search
{
"query" : {
"bool": {
"must": [
{"match": {"pet": "xiaoqi"}},
{"match": {"name": "zhang"}}
],
"filter": {
"range": {
"age": {
"gte": 27,
"lte": 30
}
}
}
}
}
}
}