VOOZH about

URL: https://redis.io/docs/latest/develop/clients/jedis/queryjson/

⇱ Index and query documents | Docs


{"categories":["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],"description":"Learn how to use Redis Search with JSON and hash documents.","duplicateOf":"head:data-ai-metadata","location":"body","relatedPages":["/develop/clients/jedis/vecsearch","/develop/ai/search-and-query"],"scope":"example","title":"Index and query documents","topics":["Redis Search","JSON","hash"],"tableOfContents":{"sections":[{"id":"initialize","title":"Initialize"},{"id":"create-data","title":"Create data"},{"id":"add-the-index","title":"Add the index"},{"id":"add-the-data","title":"Add the data"},{"id":"query-the-data","title":"Query the data"},{"id":"differences-with-hash-documents","title":"Differences with hash documents"},{"id":"more-information","title":"More information"}]},"codeExamples":[{"codetabsId":"java_home_json-stepimport","description":"Foundational: Import required Jedis classes for JSON indexing and query operations","difficulty":"beginner","id":"import","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepimport"}]},{"codetabsId":"java_home_json-stepcreate_data","description":"Foundational: Define sample user data structures for indexing and querying","difficulty":"beginner","id":"create_data","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepcreate_data"}]},{"codetabsId":"java_home_json-stepconnect","description":"Foundational: Establish a connection to a Redis server for query operations","difficulty":"beginner","id":"connect","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepconnect"}]},{"codetabsId":"java_home_json-stepcleanup_json","description":"Foundational: Clean up existing indexes and data before creating new indexes","difficulty":"beginner","id":"cleanup_json","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepcleanup_json"}]},{"codetabsId":"java_home_json-stepmake_index","description":"Foundational: Create a search index for JSON documents with field definitions and key prefix filtering","difficulty":"intermediate","id":"make_index","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepmake_index"}]},{"codetabsId":"java_home_json-stepadd_data","description":"Foundational: Store JSON documents in Redis with automatic indexing based on key prefix","difficulty":"beginner","id":"add_data","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepadd_data"}]},{"codetabsId":"java_home_json-stepquery1","description":"Query with filters: Search JSON documents using text matching and numeric range filters to find specific records","difficulty":"intermediate","id":"query1","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepquery1"}]},{"codetabsId":"java_home_json-stepquery2","description":"Query with field projection: Retrieve only specific fields from search results to reduce data transfer","difficulty":"intermediate","id":"query2","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepquery2"}]},{"codetabsId":"java_home_json-stepquery3","description":"Aggregation query: Group and count results by field values to analyze data patterns","difficulty":"intermediate","id":"query3","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepquery3"}]},{"codetabsId":"java_home_json-stepcleanup_hash","description":"Foundational: Clean up existing hash indexes and data before creating new indexes","difficulty":"beginner","id":"cleanup_hash","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepcleanup_hash"}]},{"codetabsId":"java_home_json-stepmake_hash_index","description":"Foundational: Create a search index for hash documents with field definitions and key prefix filtering","difficulty":"intermediate","id":"make_hash_index","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepmake_hash_index"}]},{"codetabsId":"java_home_json-stepadd_hash_data","description":"Foundational: Store hash documents in Redis with automatic indexing based on key prefix","difficulty":"beginner","id":"add_hash_data","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepadd_hash_data"}]},{"codetabsId":"java_home_json-stepquery1_hash","description":"Query with filters: Search hash documents using text matching and numeric range filters (same as JSON queries)","difficulty":"intermediate","id":"query1_hash","languages":[{"clientId":"jedis","clientName":"Jedis","id":"Java-Sync","langId":"java","panelId":"panel_Java-Sync_java_home_json-stepquery1_hash"}]}]}

Index and query documents

Learn how to use Redis Search with JSON and hash documents.

This example shows how to create a search index for JSON documents and run queries against the index. It then goes on to show the slight differences in the equivalent code for hash documents.

Note:
From v6.0.0 onwards, Jedis uses query dialect 2 by default. Redis Search methods such as ftSearch() will explicitly request this dialect, overriding the default set for the server. See Query dialects for more information.

Initialize

Make sure that you have Redis Open Source or another Redis server available. Also install the Jedis client library if you haven't already done so.

Add the following dependencies. All of them are applicable to both JSON and hash, except for the Path and JSONObject classes, which are specific to JSON (see Path for a description of the JSON path syntax).

Foundational: Import required Jedis classes for JSON indexing and query operations
importredis.clients.jedis.RedisClient;importredis.clients.jedis.exceptions.JedisDataException;importredis.clients.jedis.json.Path2;importredis.clients.jedis.search.*;importredis.clients.jedis.search.aggr.*;importredis.clients.jedis.search.schemafields.*;importorg.json.JSONObject;importjava.util.HashMap;importjava.util.List;importjava.util.Map;

Create data

Create some test data to add to the database:

Foundational: Define sample user data structures for indexing and querying
JSONObjectuser1=newJSONObject().put("name","Paul John").put("email","[email protected]").put("age",42).put("city","London");JSONObjectuser2=newJSONObject().put("name","Eden Zamir").put("email","[email protected]").put("age",29).put("city","Tel Aviv");JSONObjectuser3=newJSONObject().put("name","Paul Zamir").put("email","[email protected]").put("age",35).put("city","Tel Aviv");

Add the index

Connect to your Redis database. The code below shows the most basic connection but see Connect to the server to learn more about the available connection options.

Foundational: Establish a connection to a Redis server for query operations
RedisClientjedis=newRedisClient("redis://localhost:6379");

Delete any existing index called idx:users and any keys that start with user:.

Foundational: Clean up existing indexes and data before creating new indexes
try{jedis.ftDropIndex("idx:users");}catch(JedisDataExceptionj){}jedis.del("user:1","user:2","user:3");

Create an index. In this example, only JSON documents with the key prefix user: are indexed. For more information, see Query syntax.

Foundational: Create a search index for JSON documents with field definitions and key prefix filtering
SchemaField[]schema={TextField.of("$.name").as("name"),TextField.of("$.city").as("city"),NumericField.of("$.age").as("age")};StringcreateResult=jedis.ftCreate("idx:users",FTCreateParams.createParams().on(IndexDataType.JSON).addPrefix("user:"),schema);System.out.println(createResult);// >>> OK

Add the data

Add the three sets of user data to the database as JSON objects. If you use keys with the user: prefix then Redis will index the objects automatically as you add them:

Foundational: Store JSON documents in Redis with automatic indexing based on key prefix
Stringuser1Set=jedis.jsonSet("user:1",newPath2("$"),user1);Stringuser2Set=jedis.jsonSet("user:2",newPath2("$"),user2);Stringuser3Set=jedis.jsonSet("user:3",newPath2("$"),user3);

Query the data

You can now use the index to search the JSON objects. The query below searches for objects that have the text "Paul" in any field and have an age value in the range 30 to 40:

Query with filters: Search JSON documents using text matching and numeric range filters to find specific records
SearchResultfindPaulResult=jedis.ftSearch("idx:users","Paul @age:[30 40]");System.out.println(findPaulResult.getTotalResults());// >>> 1List<Document>paulDocs=findPaulResult.getDocuments();for(Documentdoc:paulDocs){System.out.println(doc.getId());}// >>> user:3

Specify query options to return only the city field:

Query with field projection: Retrieve only specific fields from search results to reduce data transfer
SearchResultcitiesResult=jedis.ftSearch("idx:users","Paul",FTSearchParams.searchParams().returnFields("city"));System.out.println(citiesResult.getTotalResults());// >>> 2for(Documentdoc:citiesResult.getDocuments()){System.out.println(doc.getId());}// >>> user:1// >>> user:3

Use an aggregation query to count all users in each city.

Aggregation query: Group and count results by field values to analyze data patterns
AggregationResultaggResult=jedis.ftAggregate("idx:users",newAggregationBuilder("*").groupBy("@city",Reducers.count().as("count")));System.out.println(aggResult.getTotalResults());// >>> 2for(RowcityRow:aggResult.getRows()){System.out.printf("%s - %d%n",cityRow.getString("city"),cityRow.getLong("count"));}// >>> London - 1// >>> Tel Aviv - 2

Differences with hash documents

Indexing for hash documents is very similar to JSON indexing but you need to specify some slightly different options.

When you create the schema for a hash index, you don't need to add aliases for the fields, since you use the basic names to access the fields anyway. Also, you must use IndexDataType.HASH for the On() option of FTCreateParams when you create the index. The code below shows these changes with a new index called hash-idx:users, which is otherwise the same as the idx:users index used for JSON documents in the previous examples.

First, delete any existing index called hash-idx:users and any keys that start with huser:.

Foundational: Clean up existing hash indexes and data before creating new indexes
try{jedis.ftDropIndex("hash-idx:users");}catch(JedisDataExceptionj){}jedis.del("huser:1","huser:2","huser:3");

Now create the new index:

Foundational: Create a search index for hash documents with field definitions and key prefix filtering
SchemaField[]hashSchema={TextField.of("name"),TextField.of("city"),NumericField.of("age")};StringhashCreateResult=jedis.ftCreate("hash-idx:users",FTCreateParams.createParams().on(IndexDataType.HASH).addPrefix("huser:"),hashSchema);System.out.println(hashCreateResult);// >>> OK

Use hset() to add the hash documents instead of jsonSet().

Foundational: Store hash documents in Redis with automatic indexing based on key prefix
Map<String,String>user1Info=newHashMap<>();user1Info.put("name","Paul John");user1Info.put("email","[email protected]");user1Info.put("age","42");user1Info.put("city","London");longhuser1Set=jedis.hset("huser:1",user1Info);System.out.println(huser1Set);// >>> 4Map<String,String>user2Info=newHashMap<>();user2Info.put("name","Eden Zamir");user2Info.put("email","[email protected]");user2Info.put("age","29");user2Info.put("city","Tel Aviv");longhuser2Set=jedis.hset("huser:2",user2Info);System.out.println(huser2Set);// >>> 4Map<String,String>user3Info=newHashMap<>();user3Info.put("name","Paul Zamir");user3Info.put("email","[email protected]");user3Info.put("age","35");user3Info.put("city","Tel Aviv");longhuser3Set=jedis.hset("huser:3",user3Info);System.out.println(huser3Set);// >>> 4

The query commands work the same here for hash as they do for JSON (but the name of the hash index is different). The results are returned in a List of Document objects, as with JSON:

Query with filters: Search hash documents using text matching and numeric range filters (same as JSON queries)
SearchResultfindPaulHashResult=jedis.ftSearch("hash-idx:users","Paul @age:[30 40]");System.out.println(findPaulHashResult.getTotalResults());// >>> 1List<Document>paulHashDocs=findPaulHashResult.getDocuments();for(Documentdoc:paulHashDocs){System.out.println(doc.getId());}// >>> user:3

More information

See the Redis Search docs for a full description of all query features with examples.

On this page