準(zhǔn)備
jdk1.8
elasticsearch 5.5.2
springboot 1.5.6RELEASE
maven 3.5.0
node.js 8.11.4
postman工具
jdk,maven,node,postman的安裝就不管了
1、安裝elasticsearch
下載壓縮包
到elastic官網(wǎng):https://www.elastic.co/cn/downloads/past-releases下載
我這里下載的是5.5.2windows版本的

修改配置文件
解壓后,修改config文件夾下的配置文件jvm.options,默認(rèn)jvm的內(nèi)容是2g,我修改為256m,測(cè)試嘛我就搞小點(diǎn),不改也行,只要電腦的內(nèi)存夠用就行

修改之后

啟動(dòng)
在bin目錄下,點(diǎn)擊elasticsearch.bat文件啟動(dòng),看到關(guān)鍵字started就是啟動(dòng)成功了

elastic默認(rèn)端口是9200,到瀏覽器訪問(wèn)localhost:9200

2、elasticsearch的head插件(es界面)
下載
下載地址:https://github.com/mobz/elasticsearch-head
啟動(dòng)
進(jìn)入elasticsearch-head-master文件夾
下載依賴: npm install
啟動(dòng):npm run start

訪問(wèn)localhost:9100

結(jié)果是未連接,暫時(shí)不知道什么原因,以后知道了再補(bǔ)充上,但是建立起elasticsearch的集群后他就連接成功了,就先放著了,接著看安裝elastic的集群搭建
3、elasticsearch集群安裝
主節(jié)點(diǎn)
修改配置文件elasticsearch.yml,將下面這些配置添加進(jìn)去
#指定集群的名稱(chēng)
cluster.name: syf
#節(jié)點(diǎn)名稱(chēng)
node.name: master
#是不是主節(jié)點(diǎn)
node.master: true
node.attr.rack: r1
#最大集群節(jié)點(diǎn)數(shù)
node.max_local_storage_nodes: 3
#網(wǎng)關(guān)地址
network.host: 127.0.0.1
#端口
http.port: 9200
#內(nèi)部節(jié)點(diǎn)之間溝通端口
transport.tcp.port: 9300
# 開(kāi)啟安全防護(hù)(啟用跨域訪問(wèn))
http.cors.enabled: true
http.cors.allow-origin: "*"
#時(shí)間放長(zhǎng),防止腦裂
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
#配置有機(jī)會(huì)參與選舉為master的節(jié)點(diǎn)
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
#elasticSearch服務(wù)器的數(shù)據(jù)目錄和日志目錄,可以自行新建后配置路徑使elasticSearch服務(wù)啟動(dòng)后將運(yùn)行數(shù)據(jù)和日志生成到指定目錄下
path.data: E:\syf\down\elasticsearch-5.5.2\data
path.logs: E:\syf\down\elasticsearch-5.5.2\logs
其中path.data和path.logs換成你自己的路徑,
從節(jié)點(diǎn)
將主節(jié)點(diǎn)復(fù)制兩份作為兩個(gè)從節(jié)點(diǎn)

分別修改elasticsearch.yml配置文件
#指定集群的名稱(chēng)
cluster.name: syf
#節(jié)點(diǎn)名稱(chēng)
node.name: slave1
#是不是主節(jié)點(diǎn)
node.master: false
node.attr.rack: r1
#最大集群節(jié)點(diǎn)數(shù)
node.max_local_storage_nodes: 3
#網(wǎng)關(guān)地址
network.host: 127.0.0.1
#端口
http.port: 9201
#內(nèi)部節(jié)點(diǎn)之間溝通端口
transport.tcp.port: 9301
# 開(kāi)啟安全防護(hù)(啟用跨域訪問(wèn))
http.cors.enabled: true
http.cors.allow-origin: "*"
#時(shí)間放長(zhǎng),防止腦裂
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
#配置有機(jī)會(huì)參與選舉為master的節(jié)點(diǎn)
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
#elasticSearch服務(wù)器的數(shù)據(jù)目錄和日志目錄,可以自行新建后配置路徑使elasticSearch服務(wù)啟動(dòng)后將運(yùn)行數(shù)據(jù)和日志生成到指定目錄下
path.data: E:\syf\down\elasticsearch-5.5.2.1\data
path.logs: E:\syf\down\elasticsearch-5.5.2.1\logs
#指定集群的名稱(chēng)
cluster.name: syf
#節(jié)點(diǎn)名稱(chēng)
node.name: slave2
#是不是主節(jié)點(diǎn)
node.master: false
node.attr.rack: r1
#最大集群節(jié)點(diǎn)數(shù)
node.max_local_storage_nodes: 3
#網(wǎng)關(guān)地址
network.host: 127.0.0.1
#端口
http.port: 9202
#內(nèi)部節(jié)點(diǎn)之間溝通端口
transport.tcp.port: 9302
# 開(kāi)啟安全防護(hù)(啟用跨域訪問(wèn))
http.cors.enabled: true
http.cors.allow-origin: "*"
#時(shí)間放長(zhǎng),防止腦裂
discovery.zen.ping_timeout: 120s
client.transport.ping_timeout: 60s
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
#配置有機(jī)會(huì)參與選舉為master的節(jié)點(diǎn)
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
#elasticSearch服務(wù)器的數(shù)據(jù)目錄和日志目錄,可以自行新建后配置路徑使elasticSearch服務(wù)啟動(dòng)后將運(yùn)行數(shù)據(jù)和日志生成到指定目錄下
path.data: E:\syf\down\elasticsearch-5.5.2.2\data
path.logs: E:\syf\down\elasticsearch-5.5.2.2\logs
啟動(dòng)集群
依次啟動(dòng)三個(gè)節(jié)點(diǎn),再訪問(wèn)localhost:9100,直接訪問(wèn)可能還得不到效果,需要等一會(huì)兒,這里我在搭建的時(shí)候等了大概3分鐘,所有節(jié)點(diǎn)才啟動(dòng)了完了,

4、elasticsearch的CRUD
直接使用head插件進(jìn)行操作的話,似乎只能進(jìn)行創(chuàng)建索引這樣的操作,所以我們使用工具,postman,elasticsearch提供了restful的api,postman是一個(gè)調(diào)試接口的利器
在進(jìn)行crud操作前,先說(shuō)明幾個(gè)概念
| 概念 | 描述 |
|---|---|
| 索引(index) | 可以簡(jiǎn)單理解為數(shù)據(jù)庫(kù)中的 庫(kù)(database) |
| 類(lèi)型(type) | 就相當(dāng)于數(shù)據(jù)庫(kù)中 表(table) |
| 文檔(doc) | 就相當(dāng)于一條數(shù)據(jù) |
| 分片 | es創(chuàng)建索引時(shí)默認(rèn)5個(gè)分片,5個(gè)分片共同存放一個(gè)索引 |
| 備份 | es默認(rèn)一個(gè)分片一個(gè)備份 |
打開(kāi)postman創(chuàng)建一個(gè)book索引,其中給一個(gè)novel類(lèi)型并定義文檔的結(jié)構(gòu)

創(chuàng)建索引的json
{
"settings":{
"number_of_shards":5,
"number_of_replicas":1
},
"mappings":{
"novel":{
"properties":{
"title":{
"type":"text"
},
"price":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd"
}
}
}
}
}
下圖表示成功

訪問(wèn)localhost:9100

其中細(xì)線的代表備份,比如細(xì)線0為粗線0的備份,以此類(lèi)推
增加一個(gè)文檔


修改文檔


書(shū)名成功修改為了西游記
獲取文檔


刪除文檔

5、springboot整合elasticsearch
創(chuàng)建一個(gè)maven項(xiàng)目
目錄結(jié)構(gòu)

pom依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<elasticsearch.version>5.5.2</elasticsearch.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
Config.java
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@SuppressWarnings("resource")
@Bean
public TransportClient getTransportClient() throws UnknownHostException{
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName("localhost"),
9300//9300是elastic的transport使用的端口
);
Settings settings = Settings.builder().put("cluster.name","syf").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
}
BookController.java
@RestController
@RequestMapping("/book")
public class BookController {
@Autowired
private TransportClient client;
@RequestMapping("/novel/get")
public Object get(String id){
GetResponse result = client.prepareGet("book","novel",id).get();
return result.getSource();
}
@RequestMapping("/novel/add")
public Object add(String title,String price,String date){
try {
XContentBuilder content = XContentFactory.jsonBuilder()
.startObject()
.field("title",title)
.field("price",price)
.field("date",date)
.endObject();
IndexResponse result = client.prepareIndex("book", "novel").setSource(content).get();
return result.getResult();
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@RequestMapping("/novel/delete")
public Object delete(String id){
DeleteResponse result = client.prepareDelete("book", "novel", id).get();
return result.getResult();
}
@RequestMapping("/novel/update")
public Object update(String id,String title,String price,String date){
UpdateRequest update = new UpdateRequest("book", "novel", id);
try {
XContentBuilder content = XContentFactory.jsonBuilder().startObject();
content.field("title", title).field("price",price).field("date",date);
content.endObject();
update.doc(content);
UpdateResponse result = client.update(update).get();
return result.getResult();
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@RequestMapping("/novel/query")
public Object query(String title,String price){
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(title!=null){
boolQuery.must(QueryBuilders.matchQuery("title", title));
}
SearchRequestBuilder search = client.prepareSearch("book").setTypes("novel")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(boolQuery);
List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
SearchResponse response = search.get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
result.add(hit.getSource());
}
return result;
}
}
springboot默認(rèn)監(jiān)聽(tīng)8080,增刪改查自己測(cè)試,我測(cè)試過(guò)是可以的
完整代碼下載地址:https://gitee.com/shyf2019/springboot_elasticsearch