Skip to content

Commit

Permalink
fix: enables explicitely indexed @id fields (resolves gh292)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden committed Jul 6, 2023
1 parent ff655d3 commit 09e1d9b
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ private List<Field> findIndexFields(java.lang.reflect.Field field, String prefix

Class<?> fieldType = ClassUtils.resolvePrimitiveIfNecessary(field.getType());

// Processed @Reference @Indexed fields: Create schema field for the ID
if (field.isAnnotationPresent(Reference.class)) {
//
// @Reference @Indexed fields: Create schema field for the reference entity @Id field
//
logger.debug("🪲Found @Reference field " + field.getName() + " in " + field.getDeclaringClass().getSimpleName());
var maybeReferenceIdField = getIdFieldForEntityClass(fieldType);
if (maybeReferenceIdField.isPresent()) {
Expand Down Expand Up @@ -695,7 +697,10 @@ private Optional<Field> createIndexedFieldForIdField(Class<?> cl, List<Field> fi
if (maybeIdField.isPresent()) {
java.lang.reflect.Field idField = maybeIdField.get();
// Only auto-index the @Id if not already indexed by the user (gh-135)
if (!idField.isAnnotationPresent(Indexed.class) && !idField.isAnnotationPresent(Searchable.class)
if (!idField.isAnnotationPresent(Indexed.class)
&& !idField.isAnnotationPresent(Searchable.class)
&& !idField.isAnnotationPresent(TagIndexed.class)
&& !idField.isAnnotationPresent(TextIndexed.class)
&& (fields.stream().noneMatch(f -> f.name.equals(idField.getName())))) {
if (Number.class.isAssignableFrom(idField.getType())) {
result = Optional.of(indexAsNumericFieldFor(maybeIdField.get(), isDocument, "", true, false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.redis.om.spring.annotations.document;

import com.google.common.collect.Sets;
import com.redis.om.spring.AbstractBaseDocumentTest;
import com.redis.om.spring.annotations.document.fixtures.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import redis.clients.jedis.json.Path;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SuppressWarnings("SpellCheckingInspection")
class ExplicitelyIndexedIdFieldsTest extends AbstractBaseDocumentTest {

@Autowired
DocWithIndexedIdRepository docWithIndexedIdRepository;

@Autowired
DocWithSearchableIdRepository docWithSearchableIdRepository;

@Autowired
DocWithTagIndexedIdRepository docWithTagIndexedIdRepository;

@BeforeEach
void cleanUp() {
docWithIndexedIdRepository.saveAll(List.of(DocWithIndexedId.of("DWII01", "DWII01"), DocWithIndexedId.of("DWII02", "DWII02")));
docWithSearchableIdRepository.saveAll(List.of(DocWithSearchableId.of("DWSI01", "DWSI01"), DocWithSearchableId.of("DWSI02", "DWSI02")));
docWithTagIndexedIdRepository.saveAll(List.of(DocWithTagIndexedId.of("DWTII01", "DWTII01"), DocWithTagIndexedId.of("DWTII02", "DWTII02")));
}

@Test void testFindById() {
Optional<DocWithIndexedId> maybeDWII01 = docWithIndexedIdRepository.findById("DWII01");
Optional<DocWithSearchableId> maybeDWSI01 = docWithSearchableIdRepository.findById("DWSI01");
Optional<DocWithTagIndexedId> maybeDWTII01 = docWithTagIndexedIdRepository.findById("DWTII01");

assertAll( //
() -> assertThat(maybeDWII01).isPresent(), //
() -> assertThat(maybeDWSI01).isPresent(), //
() -> assertThat(maybeDWTII01).isPresent() //
);
}

@Test void testFindAll() {
var allDWII = docWithIndexedIdRepository.findAll();
var allDWSI = docWithSearchableIdRepository.findAll();
var allDWTII = docWithTagIndexedIdRepository.findAll();

assertAll( //
() -> assertThat(allDWII).hasSize(2), //
() -> assertThat(allDWSI).hasSize(2), //
() -> assertThat(allDWTII).hasSize(2) //
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@Document
public class DocWithIndexedId {
@Id
@Indexed
@NonNull
protected String id;

@NonNull
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.repository.RedisDocumentRepository;

public interface DocWithIndexedIdRepository extends RedisDocumentRepository<DocWithIndexedId, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Searchable;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@Document
public class DocWithSearchableId {
@Id
@Searchable
@NonNull
protected String id;

@NonNull
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.repository.RedisDocumentRepository;

public interface DocWithSearchableIdRepository extends RedisDocumentRepository<DocWithSearchableId, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.TagIndexed;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;

@Data
@RequiredArgsConstructor(staticName = "of")
@Document
public class DocWithTagIndexedId {
@Id
@TagIndexed
@NonNull
protected String id;

@NonNull
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.redis.om.spring.annotations.document.fixtures;

import com.redis.om.spring.repository.RedisDocumentRepository;

public interface DocWithTagIndexedIdRepository extends RedisDocumentRepository<DocWithTagIndexedId, String> {
}

0 comments on commit 09e1d9b

Please sign in to comment.