How to Search Data Stored in Redis on Java

How Data Is Stored in Redis

Redis is a widely used, open-source, in-memory data structure store with a variety of applications, including NoSQL databases, caches, and message brokers. The most basic data type in Redis is the string, which can store up to 512 megabytes of binary data.

Redis strings can be used to store text, numbers, and even images, videos, and audio files. In addition, multiple Redis strings can be stored together in a data structure such as a list, set, or hash.

With this flexible combination of data types, Redis users can construct complex data representations for many different use cases. All Redis data is stored in-memory rather than on disk, which enables low latency and high performance—one of the biggest reasons for Redis’ enduring popularity among developers and database engineers.

Using Java With Redis

Data types such as strings, lists, sets, and hashes should be familiar to software developers who use programming languages such as Java. The Java programming language comes with its own set of data structures—including Strings, Lists, Sets, and Hashtables—with functionality very similar to the data types in Redis.

However, there’s one issue: Redis doesn’t come with built-in support for programming languages such as Java. Instead, Java programmers who want to work with Redis can use third-party Redis Java frameworks like Redisson.

Redisson is an open-source Redis Java client that makes it simple for Java developers to get started using Redis. The Redisson library provides dozens of data structures, objects, and services that should be familiar to any Java programmer. Using these constructs, Redisson users can easily connect to a Redis instance and start storing and querying data in Java.

Searching Redis Data in Java With Redisson

Redis provides the RediSearch module, which enables features such as querying, secondary indexing, full-text search, and more for data stored in Redis. Redisson integrates with the RediSearch module, allowing Redisson users to start searching Redis data in Java.

In particular, Redisson supports field indexing, query execution, and aggregation using the RMap and RJsonBucket interfaces. Below is a code example of how to execute queries with Redisson and RediSearch using an RMap object:

Codec codec = new CompositeCodec(StringCodec.INSTANCE, redisson.getConfig().getCodec());

RMap m = redisson.getMap("doc:1", codec);
m.put("v1", new SimpleObject("name1"));
m.put("v2", new SimpleObject("name2"));

RMap m2 = redisson.getMap("doc:2", codec);
m2.put("v1", new SimpleObject("name3"));
m2.put("v2", new SimpleObject("name4"));

RSearch s = redisson.getSearch();
s.createIndex("idx", IndexOptions.defaults()
                                 .on(IndexType.HASH)
                                 .prefix(Arrays.asList("doc:")),
                                         FieldIndex.text("v1"),
                                         FieldIndex.text("v2"));

SearchResult r = s.search("idx", "*", QueryOptions.defaults()
                 .returnAttributes(new ReturnAttribute("v1"), new ReturnAttribute("v2")));

The createIndex() function is used to create an index using the provided name and options. Then, the search() function searches the given index for a particular value while using the specified options.

The code example below demonstrates how to execute a similar query using RJsonBucket instead of RMap:

public class TestClass {

        private List arr;
        private String value;

        public TestClass() {
        }

        public TestClass(List arr, String value) {
            this.arr = arr;
            this.value = value;
        }

        public List getArr() {
            return arr;
        }

        public TestClass setArr(List arr) {
            this.arr = arr;
            return this;
        }

        public String getValue() {
            return value;
        }

        public TestClass setValue(String value) {
            this.value = value;
            return this;
        }
}

RJsonBucket b = redisson.getJsonBucket("doc:1", new JacksonCodec<>(TestClass.class));
b.set(new TestClass(Arrays.asList(1, 2, 3), "hello"));

RSearch s = redisson.getSearch(StringCodec.INSTANCE);
s.createIndex("idx", IndexOptions.defaults()
                                  .on(IndexType.JSON)
                                  .prefix(Arrays.asList("doc:")),
                                          FieldIndex.numeric("$..arr").as("arr"),
                                          FieldIndex.text("$..value").as("val"));

SearchResult r = s.search("idx", "*", QueryOptions.defaults()
                    .returnAttributes(new ReturnAttribute("arr"), new ReturnAttribute("val")));

Aggregating Redis Data in Java With Redisson

In addition to searching Redis data in Java, Redisson allows users to aggregate information, grouping records together with a single query. We will show how to do this using both RMap and RJsonBucket.

To aggregate data instead of querying it, the final line in the RMap example above should be replaced with:

AggregationResult r = s.aggregate("idx", "*", AggregationOptions.defaults() .load("v1", "v2"));

Note the three differences from the code sample above:

  • The function used is aggregate() instead of search().

  • The return type is AggregationResult instead of SearchResult.

  • The provided options are of type AggregationOptions instead of QueryOptions.

Similarly, to aggregate data with RJsonBucket instead of querying it, the final line in the example above should be replaced with:

AggregationResult r = s.aggregate("idx", "*", AggregationOptions.defaults() .load("arr", "val"));

In addition to searching and aggregating data stored in Redis, Java developers can use Redisson for various other functions. For example, the code sample below demonstrates how to run a spellcheck on text data stored in Redis using Java and Redisson:

RSearch s = redisson.getSearch();

s.createIndex("idx", IndexOptions.defaults()
                                 .on(IndexType.HASH)
                                 .prefix(Arrays.asList("doc:")),
                             FieldIndex.text("t1"),
                             FieldIndex.text("t2"));

s.addDict("name", "hockey", "stik");

Map<String, Map<String, Double>> res = s.spellcheck("idx", "Hocke sti", SpellcheckOptions.defaults()
                    .includedTerms("name"));
Getting Started With Redisson
Similar articles