Hello RediSearch

install

1
2
docker pull redislabs/redisearch:latest
docker run --name redisearch -d -p 6379:6379 redislabs/redisearch:latest

dependency

1
2
3
4
5
6
7
<dependencies>
<dependency>
<groupId>com.redislabs</groupId>
<artifactId>jredisearch</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>

create client

Initializing the client with JedisPool:

1
Client client = new Client("testing", "localhost", 6379);

Initializing the client with JedisSentinelPool:

1
2
3
4
5
6
7
8
9
10
private static final String MASTER_NAME = "mymaster";
private static final Set<String> sentinels;
static {
sentinels = new HashSet();
sentinels.add("localhost:7000");
sentinels.add("localhost:7001");
sentinels.add("localhost:7002");
}
...
Client client = new Client("testung", MASTER_NAME, sentinels);

create schema

1
2
3
4
5
6
7
8
// Create schema
Schema sc = new Schema().addTextField("name", 1.0)
.addNumericField("age")
.addTagField("job", ",")
.addSortableTextField("welfare", 1.0)
.addSortableNumericField("income")
.addSortableTagField("dream", ",");
client.createIndex(sc, Client.IndexOptions.Default());

adding documents

1
2
3
4
5
6
7
8
9
// Create document
Map<String, Object> fields = new HashMap<>();
fields.put("name", "hello world");
fields.put("age", 45);
fields.put("job", "programmer");
fields.put("welfare", "996");
fields.put("income", "3000");
fields.put("dream", "handsome, rich");
client.addDocument("doc1", fields);
1
2
3
4
5
// actual search
Query q = new Query("hello world")
.addFilter(new Query.NumericFilter("age", 0, 1000))
.limit(0,5);
SearchResult res = client.search(q);
1
2
3
4
5
6
7
// aggregation query
AggregationBuilder r = new AggregationBuilder("hello")
.apply("@income", "k")
.groupBy("@job", Reducers.avg("@k").as("avgIncome"))
.filter("@avgIncome>=2")
.sortBy(10, SortedField.asc("@dream"));
AggregationResult res = client.aggregate(r);

https://github.com/RediSearch/RediSearch
https://github.com/RediSearch/JRediSearch