# ES_Study **Repository Path**: dhuarui/ES_Study ## Basic Information - **Project Name**: ES_Study - **Description**: ES_Study ES_Study - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-01-03 - **Last Updated**: 2023-03-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ES_Study es-study ES 7.15及以上版本中,RestHighLevelClient被遗弃了 co.elastic.clients elasticsearch-java 8.1.0 jakarta.json jakarta.json-api 2.0.1 # elasticsearch spring.elasticsearch.rest.uris=http://192.168.172.187:9108 /** * 配置 RestHighLevelClient */ @Configuration public class ElasticSearchConfig { @Value("${spring.elasticsearch.rest.uris}") private String[] uris; @Bean public ElasticsearchClient elasticsearchClient(){ // // 创建多个HttpHost HttpHost[] httpHosts = Arrays.stream(uris).map(HttpHost::create).toArray(HttpHost[]::new); RestClient restClient = RestClient.builder(httpHosts).build(); ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); ElasticsearchClient client = new ElasticsearchClient(transport); return client; } } api操作可參考 https://blog.csdn.net/anjiongyi/article/details/123391835 分页模糊查询例子 // 发送请求到es SearchResponse response = elasticsearchClient.search(s -> s .index(OPENSCA_PROJECT) .query(q -> q .bool(b -> { // 模糊查询 if (StringUtils.isNotBlank(queryOpenProjectListDTO.getNameOrUrl())) { b.should(so -> so.wildcard(w -> w.field("full_name").value("*" + queryOpenProjectListDTO.getNameOrUrl() + "*"))) .should(so -> so.wildcard(w -> w.field("from_url").value("*" + queryOpenProjectListDTO.getNameOrUrl() + "*"))); } return b; })) .from(from) .size(queryOpenProjectListDTO.getPageSize()), Map.class); terms查詢 //分页查询条件opensca_project_version response = elasticsearchClient.search(s -> s .index(OPENSCA_PROJECT_VERSION) .query(q -> q.bool(b -> { b.must(must -> must .ids(idsQueryBuilder -> idsQueryBuilder.values(homologyFileMatchProjectVersionIdList)) ); if (!CollectionUtils.isEmpty(homologyResultDTO.getLanguages())) { List languages = homologyResultDTO.getLanguages().stream().map(projectId -> FieldValue.of(projectId)).collect(Collectors.toList()); b.must(must ->must.terms(term -> term.field("languages.language").terms(new TermsQueryField.Builder().value(languages).build()))); } //增加用户模糊查询的projectIdList条件 if (!CollectionUtils.isEmpty(projectIdList)) { List projectIds = projectIdList.stream().map(projectId -> FieldValue.of(projectId)).collect(Collectors.toList()); b.must(must -> must .terms(term -> term .field("hash_project") .terms(new TermsQueryField.Builder() .value(projectIds).build()))); } return b; })) .from((homologyResultDTO.getPageNum() - 1) * homologyResultDTO.getPageSize()) .size(homologyResultDTO.getPageSize()), Map.class);