ElasticSearch學(xué)習(xí)筆記

一、ElasticSearch學(xué)習(xí)筆記

擼一門技術(shù),必先登其官網(wǎng),扒其皮,喝其血

官網(wǎng)地址:https://www.elastic.co/products/elasticsearch

官方中文文檔地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

1.1.ElasticSearch簡(jiǎn)介

ElasticSearch是一個(gè)分布式搜索服務(wù),提供的是一組Restful API,底層基于Lucene,采用多shard(分片)的方式保證數(shù)據(jù)安全,并且提供自動(dòng)resharding的功能。是目前全文搜索引擎的首選,可以快速的存儲(chǔ)、搜索和分析海量數(shù)據(jù),Springboot通過(guò)整合Spring Data ElasticSearch為我們提供了非常方便的檢索功能支持。

1.2.ElasticSearch原始安裝

系統(tǒng)環(huán)境

  • CentOS 7.6.1810
  • jdk 1.8.0_201

所需安裝文件

  • elasticsearch-6.6.0.tar.gz
  • jdk-8u201-linux-x64.tar.gz
  • elasticsearch-head-master.zip
  • node-v10.15.1-linux-x64.tar.gz

elasticsearch安裝方法

tar -zxvf elasticsearch-6.6.0.tar.gz -C /opt/module/ # 解壓安裝包
[root@localhost elasticsearch-6.6.0]# mkdir data # 創(chuàng)建數(shù)據(jù)文件夾(6.0自帶logs文件夾)
vi elasticsearch.yml # 修改配置文件
cluster.name: my-application # 集群名稱(多集群時(shí)候只需節(jié)點(diǎn)名稱一直即可)
node.name: node-102 # 節(jié)點(diǎn)名稱
path.data: /opt/module/elasticsearch-6.6.0/data # 數(shù)據(jù)路徑
path.logs: /opt/module/elasticsearch-6.6.0/logs # 日志路徑
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 192.168.1.8 # 網(wǎng)絡(luò)地址
http.port: 9200 # 端口
discovery.zen.ping.unicast.hosts: ["hadoop102"] # 主機(jī)名

注意:node.name可以隨便取,但是一個(gè)集群中不能重復(fù),注意path.data前不能有空格,冒號(hào)后必須有一個(gè)空格

elasticsearch常見(jiàn)問(wèn)題解決

問(wèn)題一:ERROR: bootstrap checks failed

su root
vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

問(wèn)題二:max number of threads [1024] for user [lish] likely too low, increase to at least [2048]

vi /etc/security/limits.d/90-nproc.conf
* soft nproc 2048

問(wèn)題三:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]

vi /etc/sysctl.conf
vm.max_map_count=655360
sysctl -p

elasticsearch啟動(dòng)

elasticsearch禁止使用root用戶啟動(dòng),需要新建一個(gè)testuser用戶

[testuser@hadoop102 elasticsearch-6.6.0]$ ./bin/elasticsearch

訪問(wèn)地址:http://192.168.1.8:9200/

ElasticSearch插件安裝

插件地址:https://github.com/zt1115798334/elasticsearch-head-master

Nodejs安裝

tar -zxvf node-v10.15.1-linux-x64.tar.gz -C /opt/module/
vi /etc/profile
export NODE_HOME=/opt/module/node-v10.15.1-linux-x64
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile 

elasticsearch-head-master安裝

[root@hadoop102 sortware]# unzip elasticsearch-head-master.zip -d /opt/module/
[root@hadoop102 elasticsearch-head-master]# npm install grunt --save
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install -g grunt-cli
vim Gruntfile.js
options: {
    hostname:'0.0.0.0',
    port: 9100,
    base: '.',
    keepalive: true
}
# 檢查head根目錄下是否存在base文件夾 沒(méi)有:將 _site下的base文件夾及其內(nèi)容復(fù)制到head根目錄下
mkdir base
cp base/* ../base/
[root@hadoop102 module]# chown -R luokangyuan:luokangyuan elasticsearch-head-master/
[luokangyuan@hadoop102 elasticsearch-head-master]$ 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
http://192.168.1.8:9100/
vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"

1.3.ElasticSearch的docker安裝

啟動(dòng)Docker

[root@localhost /]# systemctl start docker

搜索鏡像

[root@localhost /]# docker search elasticsearch

使用鏡像加速器下載

[root@localhost /]# docker pull registry.docker-cn.com/library/elasticsearch

檢查是否安裝成功

[root@localhost /]# docker images

啟動(dòng)ElasticSearch

docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 5acf0e8da90b

檢查是否啟動(dòng)

[root@localhost /]# docker ps

訪問(wèn)測(cè)試

http://192.168.1.14:9200/

1.4.ElasticSearch核心概念

Cluster集群

集群就是包含了多個(gè)節(jié)點(diǎn),每一個(gè)節(jié)點(diǎn)屬于哪一個(gè)集群是通過(guò)一個(gè)集群名稱配置。

Node節(jié)點(diǎn)

集群中的一個(gè)節(jié)點(diǎn),節(jié)點(diǎn)也存在名稱,默認(rèn)是隨機(jī)分配一個(gè)名稱,默認(rèn)節(jié)點(diǎn)會(huì)加入到一個(gè)elasticsearch集群中。

Index索引

索引包含的是一大推相似結(jié)構(gòu)的文檔數(shù)據(jù),例如我們的商品索引,訂單索引等,類比于我們的數(shù)據(jù)庫(kù)。

Type類型

每一個(gè)索引里面可以有一個(gè)或者多個(gè)type,typeindex中的一個(gè)邏輯數(shù)據(jù)分類,比如我的博客系統(tǒng),一個(gè)索引,可以定義用戶數(shù)據(jù)type,可以定義文章數(shù)據(jù)type,也可以定義評(píng)論數(shù)據(jù)type,類比數(shù)據(jù)庫(kù)的表。

Document文檔

文檔是ElasticSearch中最小的數(shù)據(jù)單元,一條Document可以是一條文章數(shù)據(jù),一條用戶數(shù)據(jù),一條評(píng)論數(shù)據(jù),通常使用JSON數(shù)據(jù)結(jié)構(gòu)來(lái)表示,每個(gè)index下的type中,存儲(chǔ)多個(gè)document,類別數(shù)據(jù)庫(kù)中的行。

Field字段

FieldElasticSearch中的最小單位,一個(gè)document里面粗在多個(gè)Field字段,每個(gè)Field就是一個(gè)數(shù)據(jù)字段,類比數(shù)據(jù)庫(kù)中的列。

mapping映射

數(shù)據(jù)如何存儲(chǔ)在索引上,需要一個(gè)約束配置,例如數(shù)據(jù)類型,是否存儲(chǔ),查詢的時(shí)候是否分詞等等,類比數(shù)據(jù)庫(kù)匯總的約束。

ElasticSearch和數(shù)據(jù)庫(kù)對(duì)別

關(guān)系型數(shù)據(jù)庫(kù)Mysql 非關(guān)系型數(shù)據(jù)庫(kù)ElasticSearch
數(shù)據(jù)庫(kù)Database 索引Index
表Table 類型Type
數(shù)據(jù)行Row 文檔Document
數(shù)據(jù)列Column 字段Field
約束Schema 映射Mapping

ElasticSearch核心概念圖解

WX20190223-173048@2x

1.5.Springboot集成ElasticSearch

Springboot默認(rèn)使用Spring Data Elasticsearch模塊進(jìn)行操作,同時(shí)也存在另外一個(gè)操作ElasticSearch的模塊,那就是jest。

使用Jest與ElasticSearch進(jìn)行交互

Jest的GitHub地址:https://github.com/searchbox-io/Jest

Jest文檔地址:https://github.com/searchbox-io/Jest/tree/master/jest

第一步:增加POM 文件

<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.4</version>
</dependency>

第二步:增加ElasticSearch配置項(xiàng)

spring:
  elasticsearch:
    jest:
      uris: http://192.168.1.9:9200/

第三步:使用JestClient進(jìn)行交互

public class Users {
    // 標(biāo)示主鍵字段
    @JestId
    private Integer id;
    private Integer code;
    private String name;
    private String sex;
    private String age;
    private String notes;
}
@Autowired
JestClient jestClient;

@Test
public void contextLoads() {
    // 給es中保存一份文檔
    Users users = new Users();
    users.setId(2);
    users.setCode(123456);
    users.setAge("87");
    users.setName("魯班七號(hào)");
    users.setSex("男");
    users.setNotes("王者峽谷人見(jiàn)人想揍的小魯班");

    // 構(gòu)建一個(gè)王者榮耀的索引和英雄角色類型
    Index build = new Index.Builder(users).index("wzry").type("yxjs").build();

    try {
        jestClient.execute(build);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Test
public void testSeach(){
    // 測(cè)試搜索es中滿足條件的數(shù)據(jù)
    String json = "{\n" +
        "    \"query\" : {\n" +
        "        \"match\" : {\n" +
        "            \"notes\" : \"峽谷小人\"\n" +
        "        }\n" +
        "    }\n" +
        "}";
    Search build = new Search.Builder(json).addIndex("wzry").addType("yxjs").build();
    try {
        SearchResult execute = jestClient.execute(build);
        System.out.println(execute.getJsonString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最后測(cè)試

http://192.168.1.9:9200/wzry/yxjs/2

使用Spring Data Elasticsearch

官方文檔地址:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.5.RELEASE/reference/html/

第一步:增加POM文件

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

第二步:增加配置項(xiàng)

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 192.168.1.9:9300
      repositories:
        enabled: true

第三步:進(jìn)行數(shù)據(jù)交互

@Document(indexName = "study", type = "book")
public class Book {
    private Integer id;
    private String name;
    private String notes;
}
public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
}
@Autowired
BookRepository bookRepository;

@Test
public void testSpringDataEs(){
    Book book = new Book();
    book.setId(11);
    book.setName("一個(gè)陌生女人的來(lái)信");
    book.setNotes("還不錯(cuò)");
    bookRepository.index(book);
}

注意:如果啟動(dòng)報(bào)錯(cuò),可能是spring data elasticsearch和elasticsearch存在版本對(duì)應(yīng)關(guān)系

版本對(duì)應(yīng)參考官方文檔:https://github.com/spring-projects/spring-data-elasticsearch/blob/master/README.md

spring data elasticsearch elasticsearch
3.2.x 6.5.0
3.1.x 6.2.2
3.0.x 5.5.0
2.1.x 2.4.0
2.0.x 2.2.0
1.3.x 1.5.2

版本不適配解決方法

  • 查看spring data elasticsearch的版本號(hào)
  • 安裝對(duì)應(yīng)版本的elasticsearch即可
  • 當(dāng)然也可以根據(jù)安裝的elasticsearch版本改變Springboot版本

解決辦法示例:

# 安裝對(duì)應(yīng)版本的elasticsearch
[root@localhost /]# docker pull registry.docker-cn.com/library/elasticsearch:2.4
# 啟動(dòng)對(duì)應(yīng)版本的elasticsearch
[root@localhost /]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES14  01e5bee1e059

我個(gè)人本次測(cè)試環(huán)境:

  • Springboot:1.5.19
  • elasticsearch:2.4

附錄一:使用的Linux命令

  • mkdir 創(chuàng)建文件夾
  • pwd 查看當(dāng)前所在路徑
  • scp -r CentOS-7-x86_64-Minimal-1810.iso root@192.168.1.8:/opt/sortware 在當(dāng)前文件上傳文件到指定服務(wù)器文件夾
  • cat /etc/centos-release 查看系統(tǒng)版本
  • rm -rf jdk-8u201-linux-i586.tar.gz 不提示的遞歸刪除文件或者文件夾
  • tar -zxvf jdk-8u201-linux-x64.tar.gz 解壓
  • hostnamectl查看主機(jī)名
  • whereis sudoers 查找文件位置
  • ls -l /etc/sudoers 查看文件權(quán)限
  • chmod -v u+w /etc/sudoers 加入可寫權(quán)限
  • firewall-cmd --state 查看防火墻狀態(tài)
  • systemctl stop firewalld.service 關(guān)閉防火墻

附錄二:CentOs7安裝jdk1.8

1.上傳安裝文件

/opt/sortware/jdk-8u201-linux-x64.tar.gz

2.解壓

tar -zxvf jdk-8u201-linux-x64.tar.gz

3.重命名

mv jdk1.8.0_201 jdk1.8

4.打開(kāi)系統(tǒng)配置文件

vi /etc/profile

5.添加環(huán)境變量

## Java
export JAVA_HOME=/opt/sortware/jdk1.8
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

6.重啟配置文件

source /etc/profile

7.查看版本

java -version

附錄三:CentOs7安裝docker

CentOs7安裝步驟

附錄四:常用docker命令

刪除所有容器

docker rm `docker ps -a -q`

查看存在的鏡像

docker images

查看所有啟動(dòng)的容器

docker ps -a

停止容器

docker stop

搜索倉(cāng)庫(kù)

docker search elasticsearch

拉取倉(cāng)庫(kù)

 docker pull registry.docker-cn.com/library/elasticsearch
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容