Skip to content

Commit cbb59ce

Browse files
committed
Improve readability of the PgVector Store code
Related to #461
1 parent 811d048 commit cbb59ce

File tree

2 files changed

+12
-7
lines changed
  • spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs
  • vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore

2 files changed

+12
-7
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ vectorStore.add(List.of(document));
120120
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
121121
----
122122

123-
== Manual Configuration
123+
=== Manual Configuration
124124

125125
Instead of using the Spring Boot auto-configuration, you can manually configure the `PgVectorStore`.
126126
For this you need to add the PostgreSQL connection and `JdbcTemplate` auto-configuration dependencies to your project:

vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,20 @@ public void afterPropertiesSet() throws Exception {
342342
this.jdbcTemplate.execute("DROP TABLE IF EXISTS " + VECTOR_TABLE_NAME);
343343
}
344344

345-
this.jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS " + VECTOR_TABLE_NAME + " ( "
346-
+ "id uuid DEFAULT uuid_generate_v4 () PRIMARY KEY, " + "content text, " + "metadata json, "
347-
+ "embedding vector(" + this.embeddingDimensions() + "))");
345+
this.jdbcTemplate.execute(String.format("""
346+
CREATE TABLE IF NOT EXISTS %s (
347+
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
348+
content text,
349+
metadata json,
350+
embedding vector(%d)
351+
)
352+
""", VECTOR_TABLE_NAME, this.embeddingDimensions()));
348353

349354
if (this.createIndexMethod != PgIndexType.NONE) {
350-
this.jdbcTemplate.execute("CREATE INDEX ON " + VECTOR_TABLE_NAME + " USING " + this.createIndexMethod
351-
+ " (embedding " + this.getDistanceType().index + ")");
355+
this.jdbcTemplate.execute(String.format("""
356+
CREATE INDEX ON %s USING %s (embedding %s)
357+
""", VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
352358
}
353-
354359
}
355360

356361
int embeddingDimensions() {

0 commit comments

Comments
 (0)