From 09fdb2bd931ed25ee7790c7502cb2ad3f19baac5 Mon Sep 17 00:00:00 2001 From: Brian Sam-Bodden Date: Tue, 21 May 2024 19:03:43 -0700 Subject: [PATCH] fix: honor custom search index names for Hashes --- .../spring/RedisEnhancedKeyValueAdapter.java | 89 ++++++++----------- .../om/spring/RedisJSONKeyValueAdapter.java | 88 ++++++++---------- .../om/spring/indexing/RediSearchIndexer.java | 8 +- .../repository/query/RediSearchQuery.java | 18 ++-- .../repository/query/RedisEnhancedQuery.java | 18 ++-- .../SimpleRedisDocumentRepository.java | 29 +++--- .../SimpleRedisEnhancedRepository.java | 62 ++++++------- .../search/stream/SearchStreamImpl.java | 3 +- .../hash/BasicRedisHashMappingTest.java | 1 + .../spring/fixtures/hash/model/Student.java | 5 +- 10 files changed, 143 insertions(+), 178 deletions(-) diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java b/redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java index b45cf6c5..cd5b4a28 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/RedisEnhancedKeyValueAdapter.java @@ -207,33 +207,26 @@ public T get(Object id, String keyspace, Class type) { @Override public void deleteAllOf(String keyspace) { Class type = indexer.getEntityClassForKeyspace(keyspace); - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - searchOps.dropIndexAndDocuments(); - indexer.createIndexFor(type); - } + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + searchOps.dropIndexAndDocuments(); + indexer.createIndexFor(type); } public List getAllIds(String keyspace, Class type) { - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - List keys = List.of(); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - Optional maybeIdField = getIdFieldForEntityClass(type); - String idField = maybeIdField.map(Field::getName).orElse("id"); - - Query query = new Query("*"); - query.returnFields(idField); - SearchResult searchResult = searchOps.search(query); - - keys = searchResult.getDocuments().stream() - .map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) - .map(e -> maybeIdField.map(field -> getIdFieldForEntity(field, e)).orElse(null)).filter(Objects::nonNull) - .map(Object::toString).toList(); - } - - return keys; + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Optional maybeIdField = getIdFieldForEntityClass(type); + String idField = maybeIdField.map(Field::getName).orElse("id"); + + Query query = new Query("*"); + query.returnFields(idField); + SearchResult searchResult = searchOps.search(query); + + return searchResult.getDocuments().stream() + .map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) + .map(e -> maybeIdField.map(field -> getIdFieldForEntity(field, e)).orElse(null)).filter(Objects::nonNull) + .map(Object::toString).toList(); } /** @@ -250,26 +243,20 @@ public List getAllIds(String keyspace, Class type) { @SuppressWarnings("unchecked") @Override public List getAllOf(String keyspace, Class type, long offset, int rows) { - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - - List result = List.of(); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - Query query = new Query("*"); - offset = Math.max(0, offset); - int limit = rows; - if (limit <= 0) { - limit = redisOMProperties.getRepository().getQuery().getLimit(); - } - query.limit(Math.toIntExact(offset), limit); - SearchResult searchResult = searchOps.search(query); - - result = (List) searchResult.getDocuments().stream() // - .map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) // - .toList(); + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Query query = new Query("*"); + offset = Math.max(0, offset); + int limit = rows; + if (limit <= 0) { + limit = redisOMProperties.getRepository().getQuery().getLimit(); } + query.limit(Math.toIntExact(offset), limit); + SearchResult searchResult = searchOps.search(query); - return result; + return (List) searchResult.getDocuments().stream() // + .map(d -> documentToObject(d, type, (MappingRedisOMConverter) converter)) // + .toList(); } /* @@ -304,19 +291,15 @@ public T delete(Object id, String keyspace, Class type) { */ @Override public long count(String keyspace) { - long count = 0L; - Optional maybeIndexName = indexer.getIndexName(keyspace); - if (maybeIndexName.isPresent()) { - SearchOperations search = modulesOperations.opsForSearch(maybeIndexName.get()); - // FT.SEARCH index * LIMIT 0 0 - Query query = new Query("*"); - query.limit(0, 0); + String indexName = indexer.getIndexName(keyspace); + SearchOperations search = modulesOperations.opsForSearch(indexName); + // FT.SEARCH index * LIMIT 0 0 + Query query = new Query("*"); + query.limit(0, 0); - SearchResult result = search.search(query); + SearchResult result = search.search(query); - count = result.getTotalResults(); - } - return count; + return result.getTotalResults(); } /* diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java b/redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java index aceb8792..4f361c02 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/RedisJSONKeyValueAdapter.java @@ -149,46 +149,35 @@ public T get(String key, Class type) { */ @Override public List getAllOf(String keyspace, Class type, long offset, int rows) { - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - - List result = List.of(); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - Query query = new Query("*"); - offset = Math.max(0, offset); - int limit = rows; - if (limit <= 0) { - limit = redisOMProperties.getRepository().getQuery().getLimit(); - } - query.limit(Math.toIntExact(offset), limit); - SearchResult searchResult = searchOps.search(query); - Gson gson = gsonBuilder.create(); - result = searchResult.getDocuments().stream() - .map(d -> gson.fromJson(SafeEncoder.encode((byte[]) d.get("$")), type)) // - .toList(); + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Query query = new Query("*"); + offset = Math.max(0, offset); + int limit = rows; + if (limit <= 0) { + limit = redisOMProperties.getRepository().getQuery().getLimit(); } - - return result; + query.limit(Math.toIntExact(offset), limit); + SearchResult searchResult = searchOps.search(query); + Gson gson = gsonBuilder.create(); + return searchResult.getDocuments().stream() + .map(d -> gson.fromJson(SafeEncoder.encode((byte[]) d.get("$")), type)) // + .toList(); } public List getAllKeys(String keyspace, Class type) { - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - - List keys = List.of(); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - Optional maybeIdField = ObjectUtils.getIdFieldForEntityClass(type); - String idField = maybeIdField.map(Field::getName).orElse("id"); - - Query query = new Query("*"); - query.returnFields(idField); - SearchResult searchResult = searchOps.search(query); - - keys = searchResult.getDocuments().stream().map(Document::getId) // - .toList(); - } - - return keys; + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Optional maybeIdField = ObjectUtils.getIdFieldForEntityClass(type); + String idField = maybeIdField.map(Field::getName).orElse("id"); + + Query query = new Query("*"); + query.returnFields(idField); + SearchResult searchResult = searchOps.search(query); + + return searchResult.getDocuments().stream() // + .map(Document::getId) // + .toList(); } /* @@ -219,12 +208,10 @@ public T delete(Object id, String keyspace, Class type) { @Override public void deleteAllOf(String keyspace) { Class type = indexer.getEntityClassForKeyspace(keyspace); - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - searchOps.dropIndexAndDocuments(); - indexer.createIndexFor(type); - } + String searchIndex = indexer.getIndexName(keyspace); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + searchOps.dropIndexAndDocuments(); + indexer.createIndexFor(type); } /* @@ -236,18 +223,15 @@ public void deleteAllOf(String keyspace) { @Override public long count(String keyspace) { long count = 0L; - Optional maybeIndexName = indexer.getIndexName(keyspace); - if (maybeIndexName.isPresent()) { - SearchOperations search = modulesOperations.opsForSearch(maybeIndexName.get()); - // FT.SEARCH index * LIMIT 0 0 - Query query = new Query("*"); - query.limit(0, 0); + String indexName = indexer.getIndexName(keyspace); + SearchOperations search = modulesOperations.opsForSearch(indexName); + // FT.SEARCH index * LIMIT 0 0 + Query query = new Query("*"); + query.limit(0, 0); - SearchResult result = search.search(query); + SearchResult result = search.search(query); - count = result.getTotalResults(); - } - return count; + return result.getTotalResults(); } /* diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/indexing/RediSearchIndexer.java b/redis-om-spring/src/main/java/com/redis/om/spring/indexing/RediSearchIndexer.java index dbb92f5f..b7fca610 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/indexing/RediSearchIndexer.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/indexing/RediSearchIndexer.java @@ -190,15 +190,15 @@ public void dropIndexFor(Class cl) { dropIndex(cl, false, false); } - public Optional getIndexName(String keyspace) { + public String getIndexName(String keyspace) { return getIndexName(keyspaceToEntityClass.get(getKeyspace(keyspace))); } - public Optional getIndexName(Class entityClass) { + public String getIndexName(Class entityClass) { if (entityClass != null && entityClassToIndexName.containsKey(entityClass)) { - return Optional.of(entityClassToIndexName.get(entityClass)); + return entityClassToIndexName.get(entityClass); } else { - return Optional.empty(); + return entityClass.getName() + "Idx"; } } diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RediSearchQuery.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RediSearchQuery.java index 710e0a05..120f9d80 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RediSearchQuery.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RediSearchQuery.java @@ -52,7 +52,6 @@ public class RediSearchQuery implements RepositoryQuery { private static final Log logger = LogFactory.getLog(RediSearchQuery.class); private final QueryMethod queryMethod; - private final String searchIndex; private final RedisOMProperties redisOMProperties; private final boolean hasLanguageParameter; // aggregation fields @@ -104,8 +103,6 @@ public RediSearchQuery(// this.indexer = indexer; this.queryMethod = queryMethod; this.domainType = this.queryMethod.getEntityInformation().getJavaType(); - Optional maybeIndex = indexer.getIndexName(this.domainType); - this.searchIndex = maybeIndex.orElse(this.domainType.getName() + "Idx"); this.gsonBuilder = gsonBuilder; this.redisOMProperties = redisOMProperties; @@ -402,7 +399,8 @@ private Object executeQuery(Object[] parameters) { ParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor); - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); boolean excludeNullParams = !isNullParamQuery; String preparedQuery = prepareQuery(parameters, excludeNullParams); Query query = new Query(preparedQuery); @@ -518,7 +516,8 @@ private Object parseDocumentResult(redis.clients.jedis.search.Document doc) { } private Object executeDeleteQuery(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); String baseQuery = prepareQuery(parameters, true); AggregationBuilder aggregation = new AggregationBuilder(baseQuery); @@ -571,7 +570,8 @@ private Object executeDeleteQuery(Object[] parameters) { } private Object executeAggregation(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); // build the aggregation AggregationBuilder aggregation = new AggregationBuilder(value); @@ -683,7 +683,8 @@ private Object executeAggregation(Object[] parameters) { } private Object executeFtTagVals() { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); return ops.tagVals(this.value); } @@ -766,7 +767,8 @@ private Gson getGson() { } private Object executeNullQuery(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); String baseQuery = prepareQuery(parameters, true); AggregationBuilder aggregation = new AggregationBuilder(baseQuery); diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RedisEnhancedQuery.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RedisEnhancedQuery.java index 8ff5a7e3..e130c79b 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RedisEnhancedQuery.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/query/RedisEnhancedQuery.java @@ -54,7 +54,6 @@ public class RedisEnhancedQuery implements RepositoryQuery { private static final Log logger = LogFactory.getLog(RedisEnhancedQuery.class); private final QueryMethod queryMethod; - private final String searchIndex; private final RedisOMProperties redisOMProperties; private final boolean hasLanguageParameter; // aggregation fields @@ -106,8 +105,6 @@ public RedisEnhancedQuery(QueryMethod queryMethod, // this.modulesOperations = (RedisModulesOperations) rmo; this.queryMethod = queryMethod; this.domainType = this.queryMethod.getEntityInformation().getJavaType(); - Optional maybeIndex = indexer.getIndexName(this.domainType); - this.searchIndex = maybeIndex.orElse(this.domainType.getName() + "Idx"); this.redisOMProperties = redisOMProperties; this.redisOperations = redisOperations; this.mappingConverter = new MappingRedisOMConverter(null, new ReferenceResolverImpl(redisOperations)); @@ -402,7 +399,8 @@ private Object executeQuery(Object[] parameters) { ParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters); ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor); - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); boolean excludeNullParams = !isNullParamQuery; String preparedQuery = prepareQuery(parameters, excludeNullParams); Query query = new Query(preparedQuery); @@ -511,7 +509,8 @@ private Object executeQuery(Object[] parameters) { } private Object executeDeleteQuery(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); String baseQuery = prepareQuery(parameters, true); AggregationBuilder aggregation = new AggregationBuilder(baseQuery); @@ -580,7 +579,8 @@ private Object executeDeleteQuery(Object[] parameters) { } private Object executeAggregation(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); // build the aggregation AggregationBuilder aggregation = new AggregationBuilder(value); @@ -692,7 +692,8 @@ private Object executeAggregation(Object[] parameters) { } private Object executeFtTagVals() { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); return ops.tagVals(this.value); } @@ -769,7 +770,8 @@ private String prepareQuery(final Object[] parameters, boolean excludeNullParams } private Object executeNullQuery(Object[] parameters) { - SearchOperations ops = modulesOperations.opsForSearch(searchIndex); + String indexName = indexer.getIndexName(this.domainType); + SearchOperations ops = modulesOperations.opsForSearch(indexName); String baseQuery = prepareQuery(parameters, true); AggregationBuilder aggregation = new AggregationBuilder(baseQuery); diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java index 0a17fe3d..716f8637 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisDocumentRepository.java @@ -387,20 +387,20 @@ public Page findAll(Pageable pageable) { } if (indexer.indexDefinitionExistsFor(metadata.getJavaType())) { - Optional maybeSearchIndex = indexer.getIndexName(metadata.getJavaType()); - if (maybeSearchIndex.isPresent()) { - String searchIndex = maybeSearchIndex.get(); - SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); - Query query = new Query("*"); - query.limit(Math.toIntExact(pageable.getOffset()), pageable.getPageSize()); - - for (Order order : pageable.getSort()) { - query.setSortBy(order.getProperty(), order.isAscending()); - } + String searchIndex = indexer.getIndexName(metadata.getJavaType()); + + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Query query = new Query("*"); + query.limit(Math.toIntExact(pageable.getOffset()), pageable.getPageSize()); + + for (Order order : pageable.getSort()) { + query.setSortBy(order.getProperty(), order.isAscending()); + } - SearchResult searchResult = searchOps.search(query); - Gson gson = gsonBuilder.create(); + SearchResult searchResult = searchOps.search(query); + Gson gson = gsonBuilder.create(); + if (searchResult.getTotalResults() > 0) { List content = searchResult.getDocuments().stream() .map(d -> gson.fromJson(SafeEncoder.encode((byte[]) d.get("$")), metadata.getJavaType())).toList(); @@ -408,7 +408,6 @@ public Page findAll(Pageable pageable) { } else { return Page.empty(); } - } else { return super.findAll(pageable); } @@ -454,8 +453,8 @@ public R findBy(Example example, Function getSearchOps() { String keyspace = indexer.getKeyspaceForEntityClass(metadata.getJavaType()); - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - return modulesOperations.opsForSearch(maybeSearchIndex.get()); + String searchIndex = indexer.getIndexName(keyspace); + return modulesOperations.opsForSearch(searchIndex); } } diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java index cc580889..8758b879 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/repository/support/SimpleRedisEnhancedRepository.java @@ -92,25 +92,20 @@ public SimpleRedisEnhancedRepository( // @Override public Iterable getIds() { String keyspace = indexer.getKeyspaceForEntityClass(metadata.getJavaType()); - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - List result = List.of(); - if (maybeSearchIndex.isPresent()) { - SearchOperations searchOps = modulesOperations.opsForSearch(maybeSearchIndex.get()); - Optional maybeIdField = ObjectUtils.getIdFieldForEntityClass(metadata.getJavaType()); - String idField = maybeIdField.map(Field::getName).orElse("id"); - - Query query = new Query("*"); - query.limit(0, MAX_SEARCH_RESULTS); - query.returnFields(idField); - SearchResult searchResult = searchOps.search(query); - - result = (List) searchResult.getDocuments().stream() // - .map(d -> ObjectUtils.documentToObject(d, metadata.getJavaType(), mappingConverter)) // - .map(e -> ObjectUtils.getIdFieldForEntity(maybeIdField.get(), e)) // - .toList(); - } - - return result; + String searchIndex = indexer.getIndexName(keyspace); + + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Optional maybeIdField = ObjectUtils.getIdFieldForEntityClass(metadata.getJavaType()); + String idField = maybeIdField.map(Field::getName).orElse("id"); + Query query = new Query("*"); + query.limit(0, MAX_SEARCH_RESULTS); + query.returnFields(idField); + SearchResult searchResult = searchOps.search(query); + + return (List) searchResult.getDocuments().stream() // + .map(d -> ObjectUtils.documentToObject(d, metadata.getJavaType(), mappingConverter)) // + .map(e -> ObjectUtils.getIdFieldForEntity(maybeIdField.get(), e)) // + .toList(); } @Override @@ -191,29 +186,26 @@ public Page findAll(Pageable pageable) { } if (indexer.indexDefinitionExistsFor(metadata.getJavaType())) { - Optional maybeSearchIndex = indexer.getIndexName(metadata.getJavaType()); - if (maybeSearchIndex.isPresent()) { - String searchIndex = maybeSearchIndex.get(); - SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); - Query query = new Query("*"); - query.limit(Math.toIntExact(pageable.getOffset()), pageable.getPageSize()); - - pageable.getSort(); - for (Order order : pageable.getSort()) { - query.setSortBy(order.getProperty(), order.isAscending()); - } + String searchIndex = indexer.getIndexName(metadata.getJavaType()); + SearchOperations searchOps = modulesOperations.opsForSearch(searchIndex); + Query query = new Query("*"); + query.limit(Math.toIntExact(pageable.getOffset()), pageable.getPageSize()); + + pageable.getSort(); + for (Order order : pageable.getSort()) { + query.setSortBy(order.getProperty(), order.isAscending()); + } - SearchResult searchResult = searchOps.search(query); + SearchResult searchResult = searchOps.search(query); + if (searchResult.getTotalResults() > 0) { @SuppressWarnings("unchecked") List content = (List) searchResult.getDocuments().stream() // .map(d -> ObjectUtils.documentToObject(d, metadata.getJavaType(), mappingConverter)) // .toList(); - return new PageImpl<>(content, pageable, searchResult.getTotalResults()); } else { return Page.empty(); } - } else { Iterable content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(), metadata.getJavaType()); @@ -330,7 +322,7 @@ public R findBy(Example example, Function getSearchOps() { String keyspace = indexer.getKeyspaceForEntityClass(metadata.getJavaType()); - Optional maybeSearchIndex = indexer.getIndexName(keyspace); - return modulesOperations.opsForSearch(maybeSearchIndex.get()); + String searchIndex = indexer.getIndexName(keyspace); + return modulesOperations.opsForSearch(searchIndex); } } diff --git a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/SearchStreamImpl.java b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/SearchStreamImpl.java index a189efb2..60343f73 100644 --- a/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/SearchStreamImpl.java +++ b/redis-om-spring/src/main/java/com/redis/om/spring/search/stream/SearchStreamImpl.java @@ -86,8 +86,7 @@ public SearchStreamImpl(Class entityClass, RedisModulesOperations mod this.indexer = indexer; this.modulesOperations = modulesOperations; this.entityClass = entityClass; - Optional maybeIndex = this.indexer.getIndexName(entityClass); - this.searchIndex = maybeIndex.orElse(entityClass.getName() + "Idx"); + this.searchIndex = this.indexer.getIndexName(entityClass); this.search = modulesOperations.opsForSearch(searchIndex); this.json = modulesOperations.opsForJSON(); this.gsonBuilder = gsonBuilder; diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/BasicRedisHashMappingTest.java b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/BasicRedisHashMappingTest.java index cc58decb..66e72baf 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/BasicRedisHashMappingTest.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/annotations/hash/BasicRedisHashMappingTest.java @@ -721,6 +721,7 @@ void testCustomIndexName() { // CustomIndexHash is a Hash that has a custom index name defined in the @IndexingOptions annotation var indices = jedis.ftList(); assertThat(indices).contains("MyCustomHashIndex"); + assertThat(indices).contains("myIndexStudent"); } @Test diff --git a/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/Student.java b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/Student.java index bef83918..360cedf1 100644 --- a/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/Student.java +++ b/redis-om-spring/src/test/java/com/redis/om/spring/fixtures/hash/model/Student.java @@ -1,6 +1,8 @@ package com.redis.om.spring.fixtures.hash.model; +import com.redis.om.spring.annotations.IndexCreationMode; import com.redis.om.spring.annotations.Indexed; +import com.redis.om.spring.annotations.IndexingOptions; import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; @@ -13,7 +15,8 @@ @Data @NoArgsConstructor @RequiredArgsConstructor(staticName = "of") -@RedisHash +@IndexingOptions(creationMode = IndexCreationMode.SKIP_IF_EXIST, indexName = "myIndexStudent") +@RedisHash("my:key:space") public class Student { @Id