You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds a fail_if_exist option to index creation functions (#180)
* Updated changelog
* Added a fail_if_exists option to create_fulltext_index and create_vector_index
* Updated test_indexes.py unit tests
* Added tests to test_indexes.py to test fail_if_exists parameters
* Replace double quotes with single quotes in f strings
* Updated changelog
Copy file name to clipboardExpand all lines: src/neo4j_graphrag/indexes.py
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,7 @@ def create_vector_index(
38
38
embedding_property: str,
39
39
dimensions: int,
40
40
similarity_fn: Literal["euclidean", "cosine"],
41
+
fail_if_exists: bool=False,
41
42
neo4j_database: Optional[str] =None,
42
43
) ->None:
43
44
"""
@@ -46,7 +47,6 @@ def create_vector_index(
46
47
47
48
See Cypher manual on `creating vector indexes <https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/vector-indexes/#create-vector-index>`_.
48
49
49
-
Important: This operation will fail if an index with the same name already exists.
50
50
Ensure that the index name provided is unique within the database context.
51
51
52
52
Example:
@@ -72,6 +72,7 @@ def create_vector_index(
72
72
embedding_property="vectorProperty",
73
73
dimensions=1536,
74
74
similarity_fn="euclidean",
75
+
fail_if_exists=False,
75
76
)
76
77
77
78
@@ -83,6 +84,7 @@ def create_vector_index(
83
84
dimensions (int): Vector embedding dimension
84
85
similarity_fn (str): case-insensitive values for the vector similarity function:
85
86
``euclidean`` or ``cosine``.
87
+
fail_if_exists (bool): If True raise an error if the index already exists. Defaults to False.
86
88
neo4j_database (Optional[str]): The name of the Neo4j database. If not provided, this defaults to "neo4j" in the database (`see reference to documentation <https://neo4j.com/docs/operations-manual/current/database-administration/#manage-databases-default>`_).
87
89
88
90
Raises:
@@ -105,7 +107,7 @@ def create_vector_index(
105
107
106
108
try:
107
109
query= (
108
-
f"CREATE VECTOR INDEX $name FOR (n:{label}) ON n.{embedding_property} OPTIONS "
110
+
f"CREATE VECTOR INDEX $name {''iffail_if_existselse'IF NOT EXISTS'}FOR (n:{label}) ON n.{embedding_property} OPTIONS "
logger.info(f"Creating vector index named '{name}'")
@@ -123,6 +125,7 @@ def create_fulltext_index(
123
125
name: str,
124
126
label: str,
125
127
node_properties: list[str],
128
+
fail_if_exists: bool=False,
126
129
neo4j_database: Optional[str] =None,
127
130
) ->None:
128
131
"""
@@ -131,7 +134,6 @@ def create_fulltext_index(
131
134
132
135
See Cypher manual on `creating fulltext indexes <https://neo4j.com/docs/cypher-manual/current/indexes/semantic-indexes/full-text-indexes/#create-full-text-indexes>`_.
133
136
134
-
Important: This operation will fail if an index with the same name already exists.
135
137
Ensure that the index name provided is unique within the database context.
136
138
137
139
Example:
@@ -155,6 +157,7 @@ def create_fulltext_index(
155
157
INDEX_NAME,
156
158
label="Document",
157
159
node_properties=["vectorProperty"],
160
+
fail_if_exists=False,
158
161
)
159
162
160
163
@@ -163,6 +166,7 @@ def create_fulltext_index(
163
166
name (str): The unique name of the index.
164
167
label (str): The node label to be indexed.
165
168
node_properties (list[str]): The node properties to create the fulltext index on.
169
+
fail_if_exists (bool): If True raise an error if the index already exists. Defaults to False.
166
170
neo4j_database (Optional[str]): The name of the Neo4j database. If not provided, this defaults to "neo4j" in the database (`see reference to documentation <https://neo4j.com/docs/operations-manual/current/database-administration/#manage-databases-default>`_).
167
171
168
172
Raises:
@@ -180,7 +184,7 @@ def create_fulltext_index(
180
184
181
185
try:
182
186
query= (
183
-
"CREATE FULLTEXT INDEX $name "
187
+
f"CREATE FULLTEXT INDEX $name{''iffail_if_existselse'IF NOT EXISTS'} "
0 commit comments