Skip to content

Commit 5be7ae5

Browse files
Merge pull request #1487 from redis/DOC-5109-csharp-hash-search-example
DOC-5109 C# hash search example
2 parents fc4eb4a + 28c3c3e commit 5be7ae5

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

content/develop/clients/dotnet/queryjson.md

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,55 @@ categories:
99
- oss
1010
- kubernetes
1111
- clients
12-
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: Index and query JSON
14-
title: Example - Index and query JSON documents
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
1515
weight: 2
1616
---
1717

1818
This example shows how to create a
1919
[search index]({{< relref "/develop/interact/search-and-query/indexing" >}})
20-
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
21-
run queries against the index.
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.
2224

23-
Make sure that you have Redis Open Source and `NRedisStack` installed.
25+
## Initialize
2426

25-
Start by importing dependencies:
27+
Make sure that you have [Redis Open Source]({{< relref "/operate/oss_and_stack" >}})
28+
or another Redis server available. Also install the
29+
[`NRedisStack`]({{< relref "/develop/clients/dotnet" >}}) client library if you
30+
haven't already done so.
31+
32+
Add the following dependencies:
2633

2734
{{< clients-example cs_home_json import >}}
2835
{{< /clients-example >}}
2936

30-
Connect to the database:
31-
32-
{{< clients-example cs_home_json connect >}}
33-
{{< /clients-example >}}
37+
## Create data
3438

3539
Create some test data to add to the database:
3640

3741
{{< clients-example cs_home_json create_data >}}
3842
{{< /clients-example >}}
3943

44+
## Add the index
45+
46+
Connect to your Redis database. The code below shows the most
47+
basic connection but see
48+
[Connect to the server]({{< relref "/develop/clients/dotnet/connect" >}})
49+
to learn more about the available connection options.
50+
51+
{{< clients-example cs_home_json connect >}}
52+
{{< /clients-example >}}
53+
4054
Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
4155

4256
{{< clients-example cs_home_json make_index >}}
4357
{{< /clients-example >}}
4458

59+
## Add the data
60+
4561
Add the three sets of user data to the database as
4662
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
4763
If you use keys with the `user:` prefix then Redis will index the
@@ -50,6 +66,8 @@ objects automatically as you add them:
5066
{{< clients-example cs_home_json add_data >}}
5167
{{< /clients-example >}}
5268

69+
## Query the data
70+
5371
You can now use the index to search the JSON objects. The
5472
[query]({{< relref "/develop/interact/search-and-query/query" >}})
5573
below searches for objects that have the text "Paul" in any field
@@ -70,5 +88,39 @@ to count all users in each city.
7088
{{< clients-example cs_home_json query3 >}}
7189
{{< /clients-example >}}
7290

91+
## Differences with hash documents
92+
93+
Indexing for hash documents is very similar to JSON indexing but you
94+
need to specify some slightly different options.
95+
96+
When you create the schema for a hash index, you don't need to
97+
add aliases for the fields, since you use the basic names to access
98+
the fields anyway. Also, you must set the `On` option to `IndexDataType.HASH`
99+
in the `FTCreateParams` object when you create the index. The code below shows
100+
these changes with a new index called `hash-idx:users`, which is otherwise the
101+
same as the `idx:users` index used for JSON documents in the previous examples.
102+
103+
{{< clients-example cs_home_json make_hash_index >}}
104+
{{< /clients-example >}}
105+
106+
You use [`HashSet()`]({{< relref "/commands/hset" >}}) to add the hash
107+
documents instead of [`JSON.Set()`]({{< relref "/commands/json.set" >}}).
108+
Also, you must add the fields as key-value pairs instead of combining them
109+
into a single object.
110+
111+
{{< clients-example cs_home_json add_hash_data >}}
112+
{{< /clients-example >}}
113+
114+
The query commands work the same here for hash as they do for JSON (but
115+
the name of the hash index is different). The format of the result is
116+
almost the same except that the fields are returned directly in the
117+
`Document` object of the result (for JSON, the fields are all enclosed
118+
in a string under the key `json`):
119+
120+
{{< clients-example cs_home_json query1_hash >}}
121+
{{< /clients-example >}}
122+
123+
## More information
124+
73125
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
74126
for a full description of all query features with examples.

0 commit comments

Comments
 (0)