2020import javax .annotation .Nonnull ;
2121import lombok .Getter ;
2222import lombok .extern .slf4j .Slf4j ;
23- import redis .clients .jedis .Jedis ;
23+ import redis .clients .jedis .RedisClient ;
2424import redis .clients .jedis .UnifiedJedis ;
2525import redis .clients .jedis .search .FTCreateParams ;
2626import redis .clients .jedis .search .FTSearchParams ;
@@ -48,7 +48,6 @@ public final class SearchIndex {
4848 private final IndexSchema schema ;
4949
5050 private final BaseStorage storage ;
51- private Jedis client ;
5251 private UnifiedJedis unifiedClient ;
5352 @ Getter private boolean validateOnLoad = false ;
5453
@@ -91,40 +90,6 @@ public SearchIndex(IndexSchema schema, boolean validateOnLoad) {
9190 }
9291 this .connectionManager = null ;
9392 this .schema = schema ;
94- this .client = null ;
95- this .unifiedClient = null ;
96- this .validateOnLoad = validateOnLoad ;
97- this .storage = initializeStorage (schema );
98- }
99-
100- /**
101- * Create a SearchIndex with schema and Jedis client
102- *
103- * @param schema Index schema definition
104- * @param client Jedis client for Redis operations
105- */
106- public SearchIndex (IndexSchema schema , Jedis client ) {
107- this (schema , client , false );
108- }
109-
110- /**
111- * Create a SearchIndex with schema, Jedis client, and validateOnLoad option
112- *
113- * @param schema Index schema definition
114- * @param client Jedis client for Redis operations
115- * @param validateOnLoad Whether to validate documents on load
116- */
117- public SearchIndex (IndexSchema schema , Jedis client , boolean validateOnLoad ) {
118- if (schema == null ) {
119- throw new IllegalArgumentException ("Must provide a valid IndexSchema object" );
120- }
121- if (client == null ) {
122- throw new IllegalArgumentException ("Jedis client cannot be null" );
123- }
124- this .connectionManager = null ;
125- this .schema = schema ;
126- // Store the client - this is the expected usage pattern for this library
127- this .client = client ;
12893 this .unifiedClient = null ;
12994 this .validateOnLoad = validateOnLoad ;
13095 this .storage = initializeStorage (schema );
@@ -156,10 +121,9 @@ public SearchIndex(IndexSchema schema, String redisUrl, boolean validateOnLoad)
156121 }
157122 this .connectionManager = null ;
158123 this .schema = schema ;
159- this .client = null ;
160124 this .validateOnLoad = validateOnLoad ;
161- // Create UnifiedJedis from URL
162- this .unifiedClient = new UnifiedJedis (redisUrl );
125+ // Create RedisClient (extends UnifiedJedis) from URL - Jedis 7.2+ API
126+ this .unifiedClient = RedisClient . create (redisUrl );
163127 this .storage = initializeStorage (schema );
164128 }
165129
@@ -189,7 +153,6 @@ public SearchIndex(IndexSchema schema, UnifiedJedis unifiedClient, boolean valid
189153 }
190154 this .connectionManager = null ;
191155 this .schema = schema ;
192- this .client = null ;
193156 this .validateOnLoad = validateOnLoad ;
194157 // Store the client - this is the expected usage pattern for this library
195158 this .unifiedClient = unifiedClient ;
@@ -439,24 +402,19 @@ public static SearchIndex fromExisting(String indexName, UnifiedJedis client) {
439402 return new SearchIndex (schema , client );
440403 }
441404
442- /** Get Jedis connection from either connectionManager or direct client */
443- private Jedis getJedis () {
444- if (client != null ) {
445- return client ;
446- } else if (connectionManager != null ) {
447- return connectionManager .getJedis ();
448- } else {
449- throw new IllegalStateException ("No Redis connection available for document operations" );
450- }
451- }
452-
453- /** Get UnifiedJedis for RediSearch operations */
405+ /**
406+ * Get UnifiedJedis for Redis operations.
407+ *
408+ * <p>Returns the client from either the direct unifiedClient field or from the connectionManager.
409+ */
454410 private UnifiedJedis getUnifiedJedis () {
455411 if (unifiedClient != null ) {
456412 return unifiedClient ;
413+ } else if (connectionManager != null ) {
414+ return connectionManager .getClient ();
457415 } else {
458416 throw new IllegalStateException (
459- "RediSearch operations require UnifiedJedis client. Please use SearchIndex(schema, unifiedJedis) constructor." );
417+ "No Redis connection available. Use SearchIndex(schema, unifiedJedis) or SearchIndex(connectionManager, schema ) constructor." );
460418 }
461419 }
462420
@@ -762,60 +720,8 @@ public String addDocument(String docId, Map<String, Object> document) {
762720 Map <String , Object > processedDocument = preprocessDocument (document );
763721 // Always validate document against schema when adding directly
764722 validateDocument (processedDocument );
765- // Use UnifiedJedis if available for consistency
766- if (unifiedClient != null ) {
767- return addDocumentWithUnified (docId , processedDocument );
768- }
769-
770- Jedis jedis = getJedis ();
771- try {
772- if (getStorageType () == IndexSchema .StorageType .JSON ) {
773- // For JSON storage, use RedisJSON commands
774-
775- // Use JSON.SET command - Jedis doesn't have jsonSet, need UnifiedJedis
776- throw new IllegalStateException (
777- "JSON storage requires UnifiedJedis client. Use SearchIndex(schema, unifiedJedis) constructor." );
778- } else {
779- // For HASH storage - handle vectors specially
780- for (Map .Entry <String , Object > entry : processedDocument .entrySet ()) {
781- String key = entry .getKey ();
782- Object value = entry .getValue ();
783-
784- BaseField field = (schema != null ) ? schema .getField (key ) : null ;
785- if (field instanceof VectorField && value != null ) {
786- // Store vectors as binary data
787- byte [] vectorBytes = null ;
788- if (value instanceof byte []) {
789- // Already in byte array format
790- vectorBytes = (byte []) value ;
791- } else if (value instanceof float []) {
792- vectorBytes = ArrayUtils .floatArrayToBytes ((float []) value );
793- } else if (value instanceof double []) {
794- float [] floats = ArrayUtils .doubleArrayToFloats ((double []) value );
795- vectorBytes = ArrayUtils .floatArrayToBytes (floats );
796- }
797- if (vectorBytes != null ) {
798- jedis .hset (
799- docId .getBytes (StandardCharsets .UTF_8 ),
800- key .getBytes (StandardCharsets .UTF_8 ),
801- vectorBytes );
802- }
803- } else if (value != null ) {
804- // Store other fields as strings
805- jedis .hset (docId , key , value .toString ());
806- }
807- }
808- }
809-
810- return docId ;
811- } catch (Exception e ) {
812- throw new RuntimeException ("Failed to add document: " + e .getMessage (), e );
813- } finally {
814- // Close connection if we don't have a persistent client
815- if (client == null && connectionManager != null ) {
816- jedis .close ();
817- }
818- }
723+ // Use the unified method for all document operations
724+ return addDocumentWithUnified (docId , processedDocument );
819725 }
820726
821727 private String addDocumentWithUnified (String docId , Map <String , Object > document ) {
@@ -838,8 +744,8 @@ private String addDocumentWithUnified(String docId, Map<String, Object> document
838744 // Use JSON.SET command to store the document as an object
839745 // Path2.ROOT_PATH is the root JSON path "$"
840746 String result =
841- unifiedClient . jsonSetWithEscape (
842- docId , redis .clients .jedis .json .Path2 .ROOT_PATH , jsonDocument );
747+ getUnifiedJedis ()
748+ . jsonSetWithEscape ( docId , redis .clients .jedis .json .Path2 .ROOT_PATH , jsonDocument );
843749 log .debug ("Stored JSON document {}: {}" , docId , result );
844750 } else {
845751 // For HASH storage - handle vectors specially
@@ -874,11 +780,11 @@ private String addDocumentWithUnified(String docId, Map<String, Object> document
874780
875781 // Store binary fields
876782 if (!binaryFields .isEmpty ()) {
877- unifiedClient .hset (docId .getBytes (StandardCharsets .UTF_8 ), binaryFields );
783+ getUnifiedJedis () .hset (docId .getBytes (StandardCharsets .UTF_8 ), binaryFields );
878784 }
879785 // Store string fields
880786 if (!stringFields .isEmpty ()) {
881- unifiedClient .hset (docId , stringFields );
787+ getUnifiedJedis () .hset (docId , stringFields );
882788 }
883789 }
884790
0 commit comments