Skip to content

Commit

Permalink
Add support for FT.DROPINDEX redis#2722 (redis#3164)
Browse files Browse the repository at this point in the history
* Add support for FT.DROPINDEX redis#2722

* Polishing
  • Loading branch information
tishun authored Feb 5, 2025
1 parent fcb7a1f commit e3afb06
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ on:
branches:
- main
- '[0-9].*'
- 'feature/*'
pull_request:
branches:
- main
- '[0-9].*'
- 'feature/*'
schedule:
- cron: '0 1 * * *' # nightly build
workflow_dispatch:
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,11 @@ public RedisFuture<String> ftCreate(K index, CreateArgs<K, V> options, List<Fiel
return dispatch(searchCommandBuilder.ftCreate(index, options, fields));
}

@Override
public RedisFuture<String> ftDropindex(K index, boolean deleteDocumentKeys) {
return dispatch(searchCommandBuilder.ftDropindex(index, deleteDocumentKeys));
}

@Override
public RedisFuture<List<Long>> jsonArrappend(K key, JsonPath jsonPath, JsonValue... values) {
return dispatch(jsonCommandBuilder.jsonArrappend(key, jsonPath, values));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,11 @@ public Mono<String> ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> f
return createMono(() -> searchCommandBuilder.ftCreate(index, options, fields));
}

@Override
public Mono<String> ftDropindex(K index, boolean deleteDocumentKeys) {
return createMono(() -> searchCommandBuilder.ftDropindex(index, deleteDocumentKeys));
}

@Override
public Flux<Long> jsonArrappend(K key, JsonPath jsonPath, JsonValue... values) {
return createDissolvingFlux(() -> jsonCommandBuilder.jsonArrappend(key, jsonPath, values));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/lettuce/core/RediSearchCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.lettuce.core.protocol.Command;
import io.lettuce.core.protocol.CommandArgs;
import io.lettuce.core.protocol.CommandKeyword;
import io.lettuce.core.protocol.RedisCommand;
import io.lettuce.core.search.arguments.CreateArgs;
import io.lettuce.core.search.Field;

Expand Down Expand Up @@ -60,4 +61,23 @@ public Command<K, V, String> ftCreate(K index, CreateArgs<K, V> createArgs, List

}

/**
* Drop the index with the given name.
*
* @param index the index name
* @param deleteDocumentKeys whether to delete the document keys
* @return the result of the drop command
*/
public Command<K, V, String> ftDropindex(K index, boolean deleteDocumentKeys) {
notNullKey(index);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(index);

if (deleteDocumentKeys) {
args.add(CommandKeyword.DD);
}

return createCommand(FT_DROPINDEX, new StatusOutput<>(codec), args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@ public interface RediSearchAsyncCommands<K, V> {
*/
RedisFuture<String> ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
RedisFuture<String> ftDropindex(K index, boolean deleteDocuments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@ public interface RediSearchReactiveCommands<K, V> {
*/
Mono<String> ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
Mono<String> ftDropindex(K index, boolean deleteDocuments);

}
17 changes: 17 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RediSearchCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public interface RediSearchCommands<K, V> {
*/
String ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
String ftDropindex(K index, boolean deleteDocuments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public interface RediSearchAsyncCommands<K, V> {
*/
AsyncExecutions<String> ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
AsyncExecutions<String> ftDropindex(K index, boolean deleteDocuments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public interface RediSearchCommands<K, V> {
*/
Executions<String> ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
Executions<String> ftDropindex(K index, boolean deleteDocuments);

}
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public enum CommandKeyword implements ProtocolKeyword {

MAXTEXTFIELDS, PREFIX, FILTER, LANGUAGE, LANGUAGE_FIELD, SCORE, SCORE_FIELD, PAYLOAD_FIELD, TEMPORARY, NOOFFSETS, NOHL, NOFIELDS, NOFREQS, SKIPINITIALSCAN, STOPWORDS, AS, SORTABLE, SCHEMA, UNF, NOINDEX,

NOSTEM, PHONETIC, WEIGHT, SEPARATOR, CASESENSITIVE, WITHSUFFIXTRIE, INDEXEMPTY, INDEXMISSING;
NOSTEM, PHONETIC, WEIGHT, SEPARATOR, CASESENSITIVE, WITHSUFFIXTRIE, INDEXEMPTY, INDEXMISSING, DD;

public final byte[] bytes;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public enum CommandType implements ProtocolKeyword {
"JSON.STRLEN"), JSON_TOGGLE("JSON.TOGGLE"), JSON_TYPE("JSON.TYPE"),

// RediSearch
FT_CREATE("FT.CREATE"),
FT_CREATE("FT.CREATE"), FT_DROPINDEX("FT.DROPINDEX"),

// Others

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,22 @@ interface RediSearchCoroutinesCommands<K : Any, V : Any> {
*/
suspend fun ftCreate(index: K, options: CreateArgs<K, V>, fields: List<Field<K>>): String?

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
suspend fun ftDropindex(index: K, deleteDocuments: Boolean): String?

}

17 changes: 17 additions & 0 deletions src/main/templates/io/lettuce/core/api/RediSearchCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public interface RediSearchCommands<K, V> {
*/
String ftCreate(K index, CreateArgs<K, V> options, List<Field<K>> fields);

/**
* Drop an index.
* <p/>
* By default, <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a> does not delete the documents
* associated with the index. Adding the <code>deleteDocuments</code> option deletes the documents as well. If an index
* creation is still running (<a href="https://redis.io/docs/latest/commands/ft.create/">FT.CREATE</a> is running
* asynchronously), only the document hashes that have already been indexed are deleted. The document hashes left to be
* indexed remain in the database.
*
* @param index the index name, as a key
* @param deleteDocuments if true, delete the documents as well
* @return the result of the drop command
* @since 6.6
* @see <a href="https://redis.io/docs/latest/commands/ft.dropindex/">FT.DROPINDEX</a>
*/
String ftDropindex(K index, boolean deleteDocuments);

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,26 @@ void shouldCorrectlyConstructFtCreateCommandScenario2() {
assertThat(buf.toString(StandardCharsets.UTF_8)).isEqualTo(result);
}

@Test
void shouldCorrectlyConstructFtDropindexCommand() {
Command<String, String, String> command = builder.ftDropindex(MY_KEY, false);
ByteBuf buf = Unpooled.directBuffer();
command.encode(buf);

String result = "*2\r\n" + "$12\r\n" + "FT.DROPINDEX\r\n" + "$3\r\n" + MY_KEY + "\r\n";

assertThat(buf.toString(StandardCharsets.UTF_8)).isEqualTo(result);
}

@Test
void shouldCorrectlyConstructFtDropindexCommandDd() {
Command<String, String, String> command = builder.ftDropindex(MY_KEY, true);
ByteBuf buf = Unpooled.directBuffer();
command.encode(buf);

String result = "*3\r\n" + "$12\r\n" + "FT.DROPINDEX\r\n" + "$3\r\n" + MY_KEY + "\r\n" + "$2\r\n" + "DD\r\n";

assertThat(buf.toString(StandardCharsets.UTF_8)).isEqualTo(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -151,12 +152,12 @@ void connectShouldFail() throws Exception {
StopWatch stopWatch = new StopWatch();

assertThatThrownBy(() -> TestFutures.awaitOrTimeout(sut.getConnection(connectionKey)))
.hasCauseInstanceOf(ConnectTimeoutException.class);
.hasRootCauseInstanceOf(ConnectException.class);

stopWatch.start();

assertThatThrownBy(() -> TestFutures.awaitOrTimeout(sut.getConnection(connectionKey)))
.hasCauseInstanceOf(ConnectTimeoutException.class);
.hasRootCauseInstanceOf(ConnectException.class);

stopWatch.stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.lettuce.core.json;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisCommandExecutionException;
import io.lettuce.core.RedisContainerIntegrationTests;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.sync.RedisCommands;
Expand All @@ -26,6 +27,7 @@

import static io.lettuce.TestTags.INTEGRATION_TEST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@Tag(INTEGRATION_TEST)
public class RediSearchIntegrationTests extends RedisContainerIntegrationTests {
Expand Down Expand Up @@ -79,6 +81,9 @@ void ftCreateScenario1() {

String result = redis.ftCreate(GENERIC_INDEX, createArgs, Arrays.asList(field1, field2, field3));
assertThat(result).isEqualTo("OK");

result = redis.ftDropindex(GENERIC_INDEX, false);
assertThat(result).isEqualTo("OK");
}

}

0 comments on commit e3afb06

Please sign in to comment.