Skip to content

Commit 946f4d1

Browse files
DOC-4733 added geo indexing examples
1 parent c54f639 commit 946f4d1

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

tests/Doc/GeoIndexExample.cs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// EXAMPLE: geoindex
2+
3+
// STEP_START import
4+
using NRedisStack.RedisStackCommands;
5+
using NRedisStack.Search;
6+
using NRedisStack.Search.Literals.Enums;
7+
using StackExchange.Redis;
8+
// STEP_END
9+
10+
// REMOVE_START
11+
using NRedisStack.Tests;
12+
using NetTopologySuite.Geometries;
13+
14+
namespace Doc;
15+
[Collection("DocsTests")]
16+
// REMOVE_END
17+
18+
// HIDE_START
19+
public class GeoIndexExample
20+
{
21+
22+
[SkipIfRedis(Is.OSSCluster)]
23+
public void run()
24+
{
25+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
26+
var db = muxer.GetDatabase();
27+
//REMOVE_START
28+
// Clear any keys here before using them in tests.
29+
db.KeyDelete(new RedisKey[] { "product:46885", "product:46886", "shape:1", "shape:2", "shape:3", "shape:4" });
30+
try { db.FT().DropIndex("productidx"); } catch { }
31+
try { db.FT().DropIndex("geomidx"); } catch { }
32+
//REMOVE_END
33+
// HIDE_END
34+
35+
// STEP_START create_geo_idx
36+
Schema geoSchema = new Schema()
37+
.AddGeoField(new FieldName("$.location", "location"));
38+
39+
bool geoCreateResult = db.FT().Create(
40+
"productidx",
41+
new FTCreateParams()
42+
.On(IndexDataType.JSON)
43+
.Prefix("product:"),
44+
geoSchema
45+
);
46+
Console.WriteLine(geoCreateResult); // >>> True
47+
// STEP_END
48+
// REMOVE_START
49+
Assert.True(geoCreateResult);
50+
// REMOVE_END
51+
52+
// STEP_START add_geo_json
53+
var product46885 = new
54+
{
55+
description = "Navy Blue Slippers",
56+
price = 45.99,
57+
city = "Denver",
58+
location = "-104.991531, 39.742043"
59+
};
60+
61+
bool gjAddResult1 = db.JSON().Set("product:46885", "$", product46885);
62+
Console.WriteLine(gjAddResult1); // >>> True
63+
64+
var product46886 = new
65+
{
66+
description = "Bright Green Socks",
67+
price = 25.50,
68+
city = "Fort Collins",
69+
location = "-105.0618814,40.5150098"
70+
};
71+
72+
bool gjAddResult2 = db.JSON().Set("product:46886", "$", product46886);
73+
Console.WriteLine(gjAddResult2); // >>> True
74+
// STEP_END
75+
// REMOVE_START
76+
Assert.True(gjAddResult1);
77+
Assert.True(gjAddResult2);
78+
// REMOVE_END
79+
80+
// STEP_START geo_query
81+
SearchResult geoQueryResult = db.FT().Search(
82+
"productidx",
83+
new Query("@location:[-104.800644 38.846127 100 mi]")
84+
);
85+
Console.WriteLine(geoQueryResult.Documents.Count); // >>> 1
86+
87+
Console.WriteLine(
88+
string.Join(", ", geoQueryResult.Documents.Select(x => x["json"]))
89+
);
90+
// >>> {"description":"Navy Blue Slippers","price":45.99,"city":"Denver"...
91+
// STEP_END
92+
// REMOVE_START
93+
Assert.Single(geoQueryResult.Documents);
94+
Assert.Equal(
95+
"{\"description\":\"Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"location\":\"-104.991531, 39.742043\"}",
96+
string.Join(", ", geoQueryResult.Documents.Select(x => x["json"]))
97+
);
98+
// REMOVE_END
99+
100+
// STEP_START create_gshape_idx
101+
Schema geomSchema = new Schema()
102+
.AddGeoShapeField(
103+
new FieldName("$.geom", "geom"),
104+
Schema.GeoShapeField.CoordinateSystem.FLAT
105+
)
106+
.AddTextField(new FieldName("$.name", "name"));
107+
108+
bool geomCreateResult = db.FT().Create(
109+
"geomidx",
110+
new FTCreateParams()
111+
.On(IndexDataType.JSON)
112+
.Prefix("shape:"),
113+
geomSchema
114+
);
115+
Console.WriteLine(geomCreateResult); // >>> True
116+
// STEP_END
117+
// REMOVE_START
118+
Assert.True(geomCreateResult);
119+
// REMOVE_END
120+
121+
// STEP_START add_gshape_json
122+
var shape1 = new
123+
{
124+
name = "Green Square",
125+
geom = "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))"
126+
};
127+
128+
bool gmJsonRes1 = db.JSON().Set("shape:1", "$", shape1);
129+
Console.WriteLine(gmJsonRes1); // >>> True
130+
131+
var shape2 = new
132+
{
133+
name = "Red Rectangle",
134+
geom = "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))"
135+
};
136+
137+
bool gmJsonRes2 = db.JSON().Set("shape:2", "$", shape2);
138+
Console.WriteLine(gmJsonRes1); // >>> True
139+
140+
var shape3 = new
141+
{
142+
name = "Blue Triangle",
143+
geom = "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))"
144+
};
145+
146+
bool gmJsonRes3 = db.JSON().Set("shape:3", "$", shape3);
147+
Console.WriteLine(gmJsonRes3); // >>> True
148+
149+
var shape4 = new
150+
{
151+
name = "Purple Point",
152+
geom = "POINT (2 2)"
153+
};
154+
155+
bool gmJsonRes4 = db.JSON().Set("shape:4", "$", shape4);
156+
Console.WriteLine(gmJsonRes3); // >>> True
157+
// STEP_END
158+
// REMOVE_START
159+
Assert.True(gmJsonRes1);
160+
Assert.True(gmJsonRes2);
161+
Assert.True(gmJsonRes3);
162+
Assert.True(gmJsonRes4);
163+
// REMOVE_END
164+
165+
// STEP_START gshape_query
166+
SearchResult geomQueryResult = db.FT().Search(
167+
"geomidx",
168+
new Query("(-@name:(Green Square) @geom:[WITHIN $qshape])")
169+
.AddParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
170+
.Limit(0, 1)
171+
.Dialect(4)
172+
);
173+
174+
Console.WriteLine(geomQueryResult.Documents.Count); // >>> 1
175+
var res = string.Join(", ", geomQueryResult.Documents.Select(x => x["json"]));
176+
177+
Console.WriteLine(
178+
string.Join(", ", geomQueryResult.Documents.Select(x => x["json"]))
179+
);
180+
// >>> [{"name":"Purple Point","geom":"POINT (2 2)"}]
181+
// STEP_END
182+
// REMOVE_START
183+
Assert.Single(geomQueryResult.Documents);
184+
Assert.Equal(
185+
"[{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}]",
186+
string.Join(", ", geomQueryResult.Documents.Select(x => x["json"]))
187+
);
188+
// REMOVE_END
189+
// HIDE_START
190+
}
191+
}
192+
// HIDE_END

0 commit comments

Comments
 (0)