PgVectorStore: creating an index only if missing

- Resolve an issue where a new index keeps getting created during application start up.
 - Solution is is to create an index only if an index on the embedding column does not exist.
 - Add missing index name.
This commit is contained in:
Sagar Bhat
2024-04-22 18:43:45 +10:00
committed by Christian Tzolov
parent db6f7cda3e
commit 773b7bd3ca

View File

@@ -60,6 +60,8 @@ public class PgVectorStore implements VectorStore, InitializingBean {
public static final String VECTOR_TABLE_NAME = "vector_store";
public static final String VECTOR_INDEX_NAME = "spring_ai_vector_index";
public final FilterExpressionConverter filterExpressionConverter = new PgVectorFilterExpressionConverter();
private final JdbcTemplate jdbcTemplate;
@@ -352,8 +354,8 @@ public class PgVectorStore implements VectorStore, InitializingBean {
if (this.createIndexMethod != PgIndexType.NONE) {
this.jdbcTemplate.execute(String.format("""
CREATE INDEX ON %s USING %s (embedding %s)
""", VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
CREATE INDEX IF NOT EXISTS %s ON %s USING %s (embedding %s)
""", VECTOR_INDEX_NAME, VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
}
}