版本elasticsearch 7.6.1
elasticsearch/config/elasticsearch.yaml文件中添加如下内容
# ---------------------------------- Auth -------------------------------------
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
在elasticsearch bin目录下使用如下命令
./elasticsearch-setup-passwords interactive
设置密码,必须大于6位
elastic: admin1
apm_system: admin1
kibana: admin1
logstash_system: admin1
beats_system: admin1
remote_monitoring_user: admin1
进入kibana/config/kibana.yaml文件中修改
elasticsearch.username = elastic
elasticsearch.password = admin1
java代码中,配置一个config,使用如下代码,可以全局配置RestHighLevelClient,也可以RestHighLevelClient,执行crud时来进行设置.
application.yaml配置文件添加
elasticsearch: clientIps: 192.168.220.130 httpPort: 9200 username: elastic password: admin1 #查询时返回值在堆中的最大值,如果超过此大小,抛出异常,单位为M heapSize: 100
ElasticClientConfig.java 配置文件
import cn.hutool.core.codec.Base64;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticClientConfig {
@Value("${elasticsearch.clientIps}")
private String clientIps;
@Value("${elasticsearch.httpPort}")
private int httpPort;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
@Value("${elasticsearch.heapSize}")
private Integer heapSize;
public static RequestOptions COMMON_OPTIONS=null;
void initRequestOptions (){
String TOKEN = Base64.encode(username+":"+password) ;
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
builder.addHeader("Authorization","Basic "+TOKEN);
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(heapSize*1024*1024));
COMMON_OPTIONS = builder.build();
}
private HttpHost[] getHttpHosts(String clientIps, int esHttpPort) {
String[] clientIpList = clientIps.split(",");
HttpHost[] httpHosts = new HttpHost[clientIpList.length];
for (int i = 0; i < clientIpList.length; i++) {
httpHosts[i] = new HttpHost(clientIpList[i], esHttpPort, "http");
}
return httpHosts;
}
/**
* 创建带HTTP Basic Auth认证rest客户端
*/
@Bean
public RestHighLevelClient restHighLevelClient() {
initRequestOptions ();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort))
.setHttpClientConfigCallback(
(HttpAsyncClientBuilder httpAsyncClientBuilder) ->
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
);
}
//不带用户名密码验证
// @Bean
public RestHighLevelClient restClient() {
initRequestOptions ();
return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort)));
}
}
查询时指定需要的配置:
/**
*
* @param request
* @throws Exception
*/
private void search(SearchRequest request) throws Exception{
SearchResponse response = restHighLevelClient.search(request,ElasticClientConfig.COMMON_OPTIONS);
//将返回的数据返回到SearchHits对象中
SearchHits hits = response.getHits();
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String documentTitle = (String) sourceAsMap.get("title");
}
}