Skip to content

Add custom text-property and set default to previously used "text" #2959

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

Closed
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 @@ -75,6 +75,7 @@ public Neo4jVectorStore vectorStore(Driver driver, EmbeddingModel embeddingModel
.indexName(properties.getIndexName())
.idProperty(properties.getIdProperty())
.constraintName(properties.getConstraintName())
.textProperty(properties.getTextProperty())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class Neo4jVectorStoreProperties extends CommonVectorStoreProperties {

private String constraintName = Neo4jVectorStore.DEFAULT_CONSTRAINT_NAME;

private String textProperty = Neo4jVectorStore.DEFAULT_TEXT_PROPERTY;

public String getDatabaseName() {
return this.databaseName;
}
Expand Down Expand Up @@ -111,4 +113,11 @@ public void setConstraintName(String constraintName) {
this.constraintName = constraintName;
}

public String getTextProperty() {
return this.textProperty;
}

public void setTextProperty(String textProperty) {
this.textProperty = textProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public class Neo4jVectorStore extends AbstractObservationVectorStore implements

public static final String DEFAULT_ID_PROPERTY = "id";

public static final String DEFAULT_TEXT_PROPERTY = "text";

public static final String DEFAULT_CONSTRAINT_NAME = DEFAULT_LABEL + "_unique_idx";

private static final Map<Neo4jDistanceType, VectorStoreSimilarityMetric> SIMILARITY_TYPE_MAPPING = Map.of(
Expand All @@ -172,6 +174,8 @@ public class Neo4jVectorStore extends AbstractObservationVectorStore implements

private final String idProperty;

private final String textProperty;

private final String constraintName;

private final Neo4jVectorFilterExpressionConverter filterExpressionConverter = new Neo4jVectorFilterExpressionConverter();
Expand All @@ -192,6 +196,7 @@ protected Neo4jVectorStore(Builder builder) {
this.indexNameNotSanitized = builder.indexName;
this.indexName = SchemaNames.sanitize(builder.indexName, true).orElseThrow();
this.idProperty = SchemaNames.sanitize(builder.idProperty).orElseThrow();
this.textProperty = SchemaNames.sanitize(builder.textProperty).orElseThrow();
this.constraintName = SchemaNames.sanitize(builder.constraintName).orElseThrow();
this.initializeSchema = builder.initializeSchema;
}
Expand Down Expand Up @@ -323,7 +328,7 @@ private Map<String, Object> documentToRecord(Document document, float[] embeddin
row.put("id", document.getId());

var properties = new HashMap<String, Object>();
properties.put("text", document.getText());
properties.put(this.textProperty, document.getText());

document.getMetadata().forEach((k, v) -> properties.put("metadata." + k, Values.value(v)));
row.put("properties", properties);
Expand All @@ -345,7 +350,7 @@ private Document recordToDocument(org.neo4j.driver.Record neoRecord) {

return Document.builder()
.id(node.get(this.idProperty).asString())
.text(node.get("text").asString())
.text(node.get(this.textProperty).asString())
.metadata(Map.copyOf(metaData))
.score((double) score)
.build();
Expand Down Expand Up @@ -411,6 +416,8 @@ public static class Builder extends AbstractVectorStoreBuilder<Builder> {

private String idProperty = DEFAULT_ID_PROPERTY;

private String textProperty = DEFAULT_TEXT_PROPERTY;

private String constraintName = DEFAULT_CONSTRAINT_NAME;

private boolean initializeSchema = false;
Expand Down Expand Up @@ -516,6 +523,18 @@ public Builder idProperty(String idProperty) {
return this;
}

/**
* Sets the property name for text-content.
* @param textProperty the text property to use
* @return the builder instance
*/
public Builder textProperty(String textProperty) {
if (StringUtils.hasText(textProperty)) {
this.textProperty = textProperty;
}
return this;
}

/**
* Sets the name of the unique constraint.
* @param constraintName the constraint name to use
Expand Down
Loading