Skip to content

Commit

Permalink
RediSearch commands implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Nov 10, 2021
1 parent d2b46ba commit 7cc507c
Show file tree
Hide file tree
Showing 15 changed files with 662 additions and 63 deletions.
30 changes: 29 additions & 1 deletion src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import redis.clients.jedis.resps.LCSMatchResult.MatchedPosition;
import redis.clients.jedis.resps.LCSMatchResult.Position;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.search.aggr.AggregationResult;
import redis.clients.jedis.stream.*;
import redis.clients.jedis.util.JedisByteHashMap;
import redis.clients.jedis.util.SafeEncoder;
Expand Down Expand Up @@ -1221,7 +1222,7 @@ public LCSMatchResult build(Object data) {
}
};

public static final Builder<Class<?>> REDIS_JSON_TYPE = new Builder<Class<?>>() {
public static final Builder<Class<?>> JSON_TYPE = new Builder<Class<?>>() {
@Override
public Class<?> build(Object data) {
String str = STRING.build(data);
Expand Down Expand Up @@ -1251,6 +1252,33 @@ public String toString() {
}
};

public static final Builder<AggregationResult> SEARCH_AGGREGATION_RESULT = new Builder<AggregationResult>() {
@Override
public AggregationResult build(Object data) {
return new AggregationResult(data);
}
};

public static final Builder<AggregationResult> SEARCH_AGGREGATION_RESULT_WITH_CURSOR = new Builder<AggregationResult>() {
@Override
public AggregationResult build(Object data) {
List<Object> list = (List<Object>) data;
return new AggregationResult(list.get(0), (long) list.get(1));
}
};

public static final Builder<Map<String, List<Object>>> SEARCH_SYNONYM_GROUPS = new Builder<Map<String, List<Object>>>() {
@Override
public Map<String, List<Object>> build(Object data) {
List<Object> list = (List<Object>) data;
Map<String, List<Object>> dump = new HashMap<>(list.size() / 2);
for (int i = 0; i < list.size(); i += 2) {
dump.put(STRING.build(list.get(i)), ENCODED_OBJECT_LIST.build(list.get(i + 1)));
}
return dump;
}
};

/**
* A decorator to implement Set from List. Assume that given List do not contains duplicated
* values. The resulting set displays the same ordering, concurrency, and performance
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/redis/clients/jedis/RedisCommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import redis.clients.jedis.search.SearchProtocol.SearchCommand;
import redis.clients.jedis.search.SearchProtocol.SearchKeyword;
import redis.clients.jedis.search.SearchResult.SearchResultBuilder;
import redis.clients.jedis.search.aggr.AggregationBuilder;
import redis.clients.jedis.search.aggr.AggregationResult;
import redis.clients.jedis.stream.*;

public class RedisCommandObjects {
Expand Down Expand Up @@ -2443,6 +2445,13 @@ public CommandObject<String> ftCreate(String indexName, IndexOptions indexOption
return new CommandObject<>(args, BuilderFactory.STRING);
}

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

public CommandObject<SearchResult> ftSearch(String indexName, Query query) {
return new CommandObject<>(commandArguments(SearchCommand.SEARCH).add(indexName).addParams(query),
new SearchResultBuilder(!query.getNoContent(), query.getWithScores(), query.getWithPayloads(), true));
Expand All @@ -2452,6 +2461,29 @@ public CommandObject<SearchResult> ftSearch(byte[] indexName, Query query) {
return new CommandObject<>(commandArguments(SearchCommand.SEARCH).add(indexName).addParams(query),
new SearchResultBuilder(!query.getNoContent(), query.getWithScores(), query.getWithPayloads(), false));
}

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

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

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

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

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

// RediSearch commands

// RedisJSON commands
Expand Down Expand Up @@ -2508,11 +2540,11 @@ public final CommandObject<String> jsonToggle(String key, Path path) {
}

public final CommandObject<Class<?>> jsonType(String key) {
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key), BuilderFactory.REDIS_JSON_TYPE);
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key), BuilderFactory.JSON_TYPE);
}

public final CommandObject<Class<?>> jsonType(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key).add(path), BuilderFactory.REDIS_JSON_TYPE);
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key).add(path), BuilderFactory.JSON_TYPE);
}

public final CommandObject<Long> jsonStrAppend(String key, Path path, Object... objects) {
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.commands.JedisCommands;
import redis.clients.jedis.commands.JedisBinaryCommands;
import redis.clients.jedis.search.aggr.AggregationBuilder;
import redis.clients.jedis.search.aggr.AggregationResult;

public class UnifiedJedis implements JedisCommands, JedisBinaryCommands,
SampleKeyedCommands, SampleBinaryKeyedCommands, RedisModuleCommands,
Expand Down Expand Up @@ -2784,6 +2786,11 @@ public String ftCreate(String indexName, IndexOptions indexOptions, Schema schem
return executeCommand(commandObjects.ftCreate(indexName, indexOptions, schema));
}

@Override
public String ftAlter(String indexName, Schema schema) {
return executeCommand(commandObjects.ftAlter(indexName, schema));
}

@Override
public SearchResult ftSearch(String indexName, Query query) {
return executeCommand(commandObjects.ftSearch(indexName, query));
Expand All @@ -2793,6 +2800,72 @@ public SearchResult ftSearch(String indexName, Query query) {
public SearchResult ftSearch(byte[] indexName, Query query) {
return executeCommand(commandObjects.ftSearch(indexName, query));
}

@Override
public String ftExplain(String indexName, Query query) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public List<String> ftExplainCLI(String indexName, Query query) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public AggregationResult ftAggregate(String indexName, AggregationBuilder aggr) {
return executeCommand(commandObjects.ftAggregate(indexName, aggr));
}

@Override
public String ftDropIndex(String indexName) {
return executeCommand(commandObjects.ftDropIndex(indexName));
}

@Override
public String ftDropIndexDD(String indexName) {
return executeCommand(commandObjects.ftDropIndexDD(indexName));
}

@Override
public String ftSynUpdate(String indexName, String synonymGroupId, String... terms) {
return executeCommand(commandObjects.ftSynUpdate(indexName, synonymGroupId, terms));
}

@Override
public Map<String, List<Object>> ftSynDump(String indexName) {
return executeCommand(commandObjects.ftSynDump(indexName));
}

@Override
public Map<String, Object> ftInfo(String indexName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String ftAliasAdd(String aliasName, String indexName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String ftAliasUpdate(String aliasName, String indexName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String ftAliasDel(String aliasName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Map<String, String> ftConfigGet(String option) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String ftConfigSet(String option, String value) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

// RediSearch commands

// RedisJSON commands
Expand Down
50 changes: 25 additions & 25 deletions src/main/java/redis/clients/jedis/search/FieldName.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package redis.clients.jedis.search;

import java.util.Collection;
import java.util.List;
import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.params.IParams;

import redis.clients.jedis.util.SafeEncoder;

public class FieldName implements IParams {

// private static final String AS_ENCODED = "AS";
// private static final byte[] AS_BINARY = SafeEncoder.encode(AS_ENCODED);
private static final String AS_ENCODED = "AS";
private static final byte[] AS_BINARY = SafeEncoder.encode(AS_ENCODED);
private static final byte[] AS = SafeEncoder.encode("AS");

private final String name;
Expand All @@ -23,28 +23,28 @@ public FieldName(String name, String attribute) {
this.name = name;
this.attribute = attribute;
}
//
// public int addCommandEncodedArguments(Collection<String> args) {
// args.add(name);
// if (attribute == null) {
// return 1;
// }
//
// args.add(AS_ENCODED);
// args.add(attribute);
// return 3;
// }
//
// public int addCommandBinaryArguments(Collection<byte[]> args) {
// args.add(SafeEncoder.encode(name));
// if (attribute == null) {
// return 1;
// }
//
// args.add(AS_BINARY);
// args.add(SafeEncoder.encode(attribute));
// return 3;
// }

public int addCommandEncodedArguments(List<String> args) {
args.add(name);
if (attribute == null) {
return 1;
}

args.add(AS_ENCODED);
args.add(attribute);
return 3;
}

public int addCommandBinaryArguments(List<byte[]> args) {
args.add(SafeEncoder.encode(name));
if (attribute == null) {
return 1;
}

args.add(AS_BINARY);
args.add(SafeEncoder.encode(attribute));
return 3;
}

public int addCommandArguments(CommandArguments args) {
args.add(SafeEncoder.encode(name));
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/redis/clients/jedis/search/Query.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package redis.clients.jedis.search;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import redis.clients.jedis.CommandArguments;

import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.args.Rawable;
import redis.clients.jedis.params.IParams;
Expand Down Expand Up @@ -51,13 +50,6 @@ public NumericFilter(String property, double min, double max) {
}

private byte[] formatNum(double num, boolean exclude) {
if (num == Double.POSITIVE_INFINITY) {
return SearchKeyword.POSITIVE_INFINITY.getRaw();
}
if (num == Double.NEGATIVE_INFINITY) {
return SearchKeyword.NEGATIVE_INFINITY.getRaw();
}

return exclude ? SafeEncoder.encode("(" + num) : Protocol.toByteArray(num);
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/redis/clients/jedis/search/RediSearchCommands.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
package redis.clients.jedis.search;

import java.util.List;
import java.util.Map;
import redis.clients.jedis.search.aggr.AggregationBuilder;
import redis.clients.jedis.search.aggr.AggregationResult;

public interface RediSearchCommands {

String ftCreate(String indexName, IndexOptions indexOptions, Schema schema);

default String ftAlter(String indexName, Schema.Field... fields) {
return ftAlter(indexName, Schema.from(fields));
}

String ftAlter(String indexName, Schema schema);

SearchResult ftSearch(String indexName, Query query);

SearchResult ftSearch(byte[] indexName, Query query);

String ftExplain(String indexName, Query query);

List<String> ftExplainCLI(String indexName, Query query);

AggregationResult ftAggregate(String indexName, AggregationBuilder aggr);

String ftDropIndex(String indexName);

String ftDropIndexDD(String indexName);

String ftSynUpdate(String indexName, String synonymGroupId, String... terms);

Map<String, List<Object>> ftSynDump(String indexName);

Map<String, Object> ftInfo(String indexName);

String ftAliasAdd(String aliasName, String indexName);

String ftAliasUpdate(String aliasName, String indexName);

String ftAliasDel(String aliasName);

Map<String, String> ftConfigGet(String option);

String ftConfigSet(String option, String value);
}
19 changes: 9 additions & 10 deletions src/main/java/redis/clients/jedis/search/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public Schema() {
this.fields = new ArrayList<>();
}

public static Schema from(Field... fields) {
Schema schema = new Schema();
for (Field field : fields) {
schema.addField(field);
}
return schema;
}

/**
* Add a text field to the schema with a given weight
*
Expand Down Expand Up @@ -130,16 +138,7 @@ public Field(FieldName name, FieldType type, boolean sortable, boolean noIndex)
}

@Override
public void addParams(CommandArguments args) {

}

/**
* Sub-classes should not override this method.
*
* @param args
*/
public void serializeRedisArgs(CommandArguments args) {
public final void addParams(CommandArguments args) {
this.fieldName.addParams(args);
args.add(type.name());
addTypeArgs(args);
Expand Down
Loading

0 comments on commit 7cc507c

Please sign in to comment.