一、前言
ES有4种客户端,分别是:Jest client、Rest client、Transport client、Node client。
ES支持两种协议
HTTP协议,支持的客户端有Jest client和Rest client
Native Elasticsearch binary协议,也就是Transport client【7.0弃用】和Node client【2.3弃用】
Jest client非官方支持,在ES5.0之前官方提供的客户端只有Transport client、Node client。在5.0之后官方发布Rest client【推荐使用】。
二、Jest client和Rest client使用
2.1、Rest client方式
sptingboot2.0版本引用jar包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.15.0</version>
</dependency>
<!-- elasticsearch -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.15.0</version>
</dependency>
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.5</version>
</dependency>
pom文件配置
#es配置
es.enable=true
es.hostUrl=**
es.port=9200
es.protocol=http
es.connectTimeOut=1000
es.socketTimeOut=30000
es.connectionRequestTimeOut=500
es.maxConnectNum=100
es.maxConnectPerRoute=100
es.username=***
es.password=***
RestHighLevelClient的config如下:文章来源:https://www.toymoban.com/news/detail-482801.html
@Configuration
public class ElasticClient {
@Value("${es.hostUrl}")
private String hostUrl;
@Value("${es.port}")
private int port;
@Value("${es.protocol}")
private String protocol;
@Value("${es.connectTimeOut}")
private int connectTimeOut;
@Value("${es.socketTimeOut}")
private int socketTimeOut;
@Value("${es.connectionRequestTimeOut}")
private int connectionRequestTimeOut;
@Value("${es.maxConnectNum}")
private int maxConnectNum;
@Value("${es.maxConnectPerRoute}")
private int maxConnectPerRoute;
@Value("${es.username}")
private String username;
@Value("${es.password}")
private String password;
@Bean(destroyMethod = "close")
public RestHighLevelClient restHighLevelClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostUrl, port, protocol));
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder;
}
});
//请求配置
restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
return new RestHighLevelClient(restClientBuilder);
}
}
三、Rest client方式
3.1、基本操作
@Data
public abstract class BaseEsDo implements Serializable {
/**
* ES的INDEX
*/
@JSONField(name = "index")
private String index;
/**
* ES唯一值ID
*/
@JSONField(name = "id")
private String id;
/**
* 创建时间
*/
@JSONField(name = "create_time", format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 修改时间
*/
@JSONField(name = "update_time", format = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 文档状态
*/
@JSONField(name = "doc_state")
private Boolean docState;
/**
* 文档版本
*/
@JSONField(name = "doc_version")
private Integer docVersion;
/**
* 文档备注
*/
@JSONField(name = "doc_remarks")
private String docRemarks;
/**
* 文档查看数量
*/
@JSONField(name = "doc_view_number")
private int docViewNumber;
/**
* 文档删除状态,默认true
*/
@JSONField(name = "doc_delete")
private boolean docDelete = false;
}
AbstractElasticRepository类是封装的常见ES操作文章来源地址https://www.toymoban.com/news/detail-482801.html
@Repository
@Slf4j
public abstract class AbstractElasticRepository<T extends BaseEsDo> {
@Autowired
protected RestHighLevelClient client;
@Autowired
private JestClient jestClient; //后续研究
/**
* 全量匹配查询 【默认返回10条记录】
* @param index
* @return
*/
public <T extends BaseEsDo> List<T> queryMatchAll(String index, Class<T> clazz) throws IOException{
//1.创建 SearchRequest搜索请求
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);//指定要查询的索引
//2.创建 SearchSourceBuilder条件构造。
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(matchAllQueryBuilder);
//3.将 SearchSourceBuilder 添加到 SearchRequest中
searchRequest.source(searchSourceBuilder);
//4.执行查询
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("花费的时长:" + searchResponse.getTook());
SearchHits hits = searchResponse.getHits();
System.out.println(hits.getTotalHits());
System.out.println("符合条件的总文档数量:" + hits.getTotalHits().value);
List<T> list = Arrays.stream(hits.getHits())
.map(searchHit ->JSON.parseObject(JSON.toJSONString(searchHit.getSourceAsMap()), clazz))
.collect(Collectors.toList());
return list;
}
/**
* 分页查询、倒序或降序、指定需要返回或者排除的字段
* sortMap:不能传多个,只能传一个排序
* @throws IOException
*/
public <T extends BaseEsDo> List<T> pageQuery(String index, int from, int size, Map<
到了这里,关于【ES】---ES的基本操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!