1010import glide .api .models .ClusterValue ;
1111import glide .api .models .GlideString ;
1212import glide .api .models .commands .ConditionalChange ;
13+ import glide .api .models .commands .json .JsonArrindexOptions ;
1314import glide .api .models .commands .json .JsonGetOptions ;
1415import glide .api .models .commands .json .JsonGetOptionsBinary ;
1516import glide .utils .ArgsBuilder ;
@@ -26,6 +27,7 @@ public class Json {
2627 private static final String JSON_NUMMULTBY = JSON_PREFIX + "NUMMULTBY" ;
2728 private static final String JSON_ARRAPPEND = JSON_PREFIX + "ARRAPPEND" ;
2829 private static final String JSON_ARRINSERT = JSON_PREFIX + "ARRINSERT" ;
30+ private static final String JSON_ARRINDEX = JSON_PREFIX + "ARRINDEX" ;
2931 private static final String JSON_ARRLEN = JSON_PREFIX + "ARRLEN" ;
3032 private static final String [] JSON_DEBUG_MEMORY = new String [] {JSON_PREFIX + "DEBUG" , "MEMORY" };
3133 private static final String [] JSON_DEBUG_FIELDS = new String [] {JSON_PREFIX + "DEBUG" , "FIELDS" };
@@ -604,6 +606,171 @@ public static CompletableFuture<Object> arrinsert(
604606 .toArray ());
605607 }
606608
609+ /**
610+ * Searches for the first occurrence of a <code>scalar</code> JSON value in the arrays at the
611+ * path.
612+ *
613+ * @param client The client to execute the command.
614+ * @param key The key of the JSON document.
615+ * @param path The path within the JSON document.
616+ * @param scalar The scalar value to search for.
617+ * @return
618+ * <ul>
619+ * <li>For JSONPath (<code>path</code> starts with <code>$</code>): Returns an array with a
620+ * list of integers for every possible path, indicating the index of the matching
621+ * element. The value is <code>-1</code> if not found. If a value is not an array, its
622+ * corresponding return value is <code>null</code>.
623+ * <li>For legacy path (path doesn't start with <code>$</code>): Returns an integer
624+ * representing the index of matching element, or <code>-1</code> if not found. If the
625+ * value at the <code>path</code> is not an array, an error is raised.
626+ * </ul>
627+ *
628+ * @example
629+ * <pre>{@code
630+ * Json.set(client, key, "$", "{\"a\": [\"value\", 3], \"b\": {\"a\": [3, [\"value\", false], 5]}}").get();
631+ * var result = Json.arrindex(client, key, "$..a", "3").get();
632+ * assert Arrays.equals((Object[]) result, new Object[] {1L, 0L});
633+ *
634+ * result = Json.arrindex(client, key, "$..a", "\"value\"").get();
635+ * assert Arrays.equals((Object[]) result, new Object[] {0L, -1L});
636+ * }</pre>
637+ */
638+ public static CompletableFuture <Object > arrindex (
639+ @ NonNull BaseClient client ,
640+ @ NonNull String key ,
641+ @ NonNull String path ,
642+ @ NonNull String scalar ) {
643+ return arrindex (client , gs (key ), gs (path ), gs (scalar ));
644+ }
645+
646+ /**
647+ * Searches for the first occurrence of a <code>scalar</code> JSON value in the arrays at the
648+ * path.
649+ *
650+ * @param client The client to execute the command.
651+ * @param key The key of the JSON document.
652+ * @param path The path within the JSON document.
653+ * @param scalar The scalar value to search for.
654+ * @return
655+ * <ul>
656+ * <li>For JSONPath (<code>path</code> starts with <code>$</code>): Returns an array with a
657+ * list of integers for every possible path, indicating the index of the matching
658+ * element. The value is <code>-1</code> if not found. If a value is not an array, its
659+ * corresponding return value is <code>null</code>.
660+ * <li>For legacy path (path doesn't start with <code>$</code>): Returns an integer
661+ * representing the index of matching element, or <code>-1</code> if not found. If the
662+ * value at the <code>path</code> is not an array, an error is raised.
663+ * </ul>
664+ *
665+ * @example
666+ * <pre>{@code
667+ * Json.set(client, key, "$", "{\"a\": [\"value\", 3], \"b\": {\"a\": [3, [\"value\", false], 5]}}").get();
668+ * var result = Json.arrindex(client, gs(key), gs("$..a"), gs("3")).get();
669+ * assert Arrays.equals((Object[]) result, new Object[] {1L, 0L});
670+ *
671+ * // Searches for the first occurrence of null in the arrays
672+ * result = Json.arrindex(client, gs(key), gs("$..a"), gs("null")).get();
673+ * assert Arrays.equals((Object[]) result, new Object[] {-1L, -1L});
674+ * }</pre>
675+ */
676+ public static CompletableFuture <Object > arrindex (
677+ @ NonNull BaseClient client ,
678+ @ NonNull GlideString key ,
679+ @ NonNull GlideString path ,
680+ @ NonNull GlideString scalar ) {
681+ return executeCommand (client , new GlideString [] {gs (JSON_ARRINDEX ), key , path , scalar });
682+ }
683+
684+ /**
685+ * Searches for the first occurrence of a <code>scalar</code> JSON value in the arrays at the
686+ * path.
687+ *
688+ * @param client The client to execute the command.
689+ * @param key The key of the JSON document.
690+ * @param path The path within the JSON document.
691+ * @param scalar The scalar value to search for.
692+ * @param options The additional options for the command. See <code>JsonArrindexOptions</code>.
693+ * @return
694+ * <ul>
695+ * <li>For JSONPath (<code>path</code> starts with <code>$</code>): Returns an array with a
696+ * list of integers for every possible path, indicating the index of the matching
697+ * element. The value is <code>-1</code> if not found. If a value is not an array, its
698+ * corresponding return value is <code>null</code>.
699+ * <li>For legacy path (path doesn't start with <code>$</code>): Returns an integer
700+ * representing the index of matching element, or <code>-1</code> if not found. If the
701+ * value at the <code>path</code> is not an array, an error is raised.
702+ * </ul>
703+ *
704+ * @example
705+ * <pre>{@code
706+ * Json.set(client, key, "$", "{\"a\": [\"value\", 3], \"b\": {\"a\": [3, [\"value\", false], 5]}}").get();
707+ * var result = Json.arrindex(client, key, ".a", "3", new JsonArrindexOptions(0L)).get();
708+ * assert Arrays.equals(1L, result);
709+ * }</pre>
710+ */
711+ public static CompletableFuture <Object > arrindex (
712+ @ NonNull BaseClient client ,
713+ @ NonNull String key ,
714+ @ NonNull String path ,
715+ @ NonNull String scalar ,
716+ @ NonNull JsonArrindexOptions options ) {
717+
718+ return executeCommand (
719+ client ,
720+ new ArgsBuilder ()
721+ .add (JSON_ARRINDEX )
722+ .add (key )
723+ .add (path )
724+ .add (scalar )
725+ .add (options .toArgs ())
726+ .toArray ());
727+ }
728+
729+ /**
730+ * Searches for the first occurrence of a <code>scalar</code> JSON value in the arrays at the
731+ * path.
732+ *
733+ * @param client The client to execute the command.
734+ * @param key The key of the JSON document.
735+ * @param path The path within the JSON document.
736+ * @param scalar The scalar value to search for.
737+ * @param options The additional options for the command. See <code>JsonArrindexOptions</code>.
738+ * @return
739+ * <ul>
740+ * <li>For JSONPath (<code>path</code> starts with <code>$</code>): Returns an array with a
741+ * list of integers for every possible path, indicating the index of the matching
742+ * element. The value is <code>-1</code> if not found. If a value is not an array, its
743+ * corresponding return value is <code>null</code>..
744+ * <li>For legacy path (path doesn't start with <code>$</code>): Returns an integer
745+ * representing the index of matching element, or <code>-1</code> if not found. If the
746+ * value at the <code>path</code> is not an array, an error is raised.
747+ * </ul>
748+ *
749+ * @example
750+ * <pre>{@code
751+ * Json.set(client, key, "$", "{\"a\": [\"value\", 3], \"b\": {\"a\": [3, [\"value\", false], 5]}}").get();
752+ * var result = Json.arrindex(client, gs(key), gs(".a"), gs("3"), new JsonArrindexOptions(0L)).get();
753+ * assert Arrays.equals(1L, result);
754+ * }</pre>
755+ */
756+ public static CompletableFuture <Object > arrindex (
757+ @ NonNull BaseClient client ,
758+ @ NonNull GlideString key ,
759+ @ NonNull GlideString path ,
760+ @ NonNull GlideString scalar ,
761+ @ NonNull JsonArrindexOptions options ) {
762+
763+ return executeCommand (
764+ client ,
765+ new ArgsBuilder ()
766+ .add (JSON_ARRINDEX )
767+ .add (key )
768+ .add (path )
769+ .add (scalar )
770+ .add (options .toArgs ())
771+ .toArray ());
772+ }
773+
607774 /**
608775 * Retrieves the length of the array at the specified <code>path</code> within the JSON document
609776 * stored at <code>key</code>.
0 commit comments