Skip to content

feat: add pg hybridSearch #1097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ default List<Document> similaritySearch(String query) {
return this.similaritySearch(SearchRequest.query(query));
}

/**
* Retrieves documents by query embedding similarity using the default, If the
* subclass implements this method, use hybrid search, similar search + full text
* search. {@link SearchRequest}'s' search criteria.
* @param request request for set search parameters, such as the query text, topK,
* similarity threshold and metadata filter expressions.
* @return Returns a list of documents that have embeddings similar to the query text
*/
default List<Document> hybridSearch(SearchRequest request) {
return this.similaritySearch(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,23 @@ public void addAndSearch() {

vectorStore.add(documents);

List<Document> results = vectorStore
List<Document> similarityResults = vectorStore
.similaritySearch(SearchRequest.query("What is Great Depression?").withTopK(1));
assertThat(similarityResults).hasSize(1);
Document similarityResultDoc = similarityResults.get(0);
assertThat(similarityResultDoc.getId()).isEqualTo(documents.get(2).getId());
assertThat(similarityResultDoc.getMetadata()).containsKeys("depression", "distance");

assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(documents.get(2).getId());
assertThat(resultDoc.getMetadata()).containsKeys("depression", "distance");
List<Document> hybridResults = vectorStore
.hybridSearch(SearchRequest.query("What is Great Depression?").withTopK(1));
assertThat(hybridResults).hasSize(2);
Document hybridResultDoc = hybridResults.get(1);
assertThat(hybridResultDoc.getMetadata()).containsKeys("depression");

// Remove all documents from the store
vectorStore.delete(documents.stream().map(doc -> doc.getId()).toList());
results = vectorStore.similaritySearch(SearchRequest.query("Great Depression").withTopK(1));
assertThat(results).hasSize(0);
similarityResults = vectorStore.similaritySearch(SearchRequest.query("Great Depression").withTopK(1));
assertThat(similarityResults).hasSize(0);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ public List<Document> similaritySearch(SearchRequest request) {
new DocumentRowMapper(this.objectMapper), queryEmbedding, queryEmbedding, distance, request.getTopK());
}

@Override
public List<Document> hybridSearch(SearchRequest searchRequest) {

List<Document> similaritySearch = similaritySearch(searchRequest);

String sql = "SELECT *, ts_rank_cd(to_tsvector(content), plainto_tsquery(?)) AS rank FROM "
+ getFullyQualifiedTableName() + " ORDER BY rank DESC LIMIT ?";

List<Document> keyLikeSearch = this.jdbcTemplate.query(sql, new DocumentRowMapper(this.objectMapper, true),
searchRequest.getQuery(), searchRequest.getTopK());

similaritySearch.addAll(keyLikeSearch);
return similaritySearch;
}

public List<Double> embeddingDistance(String query) {
return this.jdbcTemplate.query(
"SELECT embedding " + this.comparisonOperator() + " ? AS distance FROM " + getFullyQualifiedTableName(),
Expand Down Expand Up @@ -425,21 +440,28 @@ private static class DocumentRowMapper implements RowMapper<Document> {

private ObjectMapper objectMapper;

private boolean isHybrid = false;

public DocumentRowMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

public DocumentRowMapper(ObjectMapper objectMapper, boolean isHybrid) {
this.objectMapper = objectMapper;
this.isHybrid = isHybrid;
}

@Override
public Document mapRow(ResultSet rs, int rowNum) throws SQLException {
String id = rs.getString(COLUMN_ID);
String content = rs.getString(COLUMN_CONTENT);
PGobject pgMetadata = rs.getObject(COLUMN_METADATA, PGobject.class);
PGobject embedding = rs.getObject(COLUMN_EMBEDDING, PGobject.class);
Float distance = rs.getFloat(COLUMN_DISTANCE);

Map<String, Object> metadata = toMap(pgMetadata);
metadata.put(COLUMN_DISTANCE, distance);

if (!isHybrid) {
Float distance = rs.getFloat(COLUMN_DISTANCE);
metadata.put(COLUMN_DISTANCE, distance);
}
Document document = new Document(id, content, metadata);
document.setEmbedding(toDoubleList(embedding));

Expand Down