本文來自于千鋒教育在阿裡雲開發者社群學習中心上線課程《SpringBoot實戰教程》,主講人楊紅豔,
點選檢視視訊内容。
ElasticSearch概述及安裝
ElasticSearch是一個基于Lucene的搜尋伺服器。它提供了一個分布式多使用者能力的全文搜尋引擎,基于RESTful web接口。Elasticsearch是用Java開發的,并作為Apache許可條款下的開放源碼釋出,是目前流行的企業級搜尋引擎。用于
雲計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用友善。
安裝
1.首先安裝好jdk.
2.從Elastic的官網下載下傳ES的安裝包
https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.4/elasticsearch-2.4.4.tar.gz3.解壓到/usr/local下
tar -xzvf elasticsearch-2.4.4.tar.gz -C /usr/local
4.編輯配置檔案:
/config/elasticsearch.yml
bootstrap.memory_lock: true
index.cache.field.max_size: 50000
index.cache.field.expire: 30m
index.cache.field.type: soft
network.host: 192.168.*.25,10.29.*.58,127.0.0.1
action.disable_delete_all_indices : true
5.啟動:啟動時不要使用root使用者,會報錯
java.lang.RuntimeException: don't run elasticsearch as root
使用普通使用者啟動會顯示權限不夠,解決辦法:
将ElasticSearch的安裝目錄及其子目錄改為另外一個非root賬戶,如:
sudo chown -R linux elasticsearch-2.4.4
sudo chgrp -R linux elasticsearch-2.4.4
使用浏覽器進行通路:

出現上述json格式,證明可以正常使用了。
Elasticsearch實作資料的存儲和檢索
在Elasticsearch中,所有的資料,都以文檔的形式存儲,每個文檔都有定義好的索引和類型。每個文檔可以包含一個或多個字段來儲存資料。
我們借助sence來實作存儲檢索。
在sence中,輸入如下的curl請求代碼:
curl -XPUT '192.168.25.129:9200/userindex/user/3?pretty' -d'
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 23,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
結果:
從Elasticsearch中檢索文檔:
在sence中,輸入如下的curl請求代碼:
curl -XGET "http://192.168.25.129:9200/userindex/user/1"
原輸入的資訊:
檢索到的文檔:
搜尋所有使用者:
curl -XGET "http://192.168.25.129:9200/userindex/user/_search"
執行結果:
查詢某個符合條件的:
curl -XGET "http://192.168.25.129:9200/userindex/user/_search?q=age:32"