Skip to content
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
17 changes: 14 additions & 3 deletions core/src/main/java/com/redis/vl/index/SearchIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -947,13 +947,24 @@ public void create(boolean overwrite) {
}

/**
* Create the index with overwrite and drop options
* Create the index with overwrite and drop options.
*
* @param overwrite Whether to overwrite an existing index
* <p>If the index already exists and {@code overwrite} is false, this attaches to the existing
* index and returns without modifying it. If {@code overwrite} is true, the existing index is
* dropped (optionally with its data) and recreated.
*
* @param overwrite Whether to overwrite an existing index; when false, an existing index is left
* intact and attached to
* @param drop Whether to drop existing data when overwriting
*/
public void create(boolean overwrite, boolean drop) {
if (overwrite && exists()) {
if (exists()) {
if (!overwrite) {
// Index already exists and overwrite is false; attach to the existing index instead
// of recreating it (matches Python's create(overwrite=False) early return).
log.info("Index {} already exists, not overwriting", getName());
return;
}
delete(drop);
}

Expand Down
34 changes: 34 additions & 0 deletions core/src/test/java/com/redis/vl/index/SearchIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ void shouldRecreateIndex() {
assertThat(searchIndex.exists()).isTrue();
}

@Test
@DisplayName("Should attach to an existing index without dropping data when not overwriting")
void shouldAttachWhenOverwriteFalseAndIndexExists() {
// Given an existing index with a document
searchIndex.create();
Map<String, Object> document = new HashMap<>();
document.put("title", "Redis in Action");
searchIndex.addDocument("doc:1", document);

// When
searchIndex.create(false);

// Then - the index is attached and the existing data is preserved
assertThat(searchIndex.exists()).isTrue();
assertThat(searchIndex.getDocumentCount()).isEqualTo(1);
}

@Test
@DisplayName("Should recreate an index without dropping data when overwriting")
void shouldRecreateWhenOverwriteTrueAndIndexExists() {
// Given an existing index with a document
searchIndex.create();
Map<String, Object> document = new HashMap<>();
document.put("title", "Redis in Action");
searchIndex.addDocument("doc:1", document);

// When
searchIndex.create(true);

// Then - the index is recreated
assertThat(searchIndex.exists()).isTrue();
assertThat(searchIndex.getDocumentCount()).isEqualTo(1);
}

@Test
@DisplayName("Should add document to index")
void shouldAddDocumentToIndex() {
Expand Down
Loading