11package com .redis .om .spring ;
22
3+ import com .google .gson .GsonBuilder ;
34import com .google .gson .annotations .JsonAdapter ;
45import com .redis .om .spring .annotations .*;
56import com .redis .om .spring .ops .RedisModulesOperations ;
67import com .redis .om .spring .ops .search .SearchOperations ;
78import com .redis .om .spring .repository .query .QueryUtils ;
9+ import com .redis .om .spring .serialization .gson .EnumTypeAdapter ;
810import org .apache .commons .lang3 .ObjectUtils ;
911import org .apache .commons .logging .Log ;
1012import org .apache .commons .logging .LogFactory ;
@@ -50,14 +52,16 @@ public class RediSearchIndexer {
5052 private final ApplicationContext ac ;
5153 private final RedisModulesOperations <String > rmo ;
5254 private final RedisMappingContext mappingContext ;
55+ private final GsonBuilder gsonBuilder ;
5356
5457 private static final String SKIPPING_INDEX_CREATION = "Skipping index creation for %s because %s" ;
5558
5659 @ SuppressWarnings ("unchecked" )
57- public RediSearchIndexer (ApplicationContext ac ) {
60+ public RediSearchIndexer (ApplicationContext ac , GsonBuilder gsonBuilder ) {
5861 this .ac = ac ;
5962 rmo = (RedisModulesOperations <String >) ac .getBean ("redisModulesOperations" );
6063 mappingContext = (RedisMappingContext ) ac .getBean ("keyValueMappingContext" );
64+ this .gsonBuilder = gsonBuilder ;
6165 }
6266
6367 public void createIndicesFor (Class <?> cls ) {
@@ -207,9 +211,10 @@ private List<Field> findIndexFields(java.lang.reflect.Field field, String prefix
207211 createIndexedFieldForReferenceIdField (field , isDocument ).ifPresent (fields ::add );
208212 } else if (indexed .schemaFieldType () == SchemaFieldType .AUTODETECT ) {
209213 //
210- // Any Character class, Enums or Boolean -> Tag Search Field
214+ // Any Character class, Boolean or Enum with AUTODETECT -> Tag Search Field
211215 //
212- if (CharSequence .class .isAssignableFrom (fieldType ) || (fieldType == Boolean .class ) || (fieldType .isEnum ())) {
216+ if (CharSequence .class .isAssignableFrom (fieldType ) || (fieldType == Boolean .class ) ||
217+ (fieldType .isEnum ())) {
213218 fields .add (indexAsTagFieldFor (field , isDocument , prefix , indexed .sortable (), indexed .separator (),
214219 indexed .arrayIndex (), indexed .alias ()));
215220 }
@@ -288,6 +293,11 @@ else if (fieldType == Point.class) {
288293 fields .addAll (findIndexFields (subfield , subfieldPrefix , isDocument ));
289294 }
290295 }
296+ case ORDINAL -> {
297+ fields .add (indexAsNumericFieldFor (field , isDocument , prefix , indexed .sortable (),
298+ indexed .noindex (), indexed .alias ()));
299+ gsonBuilder .registerTypeAdapter (fieldType , EnumTypeAdapter .of (fieldType ));
300+ }
291301 }
292302 }
293303 }
@@ -444,67 +454,49 @@ private Field indexAsTagFieldFor(java.lang.reflect.Field field, boolean isDocume
444454 }
445455
446456 private Field indexAsTextFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix , TextIndexed ti ) {
447- String fieldPrefix = getFieldPrefix (prefix , isDocument );
448- String name = fieldPrefix + field .getName ();
449- String alias = ObjectUtils .isEmpty (ti .alias ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ti .alias ();
450- FieldName fieldName = FieldName .of (name );
451- fieldName = fieldName .as (alias );
457+ var fieldName = getFieldName (field , isDocument , prefix , ObjectUtils .isEmpty (ti .alias ())
458+ ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ti .alias ());
452459
453460 String phonetic = ObjectUtils .isEmpty (ti .phonetic ()) ? null : ti .phonetic ();
454461
455462 return new TextField (fieldName , ti .weight (), ti .sortable (), ti .nostem (), ti .noindex (), phonetic );
456463 }
457464
458465 private Field indexAsTextFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix , Searchable ti ) {
459- String fieldPrefix = getFieldPrefix (prefix , isDocument );
460- String name = fieldPrefix + field .getName ();
461- String alias = ObjectUtils .isEmpty (ti .alias ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ti .alias ();
462- FieldName fieldName = FieldName .of (name );
463- fieldName = fieldName .as (alias );
466+ var fieldName = getFieldName (field , isDocument , prefix , ObjectUtils .isEmpty (ti .alias ())
467+ ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ti .alias ());
464468
465469 String phonetic = ObjectUtils .isEmpty (ti .phonetic ()) ? null : ti .phonetic ();
466470
467471 return new TextField (fieldName , ti .weight (), ti .sortable (), ti .nostem (), ti .noindex (), phonetic );
468472 }
469473
470474 private Field indexAsGeoFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix , GeoIndexed gi ) {
471- String fieldPrefix = getFieldPrefix (prefix , isDocument );
472- String name = fieldPrefix + field .getName ();
473- String alias = ObjectUtils .isEmpty (gi .alias ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : gi .alias ();
474- FieldName fieldName = FieldName .of (name );
475- fieldName = fieldName .as (alias );
475+ var fieldName = getFieldName (field , isDocument , prefix , ObjectUtils .isEmpty (gi .alias ())
476+ ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : gi .alias ());
476477
477478 return new Field (fieldName , FieldType .GEO );
478479 }
479480
480481 private Field indexAsNumericFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix ,
481482 NumericIndexed ni ) {
482- String fieldPrefix = getFieldPrefix (prefix , isDocument );
483- String name = fieldPrefix + field .getName ();
484- String alias = ObjectUtils .isEmpty (ni .alias ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ni .alias ();
485- FieldName fieldName = FieldName .of (name );
486- fieldName = fieldName .as (alias );
483+ var fieldName = getFieldName (field , isDocument , prefix , ObjectUtils .isEmpty (ni .alias ())
484+ ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : ni .alias ());
487485
488486 return new Field (fieldName , FieldType .NUMERIC );
489487 }
490488
491489 private Field indexAsNumericFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix ,
492490 boolean sortable , boolean noIndex , String annotationAlias ) {
493- String fieldPrefix = getFieldPrefix (prefix , isDocument );
494- String name = fieldPrefix + field .getName ();
495491 String alias = (annotationAlias == null || annotationAlias .isBlank ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : annotationAlias ;
496- FieldName fieldName = FieldName .of (name );
497- fieldName = fieldName .as (alias );
492+ var fieldName = getFieldName (field , isDocument , prefix , alias );
498493
499494 return new Field (fieldName , FieldType .NUMERIC , sortable , noIndex );
500495 }
501496
502497 private Field indexAsGeoFieldFor (java .lang .reflect .Field field , boolean isDocument , String prefix , String annotationAlias ) {
503- String fieldPrefix = getFieldPrefix (prefix , isDocument );
504- String name = fieldPrefix + field .getName ();
505498 String alias = (annotationAlias == null || annotationAlias .isBlank ()) ? QueryUtils .searchIndexFieldAliasFor (field , prefix ) : annotationAlias ;
506- FieldName fieldName = FieldName .of (name );
507- fieldName = fieldName .as (alias );
499+ var fieldName = getFieldName (field , isDocument , prefix , alias );
508500
509501 return new Field (fieldName , FieldType .GEO );
510502 }
@@ -514,6 +506,15 @@ private List<Field> indexAsNestedFieldFor(java.lang.reflect.Field field, String
514506 return getNestedField (fieldPrefix , field , prefix , null );
515507 }
516508
509+ private FieldName getFieldName (java .lang .reflect .Field field , boolean isDocument , String prefix ,
510+ String alias ) {
511+ String fieldPrefix = getFieldPrefix (prefix , isDocument );
512+ String name = fieldPrefix + field .getName ();
513+ FieldName fieldName = FieldName .of (name );
514+ fieldName = fieldName .as (alias );
515+ return fieldName ;
516+ }
517+
517518 private List <Field > getNestedField (String fieldPrefix , java .lang .reflect .Field field , String prefix ,
518519 List <Field > fieldList ) {
519520 if (fieldList == null ) {
0 commit comments