Skip to content

Commit

Permalink
fix: @document(indexName=) not working in conjunction with @IndexingO…
Browse files Browse the repository at this point in the history
…ptions (resolves gh-540)
  • Loading branch information
bsbodden committed Feb 15, 2025
1 parent ee74e06 commit ce03f5b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ public void createIndexFor(Class<?> cl) {
// IndexingOptions overrides Document#
if (maybeIndexingOptions.isPresent()) {
indexName = maybeIndexingOptions.get().indexName();
} else {
indexName = document.get().indexName();
}
indexName = indexName.isBlank() ? document.get().indexName() : indexName;
indexName = indexName.isBlank() ? cl.getName() + "Idx" : indexName;
} else {
if (maybeIndexingOptions.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class BasicRedisDocumentMappingTest extends AbstractBaseDocumentTest {
@Autowired
CustomIndexDocRepository customIndexDocRepository;

@Autowired
CustomIndex2DocRepository customIndex2DocRepository;

@Autowired
TypeKitchenSinkRepository typeKitchenSinkRepository;

Expand Down Expand Up @@ -823,7 +826,23 @@ void testFindAllWithSortingByIdUsingMetamodel() {
void testCustomIndexName() {
// CustomIndexDoc has a custom index name defined in the @Document annotation
var indices = jedis.ftList();
assertThat(indices).contains("MyCustomIndex");
assertThat(indices).contains("MyCustomDocIndex");
}

@Test
void testCustomIndexAndCustomPrefixWithIndexingOptions() {
CustomIndex2Doc cih2 = customIndex2DocRepository.save(CustomIndex2Doc.of("AAA", "BBB"));
String key = customIndex2DocRepository.getKeyFor(cih2); // prefix should be `cp2`
assertThat(key).startsWith("cp2:");
var indices = jedis.ftList();
assertThat(indices).contains("ci2");
}

@Test
void testCustomIndexAndCustomPrefix() {
CustomIndexDoc cih = customIndexDocRepository.save(CustomIndexDoc.of("AAA", "BBB"));
String key = customIndexDocRepository.getKeyFor(cih); // prefix should be `MyCustomPrefix`
assertThat(key).startsWith("MyCustomPrefix:");
}

//customIndexDocRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.redis.om.spring.fixtures.document.model;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.IndexCreationMode;
import com.redis.om.spring.annotations.IndexingOptions;
import com.redis.om.spring.annotations.Searchable;
import lombok.*;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(force = true)
@IndexingOptions(indexName = "ci2", creationMode = IndexCreationMode.SKIP_IF_EXIST)
@Document(value = "cp2")
public class CustomIndex2Doc {
@Id
private String id;

@NonNull
@Searchable(sortable = true)
private String first;

@NonNull
@Searchable(sortable = true)
private String second;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.redis.om.spring.fixtures.document.model;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.IndexingOptions;
import com.redis.om.spring.annotations.Searchable;
import lombok.*;
import org.springframework.data.annotation.Id;
Expand All @@ -9,7 +10,8 @@
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(force = true)
@Document(indexName = "MyCustomIndex")
@IndexingOptions(indexName = "MyCustomDocIndex")
@Document(value = "MyCustomPrefix")
public class CustomIndexDoc {
@Id
private String id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.redis.om.spring.fixtures.document.repository;

import com.redis.om.spring.fixtures.document.model.CustomIndex2Doc;
import com.redis.om.spring.repository.RedisDocumentRepository;

@SuppressWarnings("unused")
public interface CustomIndex2DocRepository extends RedisDocumentRepository<CustomIndex2Doc, String> {
}

0 comments on commit ce03f5b

Please sign in to comment.