下載
1、wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz
解壓
2、tar -zxvf elasticsearch-6.4.0.tar.gz
修改配置
cd /usr/local/apps/elasticsearch-6.4.0/config
vi elasticsearch.yml
需要改的內容:
# 集羣的名稱
cluster.name: test-cluster
# 當前節點的名稱
node.name: node-1
# ES數據存放地址
path.data: /usr/local/apps/elasticsearch-6.4.0/data
# ES日誌存放地址
path.logs: /usr/local/apps/elasticsearch-6.4.0/logs
# 當前節點的ip地址
network.host: 0.0.0.0
# ES服務的端口號
http.port: 9200
# 集羣每個節點的IP地址
# discovery.zen.ping.unicast.hosts: ["host1", "host2"]
# 集羣節點存活最少數, 建議: 總節點數/2 + 1
# discovery.zen.minimum_master_nodes:
一下是修改的系統配置:
然後 修改ES啓動內存權限
vi /etc/sysctl.conf
在文件末尾添加:
vm.max_map_count=262144
啓動es:
我們給es加了分組,
ES是不能以root用戶啓動的, 必須創建一個非root用戶來啓動ES.
# 創建用戶組es
groupadd es
# 創建用戶es, 屬於es用戶組, 密碼爲123456
useradd es -g es -p 123456
# 授予es用戶組下的es用戶, 對該路徑的讀權限
chown -R es:es /www/soft/es/elasticsearch-6.4.0/
執行完之後,java api 查詢:
<!-- 添加elasticsearch 依賴 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
package com.boot.es.utils;
import java.net.InetAddress;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
* es工具類
*
* @author zpl
*
*/
@SuppressWarnings({ "resource", "unchecked" })
public class ESUtils {
public static final String indexName = "test";// 索引名
public static final String documentType = "student";// 索引類型
public static TransportClient client = null;
static {
// ES配置
Settings settings = Settings.builder().put("cluster.name", "zpl-application")// 集羣名稱
.put("client.transport.sniff", true)// 是否自動檢測
.build();
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.179.128")// es
// 主機地址
, 9300));// 端口號
} catch (Exception e) {
e.printStackTrace();
}
}
public static TransportClient getClient() {
return client;
}
}
package com.boot.es.controller;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.boot.es.utils.ESUtils;
import com.boot.split.entity.StudentEntity;
@RestController
@RequestMapping("/es")
public class ESController {
@RequestMapping("/create/index.json")
public void createIndex() {
CreateIndexResponse response = ESUtils.getClient().admin()// admin權限
.indices()// 索引集合
.prepareCreate(ESUtils.indexName)// 索引名
.execute().actionGet();// 請求
if (response.isAcknowledged()) {
System.out.println("add Index OK");
} else {
System.out.println("add Index NO");
}
}
@RequestMapping("/create/book.json")
public void createBook() {
IndexResponse response = ESUtils.getClient().prepareIndex(ESUtils.indexName, ESUtils.documentType).get();
System.out.println(response.getResult());
}
@RequestMapping("/create/document.json")
public void insertDocument() {
StudentEntity book = new StudentEntity(1, "張三", 18);// 創建一個Book對象
IndexResponse response = ESUtils.getClient().prepareIndex(ESUtils.indexName, ESUtils.documentType)
.setSource(JSONObject.toJSONString(book), XContentType.JSON).get();
System.out.println(response.getResult());
}
@RequestMapping("/get/document.json")
public void getDocument() {
SearchResponse response = ESUtils.getClient().prepareSearch(ESUtils.indexName)
.setTypes(ESUtils.documentType).get();
System.out.println(response.toString());
}
}
查詢參考:https://blog.csdn.net/u010454030/article/details/79703649