一 : curl 连接ES服务器
Windows 安装curl,配置环境
https://jingyan.baidu.com/article/e3c78d64897d303c4c85f52d.html
这里说的很清楚,但是注意一点配置完环境变量是要重启电脑,才会生效的。
重启之后打开,命令行,
curl -u username:password url地址
返回如上内容表示连通。
二 常用curl es语句
1 查看全部内容
浏览器查看 (没有密码)
http://192.168.79.131:9200/_search?pretty
有密码的
curl -u username:password xx.xxx.xxx.xxx:xxxx/_search?pretty
2 查询某个库 全局搜之后看index; index就是索引库名字 type就是类型
//查询索引库shb01中的类型student
浏览器:http://192.168.79.131:9200/shb01/student/_search?pretty
命令行 curl -XGET http://192.168.79.131:9200/shb01/student/_search?pretty
3 :查询文档1中的数据
文档就是 _id:1 那个是文档
http://192.168.79.131:9200/shb01/student/1?pretty
http://192.168.79.131:9200/shb01/student/1?_source&pretty
两者结果一样
4:根据条件查询
q= 后面加查询条件
name:zs 找name为zs的
浏览器:http://192.168.79.131:9200/shb01/student/_search?q=name:zs&pretty
查询name为zs的数据
5:查询集群状态
Curl –XGET http://192.168.79.131:9200/_cluster/health?pretty
http://192.168.79.131:9200/_cluster/health?pretty
增删都可以用curl来实现 详细看下面的链接
https://www.cnblogs.com/jing1617/p/8060421.html
这个链接说的很清楚
二 ++ 使用postman来联通
username:password@10.203.111.119:9200/_cat/indices
三 Java 连接 es 使用
Java High Level REST Client
rest client 无论是 low level 还是 high level 都是比较主流常用的连接方式。
https://www.cnblogs.com/ginb/p/8716485.html
ESConfig.java
package xxx.xxx.common.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.ArrayList;
import java.util.List;
@Configuration
@Slf4j
public class ESConfig {
@Value("${elasticsearch.nodes}")
private String clusterNodes;
@Value("${elasticsearch.schema}")
private String schema;
@Value("${elasticsearch.max-connect-total}")
private Integer maxConnectTotal;
@Value("${elasticsearch.max-connect-per-route}")
private Integer maxConnectPerRoute;
@Value("${elasticsearch.connection-request-timeout-millis}")
private Integer connectionRequestTimeoutMillis;
@Value("${elasticsearch.socket-timeout-millis}")
private Integer socketTimeoutMillis;
@Value("${elasticsearch.connect-timeout-millis}")
private Integer connectTimeoutMillis;
@Value("${elasticsearch.username}")
private String userName;
@Value("${elasticsearch.password}")
private String password;
@Bean
@Primary
public RestHighLevelClient getRestHighLevelClient() {
List<HttpHost> httpHostList = new ArrayList<>();
if (StringUtils.isNotBlank(clusterNodes)) {
for (String address : clusterNodes.split(",")) {
String[] hostAndPort = address.split(":");
String host = hostAndPort[0];
Integer port = Integer.parseInt(hostAndPort[1]);
httpHostList.add(new HttpHost(host, port, schema));
}
}
return EsClientBuilder.build(httpHostList)
.setConnectionRequestTimeoutMillis(connectionRequestTimeoutMillis)
.setConnectTimeoutMillis(connectTimeoutMillis)
.setSocketTimeoutMillis(socketTimeoutMillis)
.setMaxConnectTotal(maxConnectTotal)
.setMaxConnectPerRoute(maxConnectPerRoute)
.setUserName(userName)
.setPassword(password)
.create();
}
}
ESClientBuilder
package xxx.common