1、pom依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.14.0</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version> <!-- 使用你所需的版本 -->
</dependency>
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>2.0.1</version>
</dependency>
2、ES 8.1的相关接口 主要参考: Java封装Elasticsearch8常用接口方法(一)_elasticsearch8 java接口-CSDN博客
3、EsService 主要 增加knnSearch 和 混合检索 mixSearch
/**
* KNN搜索
*
* @param indexName 索引名称
* @param vector 向量数据
* @param field KNN字段名
* @return KnnSearchResponse
* @throws Exception
*/
public SearchResponse knnSearchTestTwo(String indexName, List<Float> vector, String field, List<String> list) throws Exception {
ElasticsearchClient client = ESClientPool.getClient();
KnnQuery knnQuery = KnnQuery.of(m -> m.field(field).queryVector(vector).numCandidates(100));
SearchResponse<ObjectNode> response = client.search(s -> s
.index(indexName)
.query(knnQuery._toQuery())
.source(SourceConfig.of(sf -> sf.filter(f -> f.includes(list))))
, ObjectNode.class);
List<ObjectNode> result = new ArrayList<>();
for (Hit<ObjectNode> hit : response.hits().hits()) {
ObjectNode source = hit.source();
source.put("score", hit.score());
result.add(source);
}
log.info("SearchResponse: {}", JSON.toJSONString(result));
ESClientPool.returnClient(client);
return response;
}
/**
* 混合查询测试
*
* @param indexName 索引名称
* @param searchText 查询文本
* @param vector 向量
* @param knnField KNN字段名
* @return SearchResponse
* @throws Exception
*/
public SearchResponse mixSearch(String indexName, String searchText, String searchField, List<Float> vector, String knnField, List<String> returnFields) throws Exception {
ElasticsearchClient client = ESClientPool.getClient();
MatchQuery searchTextQuery = new MatchQuery.Builder().field(searchField).query(searchText).build();
KnnQuery knnQuery = new KnnQuery.Builder()
.field(knnField)
.queryVector(vector)
.numCandidates(100)
.build();
// 创建must子句的bool查询,这里只有一个should子句
BoolQuery boolQuery = new BoolQuery.Builder()
.must(searchTextQuery._toQuery()) // 必须满足的文本匹配
.should(knnQuery._toQuery()) // 可以满足的向量相似性
.build();
SearchResponse<ObjectNode> response = client.search(s -> s
.index(indexName)
.query(boolQuery._toQuery())
.source(SourceConfig.of(sf -> sf.filter(f -> f.includes(returnFields))))
, ObjectNode.class);
ESClientPool.returnClient(client);
return response;
}