-
Notifications
You must be signed in to change notification settings - Fork 46
Support GEOSHAPE field type in RediSearch #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
690b8d1
Initial support for OSS Cluster API (#170)
shacharPash 387f461
add GeoShapeField
shacharPash b299659
Merge branch 'master' into Issue188/GeoShape
shacharPash 02f5735
Update SkipIfRedisAttribute.cs
shacharPash e7bc730
delete line
shacharPash 1fbc1b5
AddGeoShapeField
shacharPash 222c2f2
add test
shacharPash 779d85c
upgrade SE.Redis
shacharPash 1e68553
Merge branch 'master' into Issue188/GeoShape
shacharPash e721dd9
fix test
shacharPash 244889b
add async test
shacharPash da05e24
add tests + Skips
shacharPash dcdafd9
skip id less than 7.2.1
shacharPash 432ee11
skip if cluster
shacharPash 174ca4d
add example
shacharPash bf52646
chayim review
shacharPash c22527e
chayims review 2
shacharPash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# GeoShape Fields Usage In RediSearch | ||
|
||
NRedisStack now supports GEOSHAPE field querying. | ||
|
||
Any object that serializes the [well-known text (WKT)](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) as a `string` can be used with NRedisStack. | ||
|
||
Using GeoShape fields in searches with the [NetTopologySuite](https://github.com/NetTopologySuite/NetTopologySuite) library. | ||
|
||
## Example | ||
|
||
### Modules Needed | ||
|
||
```c# | ||
using StackExchange.Redis; | ||
using NRedisStack.RedisStackCommands; | ||
using NRedisStack.Search; | ||
using NetTopologySuite.Geometries; | ||
using NetTopologySuite.IO; | ||
``` | ||
|
||
### Setup | ||
|
||
```csharp | ||
// Connect to the Redis server: | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(); | ||
// Get a reference to the database and for search commands: | ||
var ft = db.FT(); | ||
|
||
// Create WTKReader and GeometryFactory objects: | ||
WKTReader reader = new WKTReader(); | ||
GeometryFactory factory = new GeometryFactory(); | ||
|
||
``` | ||
|
||
### Create the index | ||
|
||
```csharp | ||
ft.Create(index, new Schema().AddGeoShapeField("geom", GeoShapeField.CoordinateSystem.FLAT)); | ||
``` | ||
|
||
### Prepare the data | ||
|
||
```csharp | ||
Polygon small = factory.CreatePolygon(new Coordinate[]{new Coordinate(1, 1), | ||
new Coordinate(1, 100), new Coordinate(100, 100), new Coordinate(100, 1), new Coordinate(1, 1)}); | ||
db.HashSet("small", "geom", small.ToString()); | ||
|
||
Polygon large = factory.CreatePolygon(new Coordinate[]{new Coordinate(1, 1), | ||
new Coordinate(1, 200), new Coordinate(200, 200), new Coordinate(200, 1), new Coordinate(1, 1)}); | ||
db.HashSet("large", "geom", large.ToString()); | ||
``` | ||
|
||
## Polygon type | ||
|
||
### Querying within condition | ||
|
||
```csharp | ||
Polygon within = factory.CreatePolygon(new Coordinate[]{new Coordinate(0, 0), | ||
new Coordinate(0, 150), new Coordinate(150, 150), new Coordinate(150, 0), new Coordinate(0, 0)}); | ||
|
||
SearchResult res = ft.Search(index, new Query("@geom:[within $poly]") | ||
.AddParam("poly", within.ToString()) // Note serializing the argument to string | ||
.Dialect(3)); // DIALECT 3 is required for this query | ||
``` | ||
|
||
The search result from redis is: | ||
|
||
```bash | ||
1) (integer) 1 | ||
2) "small" | ||
3) 1) "geom" | ||
2) "POLYGON ((1 1, 1 100, 100 100, 100 1, 1 1))" | ||
``` | ||
|
||
Use the reader to get the polygon: | ||
|
||
```csharp | ||
reader.Read(res.Documents[0]["geom"].ToString()); | ||
``` | ||
|
||
### Querying contains condition | ||
|
||
```csharp | ||
Polygon contains = factory.CreatePolygon(new Coordinate[]{new Coordinate(2, 2), | ||
new Coordinate(2, 50), new Coordinate(50, 50), new Coordinate(50, 2), new Coordinate(2, 2)}); | ||
|
||
res = ft.Search(index, new Query("@geom:[contains $poly]") | ||
.AddParam("poly", contains.ToString()) // Note serializing the argument to string | ||
.Dialect(3)); // DIALECT 3 is required for this query | ||
|
||
``` | ||
|
||
Our search result: | ||
|
||
```bash | ||
1) (integer) 2 | ||
2) "small" | ||
3) 1) "geom" | ||
2) "POLYGON ((1 1, 1 100, 100 100, 100 1, 1 1))" | ||
4) "large" | ||
5) 1) "geom" | ||
2) "POLYGON ((1 1, 1 200, 200 200, 200 1, 1 1))" | ||
``` | ||
|
||
### Searching with Coordinates | ||
|
||
```csharp | ||
Point point = factory.CreatePoint(new Coordinate(10, 10)); | ||
db.HashSet("point", "geom", point.ToString()); | ||
|
||
res = ft.Search(index, new Query("@geom:[within $poly]") | ||
.AddParam("poly", within.ToString()) // Note serializing the argument to string | ||
.Dialect(3)); // DIALECT 3 is required for this query | ||
|
||
``` | ||
|
||
Our search result: | ||
|
||
```bash | ||
1) (integer) 2 | ||
2) "small" | ||
3) 1) "geom" | ||
2) "POLYGON ((1 1, 1 100, 100 100, 100 1, 1 1))" | ||
4) "point" | ||
5) 1) "geom" | ||
2) "POINT (10 10)" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using NRedisStack.Search.Literals; | ||
using static NRedisStack.Search.Schema.GeoShapeField; | ||
using static NRedisStack.Search.Schema.VectorField; | ||
|
||
namespace NRedisStack.Search | ||
|
@@ -13,6 +14,7 @@ public enum FieldType | |
{ | ||
Text, | ||
Geo, | ||
GeoShape, | ||
Numeric, | ||
Tag, | ||
Vector | ||
|
@@ -38,6 +40,7 @@ internal void AddSchemaArgs(List<object> args) | |
{ | ||
FieldType.Text => "TEXT", | ||
FieldType.Geo => "GEO", | ||
FieldType.GeoShape => "GEOSHAPE", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good |
||
FieldType.Numeric => "NUMERIC", | ||
FieldType.Tag => "TAG", | ||
FieldType.Vector => "VECTOR", | ||
|
@@ -178,6 +181,37 @@ internal override void AddFieldTypeArgs(List<object> args) | |
|
||
} | ||
|
||
public class GeoShapeField : Field | ||
{ | ||
public enum CoordinateSystem | ||
{ | ||
/// <summary> | ||
/// For cartesian (X,Y). | ||
/// </summary> | ||
FLAT, | ||
|
||
/// <summary> | ||
/// For geographic (lon, lat). | ||
/// </summary> | ||
SPHERICAL | ||
} | ||
private CoordinateSystem system { get; } | ||
|
||
internal GeoShapeField(FieldName name, CoordinateSystem system) | ||
: base(name, FieldType.GeoShape) | ||
{ | ||
this.system = system; | ||
} | ||
|
||
internal GeoShapeField(string name, CoordinateSystem system) | ||
: this(FieldName.Of(name), system) { } | ||
|
||
internal override void AddFieldTypeArgs(List<object> args) | ||
{ | ||
args.Add(system.ToString()); | ||
} | ||
} | ||
|
||
public class NumericField : Field | ||
{ | ||
public bool Sortable { get; } | ||
|
@@ -288,6 +322,30 @@ public Schema AddTextField(FieldName name, double weight = 1.0, bool sortable = | |
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Add a GeoShape field to the schema. | ||
/// </summary> | ||
/// <param name="name">The field's name.</param> | ||
/// <param name="system">The coordinate system to use.</param> | ||
/// <returns>The <see cref="Schema"/> object.</returns> | ||
public Schema AddGeoShapeField(string name, CoordinateSystem system) | ||
{ | ||
Fields.Add(new GeoShapeField(name, system)); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Add a GeoShape field to the schema. | ||
/// </summary> | ||
/// <param name="name">The field's name.</param> | ||
/// <param name="system">The coordinate system to use.</param> | ||
/// <returns>The <see cref="Schema"/> object.</returns> | ||
public Schema AddGeoShapeField(FieldName name, CoordinateSystem system) | ||
{ | ||
Fields.Add(new GeoShapeField(name, system)); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Add a Geo field to the schema. | ||
/// </summary> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.