本文來自於千鋒教育在阿里雲開發者社區學習中心上線課程《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.gz
3.解壓到/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"
執行結果: