Skip to content

Commit 2130398

Browse files
DOC-4733 added geo indexing examples (#376)
* DOC-4733 added geo indexing examples * DOC-4733 updated testing code * DOC-4733 try version check, following feedback * DOC-4733 removed obsolete request for dialect 4 * DOC-4733 skip another example < v7 --------- Co-authored-by: atakavci <58048133+atakavci@users.noreply.github.com> Co-authored-by: atakavci <a_takavci@yahoo.com>
1 parent 049ecb5 commit 2130398

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

tests/Doc/GeoIndexExample.cs

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

0 commit comments

Comments
 (0)