Skip to content

Commit

Permalink
Direct FT.CREATE to proper node in LIGHT search mode (#3593)
Browse files Browse the repository at this point in the history
along with some other commands.

FT.SEARCH and some other commads were already being directed.
  • Loading branch information
sazzad16 committed Nov 20, 2023
1 parent 9fe49df commit 0aea49e
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -3163,8 +3163,24 @@ private boolean isRoundRobinSearchCommand() {
return true;
}

private CommandArguments checkAndRoundRobinSearchCommand(CommandArguments commandArguments, String indexName) {
return isRoundRobinSearchCommand() ? commandArguments.add(indexName) : commandArguments.key(indexName);
private CommandArguments checkAndRoundRobinSearchCommand(SearchCommand sc, String idx) {
CommandArguments ca = commandArguments(sc);
if (isRoundRobinSearchCommand()) {
ca.add(idx);
} else {
ca.key(idx);
}
return ca;
}

private CommandArguments checkAndRoundRobinSearchCommand(SearchCommand sc, String idx1, String idx2) {
CommandArguments ca = commandArguments(sc);
if (isRoundRobinSearchCommand()) {
ca.add(idx1).add(idx2);
} else {
ca.key(idx1).key(idx2);
}
return ca;
}

private CommandArguments checkAndRoundRobinSearchCommand(CommandArguments commandArguments, byte[] indexName) {
Expand All @@ -3177,46 +3193,46 @@ private <T> CommandObject<T> directSearchCommand(CommandObject<T> object, String
}

public final CommandObject<String> ftCreate(String indexName, IndexOptions indexOptions, Schema schema) {
CommandArguments args = commandArguments(SearchCommand.CREATE).add(indexName)
CommandArguments args = checkAndRoundRobinSearchCommand(SearchCommand.CREATE, indexName)
.addParams(indexOptions).add(SearchKeyword.SCHEMA);
schema.fields.forEach(field -> args.addParams(field));
return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<String> ftCreate(String indexName, FTCreateParams createParams,
Iterable<SchemaField> schemaFields) {
CommandArguments args = commandArguments(SearchCommand.CREATE).add(indexName)
CommandArguments args = checkAndRoundRobinSearchCommand(SearchCommand.CREATE, indexName)
.addParams(createParams).add(SearchKeyword.SCHEMA);
schemaFields.forEach(field -> args.addParams(field));
return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<String> ftAlter(String indexName, Schema schema) {
CommandArguments args = commandArguments(SearchCommand.ALTER).add(indexName)
CommandArguments args = checkAndRoundRobinSearchCommand(SearchCommand.ALTER, indexName)
.add(SearchKeyword.SCHEMA).add(SearchKeyword.ADD);
schema.fields.forEach(field -> args.addParams(field));
return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<String> ftAlter(String indexName, Iterable<SchemaField> schemaFields) {
CommandArguments args = commandArguments(SearchCommand.ALTER).add(indexName)
CommandArguments args = checkAndRoundRobinSearchCommand(SearchCommand.ALTER, indexName)
.add(SearchKeyword.SCHEMA).add(SearchKeyword.ADD);
schemaFields.forEach(field -> args.addParams(field));
return new CommandObject<>(args, BuilderFactory.STRING);
}

public final CommandObject<SearchResult> ftSearch(String indexName, String query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SEARCH), indexName).add(query),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SEARCH, indexName).add(query),
new SearchResultBuilder(true, false, false, true));
}

public final CommandObject<SearchResult> ftSearch(String indexName, String query, FTSearchParams params) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SEARCH), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SEARCH, indexName)
.add(query).addParams(params), new SearchResultBuilder(!params.getNoContent(), params.getWithScores(), false, true));
}

public final CommandObject<SearchResult> ftSearch(String indexName, Query query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SEARCH), indexName).addParams(query),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SEARCH, indexName).addParams(query),
new SearchResultBuilder(!query.getNoContent(), query.getWithScores(), query.getWithPayloads(), true));
}

Expand All @@ -3226,35 +3242,35 @@ public final CommandObject<SearchResult> ftSearch(byte[] indexName, Query query)
}

public final CommandObject<String> ftExplain(String indexName, Query query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.EXPLAIN), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.EXPLAIN, indexName)
.addParams(query), BuilderFactory.STRING);
}

public final CommandObject<List<String>> ftExplainCLI(String indexName, Query query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.EXPLAINCLI), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.EXPLAINCLI, indexName)
.addParams(query), BuilderFactory.STRING_LIST);
}

public final CommandObject<AggregationResult> ftAggregate(String indexName, AggregationBuilder aggr) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.AGGREGATE), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.AGGREGATE, indexName)
.addObjects(aggr.getArgs()), !aggr.isWithCursor() ? SearchBuilderFactory.SEARCH_AGGREGATION_RESULT
: SearchBuilderFactory.SEARCH_AGGREGATION_RESULT_WITH_CURSOR);
}

public final CommandObject<AggregationResult> ftCursorRead(String indexName, long cursorId, int count) {
return new CommandObject<>(commandArguments(SearchCommand.CURSOR).add(SearchKeyword.READ)
.add(indexName).add(cursorId).add(SearchKeyword.COUNT).add(count),
.key(indexName).add(cursorId).add(SearchKeyword.COUNT).add(count),
SearchBuilderFactory.SEARCH_AGGREGATION_RESULT_WITH_CURSOR);
}

public final CommandObject<String> ftCursorDel(String indexName, long cursorId) {
return new CommandObject<>(commandArguments(SearchCommand.CURSOR).add(SearchKeyword.DEL)
.add(indexName).add(cursorId), BuilderFactory.STRING);
.key(indexName).add(cursorId), BuilderFactory.STRING);
}

public final CommandObject<Map.Entry<AggregationResult, Map<String, Object>>> ftProfileAggregate(
String indexName, FTProfileParams profileParams, AggregationBuilder aggr) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.PROFILE), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.PROFILE, indexName)
.add(SearchKeyword.AGGREGATE).addParams(profileParams).add(SearchKeyword.QUERY)
.addObjects(aggr.getArgs()), new SearchProfileResponseBuilder<>(!aggr.isWithCursor()
? SearchBuilderFactory.SEARCH_AGGREGATION_RESULT
Expand All @@ -3263,36 +3279,36 @@ public final CommandObject<Map.Entry<AggregationResult, Map<String, Object>>> ft

public final CommandObject<Map.Entry<SearchResult, Map<String, Object>>> ftProfileSearch(
String indexName, FTProfileParams profileParams, Query query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.PROFILE), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.PROFILE, indexName)
.add(SearchKeyword.SEARCH).addParams(profileParams).add(SearchKeyword.QUERY)
.addParams(query), new SearchProfileResponseBuilder<>(new SearchResultBuilder(
!query.getNoContent(), query.getWithScores(), query.getWithPayloads(), true)));
}

public final CommandObject<Map.Entry<SearchResult, Map<String, Object>>> ftProfileSearch(
String indexName, FTProfileParams profileParams, String query, FTSearchParams searchParams) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.PROFILE), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.PROFILE, indexName)
.add(SearchKeyword.SEARCH).addParams(profileParams).add(SearchKeyword.QUERY).add(query)
.addParams(searchParams), new SearchProfileResponseBuilder<>(new SearchResultBuilder(
!searchParams.getNoContent(), searchParams.getWithScores(), false, true)));
}

public final CommandObject<String> ftDropIndex(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.DROPINDEX).add(indexName), BuilderFactory.STRING);
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.DROPINDEX, indexName), BuilderFactory.STRING);
}

public final CommandObject<String> ftDropIndexDD(String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.DROPINDEX).add(indexName).add(SearchKeyword.DD),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.DROPINDEX, indexName).add(SearchKeyword.DD),
BuilderFactory.STRING);
}

public final CommandObject<String> ftSynUpdate(String indexName, String synonymGroupId, String... terms) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SYNUPDATE), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SYNUPDATE, indexName)
.add(synonymGroupId).addObjects((Object[]) terms), BuilderFactory.STRING);
}

public final CommandObject<Map<String, List<String>>> ftSynDump(String indexName) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SYNDUMP), indexName),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SYNDUMP, indexName),
SearchBuilderFactory.SEARCH_SYNONYM_GROUPS);
}

Expand Down Expand Up @@ -3323,36 +3339,36 @@ public final CommandObject<Set<String>> ftDictDumpBySampleKey(String indexName,
}

public final CommandObject<Map<String, Map<String, Double>>> ftSpellCheck(String index, String query) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SPELLCHECK), index).add(query),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SPELLCHECK, index).add(query),
SearchBuilderFactory.SEARCH_SPELLCHECK_RESPONSE);
}

public final CommandObject<Map<String, Map<String, Double>>> ftSpellCheck(String index, String query,
FTSpellCheckParams spellCheckParams) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.SPELLCHECK), index).add(query)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.SPELLCHECK, index).add(query)
.addParams(spellCheckParams), SearchBuilderFactory.SEARCH_SPELLCHECK_RESPONSE);
}

public final CommandObject<Map<String, Object>> ftInfo(String indexName) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.INFO), indexName),
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.INFO, indexName),
BuilderFactory.ENCODED_OBJECT_MAP);
}

public final CommandObject<Set<String>> ftTagVals(String indexName, String fieldName) {
return new CommandObject<>(checkAndRoundRobinSearchCommand(commandArguments(SearchCommand.TAGVALS), indexName)
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.TAGVALS, indexName)
.add(fieldName), BuilderFactory.STRING_SET);
}

public final CommandObject<String> ftAliasAdd(String aliasName, String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASADD).add(aliasName).add(indexName), BuilderFactory.STRING);
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.ALIASADD, aliasName, indexName), BuilderFactory.STRING);
}

public final CommandObject<String> ftAliasUpdate(String aliasName, String indexName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASUPDATE).add(aliasName).add(indexName), BuilderFactory.STRING);
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.ALIASUPDATE, aliasName, indexName), BuilderFactory.STRING);
}

public final CommandObject<String> ftAliasDel(String aliasName) {
return new CommandObject<>(commandArguments(SearchCommand.ALIASDEL).add(aliasName), BuilderFactory.STRING);
return new CommandObject<>(checkAndRoundRobinSearchCommand(SearchCommand.ALIASDEL, aliasName), BuilderFactory.STRING);
}

public final CommandObject<Map<String, String>> ftConfigGet(String option) {
Expand Down

0 comments on commit 0aea49e

Please sign in to comment.