1.什么是全文檢索和Lucene
基于java環(huán)境,基于Lucene之上包裝一層外殼 Lucene是一個(gè)java的搜索引擎庫(kù),操作非常繁瑣
https://blog.csdn.net/sinat_35188997/article/details/83502920
2.Elasticsearch應(yīng)用場(chǎng)景
1.搜索:電商灯蝴,百科
2.高亮顯示:githup
3.分析和數(shù)據(jù)挖掘
3.Elasticsearch特點(diǎn)
1.高性能,分布式
2.對(duì)運(yùn)維友好乔夯,不需要會(huì)java語(yǔ)言,開(kāi)箱即用
3.功能豐富
mysql和ES對(duì)比 庫(kù) 索引 index 表 類(lèi)型 type 字段 項(xiàng) filter 行 文檔 doc
4.Elasticsearch在電商搜索的實(shí)現(xiàn)
mysql: skuid name 1 狗糧100kg 2 貓糧50kg 3 貓罐頭200g
ES: 聚合運(yùn)算之后得到SKUID: 1 2
拿到ID之后,mysql就只需要簡(jiǎn)單地where查詢(xún)即可 mysql: select xx from xxx where skuid 1
通俗的說(shuō)就是es會(huì)在運(yùn)算后得到一個(gè)值與mysql里面相對(duì)應(yīng)砰诵,減少mysql的壓力。
5.ES安裝啟動(dòng)
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的啟動(dòng)文件
/etc/sysconfig/elasticsearch #ES環(huán)境變量的相關(guān)配置
/usr/lib/sysctl.d/elasticsearch.conf #ES環(huán)境變量的相關(guān)配置
/usr/lib/systemd/system/elasticsearch.service #systemcd啟動(dòng)文件
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.重啟服務(wù)后發(fā)現(xiàn)報(bào)錯(cuò)
systemctl restart elasticsearch
4.解決內(nèi)存鎖定失敗:
查看日志發(fā)現(xiàn)提示內(nèi)存鎖定失敗
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命令:
? 最繁瑣
? 最復(fù)雜
? 最容易出錯(cuò)
? 不需要安裝任何軟件曙求,只需要有curl命令
es-head插件
? 查看數(shù)據(jù)方便
? 操作相對(duì)容易
? 需要node環(huán)境
kibana
? 查看數(shù)據(jù)以及報(bào)表格式豐富
? 操作很簡(jiǎn)單
? 需要java環(huán)境和安裝配置kibana
8.es-head插件的安裝
注意:需要修改配置文件添加允許跨域參數(shù)
修改ES配置文件支持跨域 http.cors.enabled: true http.cors.allow-origin: "*"
es-head 三種方式: 1.npm安裝方式
需要nodejs環(huán)境
需要連接國(guó)外源
2.docker安裝
3.google瀏覽器插件
修改文件名為zip后綴
解壓目錄
拓展程序-開(kāi)發(fā)者模式-打開(kāi)已解壓的目錄
連接地址修改為ES的IP地址
4.具體操作命令
Head插件在5.0以后安裝方式發(fā)生了改變与帆,需要nodejs環(huán)境支持卖词,或者直接使用別人封裝好的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.啟動(dòng)kibana
systemctl start kibana
4.操作ES
Dev Tools
10.插入數(shù)據(jù)
1.使用自定義的ID
PUT oldzhang/info/1
{
"name": "zhang",
"age": "29"
}
2.使用隨機(jī)ID
POST oldzhang/info/
{
"name": "zhang",
"age": "29",
"pet": "xiaoqi"
}
3.和mysql對(duì)應(yīng)關(guān)系建議單獨(dú)列一個(gè)字段
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"
}'
#創(chuàng)建測(cè)試語(yǔ)句
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"
}
#簡(jiǎn)單查詢(xún)
GET oldzhang/_search/
#條件查詢(xún)
GET oldzhang/_search
{
"query": {
"term": {
"name": {
"value": "xiaomin"
}
}
}
}
GET oldzhang/_search
{
"query": {
"term": {
"job": {
"value": "it"
}
}
}
}
#多條件查詢(xún)
GET /oldzhang/_search
{
"query" : {
"bool": {
"must": [
{"match": {"pet": "xiaoqi"}},
{"match": {"name": "zhang"}}
],
"filter": {
"range": {
"age": {
"gte": 27,
"lte": 30
}
}
}
}
}
}
}