1 ElasticSearch簡介#
1.1 什么是ElasticSearch
? Elasticsearch是一個實時的分布式搜索和分析引擎统刮。它可以幫助你用前所未有的速
度去處理大規(guī)模數(shù)據(jù)识脆。ElasticSearch是一個基于Lucene的搜索服務(wù)器鲤遥。它提供了一個分
布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發(fā)
的沟于,并作為Apache許可條款下的開放源碼發(fā)布眼五,是當(dāng)前流行的企業(yè)級搜索引擎妆艘。設(shè)計用
于云計算中,能夠達到實時搜索看幼,穩(wěn)定批旺,可靠,快速诵姜,安裝使用方便汽煮。
1.2 ElasticSearch特點
(1)可以作為一個大型分布式集群(數(shù)百臺服務(wù)器)技術(shù),處理PB級數(shù)據(jù)棚唆,服務(wù)大公
司暇赤;也可以運行在單機上
(2)將全文檢索、數(shù)據(jù)分析以及分布式技術(shù)宵凌,合并在了一起鞋囊,才形成了獨一無二的ES;
(3)開箱即用的瞎惫,部署簡單
(4)全文檢索溜腐,同義詞處理,相關(guān)度排名瓜喇,復(fù)雜數(shù)據(jù)分析挺益,海量數(shù)據(jù)的近實時處理
1.3 ElasticSearch體系結(jié)構(gòu)
下表是Elasticsearch與MySQL數(shù)據(jù)庫邏輯結(jié)構(gòu)概念的對比
Elasticsearch | 關(guān)系型數(shù)據(jù)庫Mysql |
---|---|
索引(index) | 數(shù)據(jù)庫(databases) |
類型(type) | 表(table) |
文檔(document) | 行(row) |
2 走進ElasticSearch
2.1 ElasticSearch部署與啟動
下載ElasticSearch 5.6.8版本
https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8
資源\配套軟件中也提供了安裝包
無需安裝,解壓安裝包后即可使用
在命令提示符下欠橘,進入ElasticSearch安裝目錄下的bin目錄,執(zhí)行命令
elasticsearch
即可啟動矩肩。
我們打開瀏覽器,在地址欄輸入http://127.0.0.1:9200/ 即可看到輸出結(jié)果
{
"name" : "uV2glMR",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "RdV7UTQZT1‐Jnka9dDPsFg",
"version" : {
"number" : "5.6.8",
"build_hash" : "688ecce",
"build_date" : "2018‐02‐16T16:46:30.010Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
2.2 Postman調(diào)用RestAPI
2.2.1 新建索引
例如我們要創(chuàng)建一個叫articleindex的索引 ,就以put方式提交
http://127.0.0.1:9200/articleindex/
2.2.2 新建文檔
新建文檔:
以post方式提交 http://127.0.0.1:9200/articleindex/article
body:
{
"title":"SpringBoot2.0",
"content":"發(fā)布啦"
}
返回結(jié)果如下:
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
_id是由系統(tǒng)自動生成的。 為了方便之后的演示黍檩,我們再次錄入幾條測試數(shù)據(jù)叉袍。
2.2.3 查詢?nèi)课臋n
查詢某索引某類型的全部數(shù)據(jù),以get方式請求
http://127.0.0.1:9200/articleindex/article/_search 返回結(jié)果如下
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKrI4pFdLZnId5S_F7",
"_score": 1,
"_source": {
"title": "SpringBoot2.0",
"content": "發(fā)布啦"
}
},
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_score": 1,
"_source": {
"title": "elasticsearch入門",
"content": "零基礎(chǔ)入門"
}
}
]
}
}
2.2.4 修改文檔
以put形式提交以下地址:
http://192.168.184.134:9200/articleindex/article/AWPKrI4pFdLZnId5S_F7
body:
{
"title":"SpringBoot2.0正式版",
"content":"發(fā)布了嗎"
}
返回結(jié)果
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
如果我們在地址中的ID不存在刽酱,則會創(chuàng)建新文檔
以put形式提交以下地址:
http://192.168.184.134:9200/articleindex/article/1
body:
{
"title":"十次方課程好給力",
"content":"知識點很多"
}
返回信息:
{
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
再次查詢喳逛,看是否有新增的這條文檔
2.2.5 按ID查詢文檔
GET方式請求
http://192.168.184.134:9200/articleindex/article/1
2.2.6 基本匹配查詢
根據(jù)某列進行查詢 get方式提交下列地址:
http://192.168.184.134:9200/articleindex/article/_search?q=title:十次方課程好給力
以上為按標(biāo)題查詢,返回結(jié)果如下:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.0649285,
"hits": [
{
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_score": 2.0649285,
"_source": {
"title": "十次方課程好給力",
"content": "知識點很多"
}
}
]
}
}
2.2.7 模糊查詢
我們可以用代表任意字符:
http://192.168.184.134:9200/articleindex/article/_search?q=title:s*
2.2.8 刪除文檔
根據(jù)ID刪除文檔,刪除ID為1的文檔 DELETE方式提交
http://192.168.184.134:9200/articleindex/article/1
返回結(jié)果如下:
{
"found": true,
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再次查看全部是否還存在此記錄
3 Head插件的安裝與使用
3.1 Head插件安裝
如果都是通過rest請求的方式使用Elasticsearch棵里,未免太過麻煩润文,而且也不夠人性化。我
們一般都會使用圖形化界面來實現(xiàn)Elasticsearch的日常管理殿怜,最常用的就是Head插件
步驟1:
下載head插件:https://github.com/mobz/elasticsearch-head
配套資料中已提供典蝌。 elasticsearch-head-master.zip
步驟2:
解壓到任意目錄,但是要和elasticsearch的安裝目錄區(qū)別開头谜。
步驟3:
安裝node js ,安裝cnpm
npm install ‐g cnpm ‐‐registry=https://registry.npm.taobao.org
步驟4:
將grunt安裝為全局命令 骏掀。Grunt是基于Node.js的項目構(gòu)建工具。它可以自動運行你所
設(shè)定的任務(wù)
npm install ‐g grunt‐cli
步驟5:安裝依賴
步驟6:
進入head目錄啟動head柱告,在命令提示符下輸入命令
cnpm install
步驟6:
進入head目錄啟動head截驮,在命令提示符下輸入命令
grunt server
步驟7:
打開瀏覽器,輸入 http://localhost:9100
步驟8:
點擊連接按鈕沒有任何相應(yīng)际度,按F12發(fā)現(xiàn)有如下錯誤
No 'Access-Control-Allow-Origin' header is present on the requested resource
這個錯誤是由于elasticsearch默認(rèn)不允許跨域調(diào)用葵袭,而elasticsearch-head是屬于前端工
程,所以報錯乖菱。
我們這時需要修改elasticsearch的配置坡锡,讓其允許跨域訪問。
修改elasticsearch配置文件:elasticsearch.yml块请,增加以下兩句命令:
http.cors.enabled: true
http.cors.allow‐origin: "*"
此步為允許elasticsearch跨越訪問 點擊連接即可看到相關(guān)信息
3.2 Head插件操作
3.2.1 新建索引
選擇“索引”選項卡娜氏,點擊“新建索引”按鈕
3.2.2 新建或修改文檔
3.2.3 搜索文檔
3.2.4 刪除文檔
4 IK分詞器
4.1什么是IK分詞器
我們在瀏覽器地址欄輸入http://127.0.0.1:9200/_analyze?
analyzer=chinese&pretty=true&text=我是程序員,瀏覽器顯示效果如下
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "程",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "序",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "員",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
}
]
}
默認(rèn)的中文分詞是將每個字看成一個詞墩新,這顯然是不符合要求的贸弥,所以我們需要安裝中
文分詞器來解決這個問題。
IK分詞是一款國人開發(fā)的相對簡單的中文分詞器海渊。雖然開發(fā)者自2012年之后就不在維護
了绵疲,但在工程應(yīng)用中IK算是比較流行的一款!我們今天就介紹一下IK中文分詞器的使用臣疑。
4.2 IK分詞器安裝
下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases 下載5.6.8版
本 課程配套資源也提供了: 資源\配套軟件\elasticsearch\elasticsearch-analysis-ik-5.6.8.zip
(1)先將其解壓盔憨,將解壓后的elasticsearch文件夾重命名文件夾為ik
(2)將ik文件夾拷貝到elasticsearch/plugins 目錄下。
(3)重新啟動讯沈,即可加載IK分詞器
4.3 IK分詞器測試
IK提供了兩個分詞算法ik_smart 和 ik_max_word
其中 ik_smart 為最少切分郁岩,ik_max_word為最細(xì)粒度劃分
我們分別來試一下
(1)最小切分:在瀏覽器地址欄輸入地址
http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序員
輸出的結(jié)果為:
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "程序員",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
}
]
}
(2)最細(xì)切分:在瀏覽器地址欄輸入地址
http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序
員
輸出的結(jié)果為:
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "程序員",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "程序",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "員",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 4
}
]
}
4.4 自定義詞庫
我們現(xiàn)在測試"傳智播客",瀏覽器的測試效果如下:
http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=傳智播客
{
"tokens" : [
{
"token" : "傳",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "智",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "播",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "客",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 3
}
]
}
默認(rèn)的分詞并沒有識別“傳智播客”是一個詞。如果我們想讓系統(tǒng)識別“傳智播客”是一個
詞问慎,需要編輯自定義詞庫萍摊。
步驟:
(1)進入elasticsearch/plugins/ik/config目錄
(2)新建一個my.dic文件,編輯內(nèi)容:
測試測試
修改IKAnalyzer.cfg.xml(在ik/config目錄下)
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!‐‐用戶可以在這里配置自己的擴展字典 ‐‐>
<entry key="ext_dict">my.dic</entry>
<!‐‐用戶可以在這里配置自己的擴展停止詞字典‐‐>
<entry key="ext_stopwords"></entry>
</properties>
重新啟動elasticsearch,通過瀏覽器測試分詞效果
{
"tokens" : [
{
"token" : "傳智播客",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
}
]
}
5 搜索微服務(wù)開發(fā)
5.1 需求分析
5.2 代碼編寫
5.2.1 模塊搭建
(1)創(chuàng)建模塊tensquare_search 如叼,pom.xml引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring‐data‐elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0‐SNAPSHOT</version>
</dependency>
</dependencies>
(2)application.yml
server:
port: 9007
spring:
application:
name: tensquare‐search #指定服務(wù)名
data:
elasticsearch:
cluster‐nodes: 127.0.0.1:9300
(3)創(chuàng)建包com.tensquare.search 冰木,包下創(chuàng)建啟動類
@SpringBootApplication
public class RecruitApplication {
public static void main(String[] args) {
SpringApplication.run(RecruitApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
}
5.2.2 添加文章
(1)創(chuàng)建實體類
創(chuàng)建com.tensquare.search.pojo包,包下建立類
/**
* 文章實體類
*/
@Document(indexName="tensquare",type="article")
public class Article implements Serializable{
@Id
private String id;//ID
@Field(index= true
,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String title;//標(biāo)題
@Field(index= true
,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String content;//文章正文
private String state;//審核狀態(tài)
//getter and setter ......
}
(2)創(chuàng)建數(shù)據(jù)訪問接口
創(chuàng)建com.tensquare.search.dao包笼恰,包下建立接口
/**
* 文章數(shù)據(jù)訪問層接口
*/
public interface ArticleSearchDao extends
ElasticsearchRepository<Article,String> {
}
(3)創(chuàng)建業(yè)務(wù)邏輯類
創(chuàng)建com.tensquare.search.service包踊沸,包下建立類
@Service
public class ArticleSearchService {
@Autowired
private ArticleSearchDao articleSearchDao;
/**
* 增加文章
* @param article
*/
public void add(Article article){
articleSearchDao.save(article);
}
}
(4)創(chuàng)建控制器類
創(chuàng)建com.tensquare.search.controller包,包下建立類
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
@Autowired
private ArticleSearchService articleSearchService;
@RequestMapping(method= RequestMethod.POST)
public Result save(@RequestBody Article article){
articleSearchService.save(article);
return new Result(true, StatusCode.OK, "操作成功");
}
}
5.2.3 文章搜索
(1)ArticleSearchRepository新增方法定義
/**
* 檢索
* @param
* @return
*/
public Page<Article> findByTitleOrContentLike(String title, String
content, Pageable pageable);
(2)ArticleSearchService新增方法
public Page<Article> findByTitleLike(String keywords, int page, int size)
{
PageRequest pageRequest = PageRequest.of(page‐1, size);
return
articleSearchRepository.findByTitleOrContentLike(keywords,keywords,
pageRequest);
}
(3)ArticleSearchController方法
@RequestMapping(value="/search/{keywords}/{page}/{size}",method=
RequestMethod.GET)
public Result findByTitleLike(@PathVariable String keywords,
@PathVariable int page, @PathVariable int size){
Page<Article> articlePage =
articleSearchService.findByTitleLike(keywords,page,size);
return new Result(true, StatusCode.OK, "查詢成功",
new PageResult<Article>(articlePage.getTotalElements(),
articlePage.getContent()));
}
6 elasticsearch與MySQL數(shù)據(jù)同步
6.1 Logstash
6.1.1什么是Logstash
Logstash是一款輕量級的日志搜集處理框架社证,可以方便的把分散的逼龟、多樣化的日志搜集
起來,并進行自定義的處理追葡,然后傳輸?shù)街付ǖ奈恢蒙舐郑热缒硞€服務(wù)器或者文件。
6.1.2 Logstash安裝與測試
解壓辽俗,進入bin目錄
logstash ‐e 'input { stdin { } } output { stdout {} }'
控制臺輸入字符,隨后就有日志輸出
stdin篡诽,表示輸入流崖飘,指從鍵盤輸入
stdout,表示輸出流杈女,指從顯示器輸出
命令行參數(shù):
-e 執(zhí)行
--config 或 -f 配置文件朱浴,后跟參數(shù)類型可以是一個字符串的配置或全路徑文件名或全路徑
路徑(如:/etc/logstash.d/,logstash會自動讀取/etc/logstash.d/目錄下所有*.conf 的文
本文件达椰,然后在自己內(nèi)存里拼接成一個完整的大配置文件再去執(zhí)行)
6.2 MySQL數(shù)據(jù)導(dǎo)入Elasticsearch
(1)在logstash-5.6.8安裝目錄下創(chuàng)建文件夾mysqletc (名稱隨意)
(2)文件夾下創(chuàng)建mysql.conf (名稱隨意) 翰蠢,內(nèi)容如下:
input {
jdbc {
# mysql jdbc connection string to our backup databse 后面的test
對應(yīng)mysql中的test數(shù)據(jù)庫
jdbc_connection_string =>
"jdbc:mysql://127.0.0.1:3306/tensquare_article?characterEncoding=UTF8"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "123456"
# the path to our downloaded jdbc driver
jdbc_driver_library => "D:/logstash‐5.6.8/mysqletc/mysql‐
connector‐java‐5.1.46.jar"
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
#以下對應(yīng)著要執(zhí)行的sql的絕對路徑。
statement => "select id,title,content from tb_article"
#定時字段 各字段含義(由左至右)分啰劲、時梁沧、天、月蝇裤、年廷支,全部為*默認(rèn)含義為
每分鐘都更新
schedule => "* * * * *"
}
}
output {
elasticsearch {
#ESIP地址與端口
hosts => "localhost:9200"
#ES索引名稱(自己定義的)
index => "tensquare"
#自增ID編號
document_id => "%{id}"
document_type => "article"
}
stdout {
#以JSON格式輸出
codec => json_lines
}
}
(3)將mysql驅(qū)動包mysql-connector-java-5.1.46.jar拷貝至D:/logstash-
5.6.8/mysqletc/ 下 。D:/logstash-5.6.8是你的安裝目錄
(4)命令行下執(zhí)行
logstash ‐f ../mysqletc/mysql.conf
觀察控制臺輸出栓辜,每間隔1分鐘就執(zhí)行一次sql查詢恋拍。
再次刷新elasticsearch-head的數(shù)據(jù)顯示,看是否也更新了數(shù)據(jù)藕甩。
7 Elasticsearch Docker環(huán)境下安裝
7.1 容器的創(chuàng)建與遠(yuǎn)程連接
(1)下載鏡像(此步省略)
docker pull elasticsearch:5.6.8
(2)創(chuàng)建容器
docker run ‐di ‐‐name=tensquare_elasticsearch ‐p 9200:9200 ‐p 9300:9300
elasticsearch:5.6.8
(3)瀏覽器輸入地址http://192.168.184.134:9200/ 即可看到如下信息
{
"name" : "WmBn0H‐",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "2g‐VVbm9Rty7J4sksZNJEg",
"version" : {
"number" : "5.6.8",
"build_hash" : "688ecce",
"build_date" : "2018‐02‐16T16:46:30.010Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
(4)我們修改demo的application.yml
spring:
data:
elasticsearch:
cluster‐nodes: 192.168.184.135:9300
(5)運行測試程序施敢,發(fā)現(xiàn)會報如下錯誤
NoNodeAvailableException[None of the configured nodes are available:
[{#transport#‐1}{exvgJLR‐RlCNMJy‐hzKtnA}{192.168.184.135}
{192.168.184.135:9300}]
]
at
org.elasticsearch.client.transport.TransportClientNodesService.ensureNodes
AreAvailable(TransportClientNodesService.java:347)
at
org.elasticsearch.client.transport.TransportClientNodesService.execute(Tra
nsportClientNodesService.java:245)
at
org.elasticsearch.client.transport.TransportProxyClient.execute(TransportP
roxyClient.java:59)
這是因為elasticsearch從5版本以后默認(rèn)不開啟遠(yuǎn)程連接,需要修改配置文件
(6)我們進入容器
docker exec ‐it tensquare_elasticsearch /bin/bash
此時,我們看到elasticsearch所在的目錄為/usr/share/elasticsearch ,進入config看到了
配置文件
elasticsearch.yml
我們通過vi命令編輯此文件僵娃,尷尬的是容器并沒有vi命令 概作,咋辦?我們需要以文件掛載的
方式創(chuàng)建容器才行悯许,這樣我們就可以通過修改宿主機中的某個文件來實現(xiàn)對容器內(nèi)配置
文件的修改
(7)拷貝配置文件到宿主機
首先退出容器仆嗦,然后執(zhí)行命令:
docker cp
tensquare_elasticsearch:/usr/share/elasticsearch/config/elasticsearch.yml
/usr/share/elasticsearch.yml
(8)停止和刪除原來創(chuàng)建的容器
docker stop tensquare_elasticsearch
docker rm tensquare_elasticsearch
(9)重新執(zhí)行創(chuàng)建容器命令
docker run ‐di ‐‐name=tensquare_elasticsearch ‐p 9200:9200 ‐p 9300:9300 ‐v
/usr/share/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch
.yml elasticsearch:5.6.8
(10)修改/usr/share/elasticsearch.yml 將 transport.host: 0.0.0.0 前的#去掉后保
存文件退出。其作用是允許任何ip地址訪問elasticsearch .開發(fā)測試階段可以這么做先壕,生
產(chǎn)環(huán)境下指定具體的IP
(11)重啟啟動
docker restart tensquare_elasticsearch
重啟后發(fā)現(xiàn)重啟啟動失敗了瘩扼,這時什么原因呢?這與我們剛才修改的配置有關(guān)垃僚,因為
elasticsearch在啟動的時候會進行一些檢查集绰,比如最多打開的文件的個數(shù)以及虛擬內(nèi)存
區(qū)域數(shù)量等等,如果你放開了此配置谆棺,意味著需要打開更多的文件以及虛擬內(nèi)存栽燕,所以
我們還需要系統(tǒng)調(diào)優(yōu)。
(12)系統(tǒng)調(diào)優(yōu)
我們一共需要修改兩處
修改/etc/security/limits.conf 改淑,追加內(nèi)容
- soft nofile 65536 - hard nofile 65536
nofile是單個進程允許打開的最大文件個數(shù) soft nofile 是軟限制 hard nofile是硬限制
修改/etc/sysctl.conf碍岔,追加內(nèi)容
vm.max_map_count=655360
限制一個進程可以擁有的VMA(虛擬內(nèi)存區(qū)域)的數(shù)量
執(zhí)行下面命令 修改內(nèi)核參數(shù)馬上生效
sysctl ‐p
(13)重新啟動虛擬機,再次啟動容器朵夏,發(fā)現(xiàn)已經(jīng)可以啟動并遠(yuǎn)程訪問
7.2 IK分詞器安裝
(1)快捷鍵alt+p進入sftp , 將ik文件夾上傳至宿主機
sftp> put ‐r d:\setup\ik
(2)在宿主機中將ik文件夾拷貝到容器內(nèi) /usr/share/elasticsearch/plugins 目錄下蔼啦。
(3)重新啟動,即可加載IK分詞器
docker cp ik tensquare_elasticsearch:/usr/share/elasticsearch/plugins/
(3)重新啟動仰猖,即可加載IK分詞器
docker restart tensquare_elasticsearch
7.3 HEAD插件安裝
(1)修改/usr/share/elasticsearch.yml ,添加允許跨域配置
http.cors.enabled: true
http.cors.allow‐origin: "*"
(2)重新啟動elasticseach容器
(3)下載head鏡像(此步省略)
docker pull mobz/elasticsearch‐head:5
(4)創(chuàng)建head容器
docker run ‐di ‐‐name=myhead ‐p 9100:9100 docker pull mobz/elasticsearch‐
head:5