|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Learn how to use the Redis query engine with JSON and hash documents. |
| 13 | +linkTitle: Index and query documents |
| 14 | +title: Index and query documents |
| 15 | +weight: 2 |
| 16 | +--- |
| 17 | + |
| 18 | +This example shows how to create a |
| 19 | +[search index]({{< relref "/develop/ai/search-and-query/indexing" >}}) |
| 20 | +for [JSON]({{< relref "/develop/data-types/json" >}}) documents and |
| 21 | +run queries against the index. It then goes on to show the slight differences |
| 22 | +in the equivalent code for [hash]({{< relref "/develop/data-types/hashes" >}}) |
| 23 | +documents. |
| 24 | + |
| 25 | +## Initialize |
| 26 | + |
| 27 | +Make sure that you have [Redis Open Source]({{< relref "/operate/oss_and_stack/" >}}) |
| 28 | +or another Redis server available. Also install the |
| 29 | +[Lettuce]({{< relref "/develop/clients/lettuce" >}}) client library if you |
| 30 | +haven't already done so. |
| 31 | + |
| 32 | +Add the following dependencies. All of them are applicable to both JSON and hash, |
| 33 | +except for the `JsonParser`, `JsonPath`, and `JsonObject` classes. |
| 34 | + |
| 35 | +{{< clients-example lettuce_home_json import >}} |
| 36 | +{{< /clients-example >}} |
| 37 | + |
| 38 | +## Create data |
| 39 | + |
| 40 | +Create some test data to add to the database: |
| 41 | + |
| 42 | +{{< clients-example lettuce_home_json create_data >}} |
| 43 | +{{< /clients-example >}} |
| 44 | + |
| 45 | +## Add the index |
| 46 | + |
| 47 | +Connect to your Redis database. The code below shows the most |
| 48 | +basic connection but see |
| 49 | +[Connect to the server]({{< relref "/develop/clients/lettuce/connect" >}}) |
| 50 | +to learn more about the available connection options. |
| 51 | + |
| 52 | +{{< clients-example lettuce_home_json connect >}} |
| 53 | +{{< /clients-example >}} |
| 54 | + |
| 55 | +Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/ai/search-and-query/query/" >}}). |
| 56 | + |
| 57 | +{{< clients-example lettuce_home_json make_index >}} |
| 58 | +{{< /clients-example >}} |
| 59 | + |
| 60 | +## Add the data |
| 61 | + |
| 62 | +Add the three sets of user data to the database as |
| 63 | +[JSON]({{< relref "/develop/data-types/json" >}}) objects. |
| 64 | +If you use keys with the `user:` prefix then Redis will index the |
| 65 | +objects automatically as you add them: |
| 66 | + |
| 67 | +{{< clients-example lettuce_home_json add_data >}} |
| 68 | +{{< /clients-example >}} |
| 69 | + |
| 70 | +## Query the data |
| 71 | + |
| 72 | +You can now use the index to search the JSON objects. The |
| 73 | +[query]({{< relref "/develop/ai/search-and-query/query" >}}) |
| 74 | +below searches for objects that have the text "Paul" in any field |
| 75 | +and have an `age` value in the range 30 to 40: |
| 76 | + |
| 77 | +{{< clients-example lettuce_home_json query1 >}} |
| 78 | +{{< /clients-example >}} |
| 79 | + |
| 80 | +Specify query options to return only the `city` field: |
| 81 | + |
| 82 | +{{< clients-example lettuce_home_json query2 >}} |
| 83 | +{{< /clients-example >}} |
| 84 | + |
| 85 | +Use an |
| 86 | +[aggregation query]({{< relref "/develop/ai/search-and-query/query/aggregation" >}}) |
| 87 | +to count all users in each city. |
| 88 | + |
| 89 | +{{< clients-example lettuce_home_json query3 >}} |
| 90 | +{{< /clients-example >}} |
| 91 | + |
| 92 | +## Differences with hash documents |
| 93 | + |
| 94 | +Indexing for hash documents is very similar to JSON indexing but you |
| 95 | +need to specify some slightly different options. |
| 96 | + |
| 97 | +When you create the schema for a hash index, you don't need to |
| 98 | +add aliases for the fields, since you use the basic names to access |
| 99 | +the fields. Also, you must use `CreateArgs.TargetType.HASH` for the `On()` |
| 100 | +option of `CreateArgs` when you create the index. The code below shows these |
| 101 | +changes with a new index called `hash-idx:users`, which is otherwise the same as |
| 102 | +the `idx:users` index used for JSON documents in the previous examples. |
| 103 | + |
| 104 | +{{< clients-example lettuce_home_json make_hash_index >}} |
| 105 | +{{< /clients-example >}} |
| 106 | + |
| 107 | +Use [`hset()`]({{< relref "/commands/hset" >}}) to add the hash |
| 108 | +documents instead of [`jsonSet()`]({{< relref "/commands/json.set" >}}). |
| 109 | + |
| 110 | +{{< clients-example lettuce_home_json add_hash_data >}} |
| 111 | +{{< /clients-example >}} |
| 112 | + |
| 113 | +The query commands work the same here for hash as they do for JSON (but |
| 114 | +the name of the hash index is different). The results are returned in |
| 115 | +a `List` of `SearchReply.SearchResult<String, String>` objects, as with JSON: |
| 116 | + |
| 117 | +{{< clients-example lettuce_home_json query1_hash >}} |
| 118 | +{{< /clients-example >}} |
| 119 | + |
| 120 | +## More information |
| 121 | + |
| 122 | +See the [Redis query engine]({{< relref "/develop/ai/search-and-query" >}}) docs |
| 123 | +for a full description of all query features with examples. |
0 commit comments