- 下载安装包
- ES安装
- java测试样例
es官网下载 https://www.elastic.co/cn/downloads/elasticsearch
随心情选择一个版本即可
- 新增用户,es不支持root用户操作
useradd es passwd es
- 修改系统配置
vi /etc/sysctl.conf
在最后增加vm.max_map_count=262144,保存退出
vi /etc/security/limits.conf
修改或增加以下内容
* hard nofile 65536 * soft nofile 65536 * soft nproc 2048 * hard nproc 4096
-
切换到es用户安装es
将从官网下载的压缩包上传到服务器并解压缩 -
更改es配置
es安装目录下config文件夹,修改elasticsearch.yml文件。
network.host: 0.0.0.0 #也可换成内网ip,不可使用公网ip network.publish_host: xx.xx.xx.xx #公网ip http.port: 9200 #默认端口9200 cluster.name: es-study #集群名称,随便起一个就行
- 启动es
进入到根目录下bin文件夹
nohup ./elasticsearch >1.log 2>&1 &
打开1.log文件,能看到当前的启动日志,看到started基本就好啦
[2021-12-29T13:42:21,325][DEBUG][o.e.a.ActionModule ] Using REST wrapper from plugin org.elasticsearch.xpack.security.Security
[2021-12-29T13:42:21,703][INFO ][o.e.d.DiscoveryModule ] [node-1] using discovery type [single-node]
[2021-12-29T13:42:22,518][INFO ][o.e.n.Node ] [node-1] initialized
[2021-12-29T13:42:22,518][INFO ][o.e.n.Node ] [node-1] starting ...
[2021-12-29T13:42:22,694][INFO ][o.e.t.TransportService ] [node-1] publish_address {公网ip:9300}, bound_addresses {内网ip:9300}
[2021-12-29T13:42:22,812][INFO ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [node-1] publish_address {公网ip:9200}, bound_addresses {内网ip:9200}
[2021-12-29T13:42:22,812][INFO ][o.e.n.Node ] [node-1] started
[2021-12-29T13:42:23,241][INFO ][o.e.c.s.ClusterSettings ] [node-1] updating [xpack.monitoring.collection.enabled] from [false] to [true]
[2021-12-29T13:42:23,594][WARN ][o.e.x.s.a.s.m.NativeRoleMappingStore] [node-1] Failed to clear cache for realms [[]]
java测试样例
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name","es-study")
.build();
//构建client
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("公网ip"), 9300));
try {
SearchRequest request = new SearchRequest();
request.indices("xxx");
request.types("popular");
SearchSourceBuilder search = new SearchSourceBuilder();
search.query(QueryBuilders.matchQuery("songsName", "test"));
search.timeout(new Timevalue(2000, TimeUnit.MILLISECONDS));
request.source(search);
ActionFuture search1 = client.search(request);
SearchResponse searchResponse = search1.get();
SearchHits hits = searchResponse.getHits();
SearchHit[] hitArr = hits.getHits();
Arrays.stream(hitArr).forEach(hit -> {
System.out.println(hit.getType() + "," + hit.getScore());
System.out.println("sourceAsString:" + hit.getSourceAsString());
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
client.close();
}
}



