下载
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