public void PureLucene() { using (var dir = new RAMDirectory()) { using (var keywordAnalyzer = new KeywordAnalyzer()) using (var writer = new IndexWriter(dir, keywordAnalyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)) { var doc = new Lucene.Net.Documents.Document(); var writeShape = NtsSpatialContext.GEO.ReadShape("LINESTRING (0 0, 1 1, 2 1)"); var spatialField = new SpatialField("WKT", new SpatialOptionsFactory().Geography.Default()); var writeStrategy = spatialField.GetStrategy(); foreach (var f in writeStrategy.CreateIndexableFields(writeShape)) { doc.Add(f); } writer.AddDocument(doc); writer.Commit(); } var shape = NtsSpatialContext.GEO.ReadShape("LINESTRING (1 0, 1 1, 1 2)"); SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape); var spatialField2 = new SpatialField("WKT", new SpatialOptionsFactory().Geography.Default()); var writeStrategy2 = spatialField2.GetStrategy(); var makeQuery = writeStrategy2.MakeQuery(args); using (var search = new IndexSearcher(dir)) { var topDocs = search.Search(makeQuery, 5); Assert.Equal(1, topDocs.TotalHits); } } }
internal static IEnumerable <AbstractField> CreateSpatialField(SpatialField spatialField, double?lat, double?lng) { if (lng == null || double.IsNaN(lng.Value)) { return(Enumerable.Empty <AbstractField>()); } if (lat == null || double.IsNaN(lat.Value)) { return(Enumerable.Empty <AbstractField>()); } Shape shape = spatialField.GetContext().MakePoint(lng.Value, lat.Value); return(spatialField.CreateIndexableFields(shape)); }
private static Shape HandleWkt(Query query, MethodExpression expression, QueryMetadata metadata, BlittableJsonReaderObject parameters, string fieldName, SpatialField spatialField) { var wktValue = GetValue(fieldName, query, metadata, parameters, (ValueExpression)expression.Arguments[0]); AssertValueIsString(fieldName, wktValue.Type); SpatialUnits?spatialUnits = null; if (expression.Arguments.Count == 2) { spatialUnits = GetSpatialUnits(query, expression.Arguments[3] as ValueExpression, metadata, parameters, fieldName); } return(spatialField.ReadShape((string)wktValue.Value, spatialUnits)); }
private static Shape HandleCircle(Query query, MethodExpression expression, QueryMetadata metadata, BlittableJsonReaderObject parameters, string fieldName, SpatialField spatialField) { var radius = GetValue(fieldName, query, metadata, parameters, (ValueExpression)expression.Arguments[0]); AssertValueIsNumber(fieldName, radius.Type); var latitute = GetValue(fieldName, query, metadata, parameters, (ValueExpression)expression.Arguments[1]); AssertValueIsNumber(fieldName, latitute.Type); var longitude = GetValue(fieldName, query, metadata, parameters, (ValueExpression)expression.Arguments[2]); AssertValueIsNumber(fieldName, longitude.Type); SpatialUnits?spatialUnits = null; if (expression.Arguments.Count == 4) { spatialUnits = GetSpatialUnits(query, expression.Arguments[3] as ValueExpression, metadata, parameters, fieldName); } return(spatialField.ReadCircle(Convert.ToDouble(radius.Value), Convert.ToDouble(latitute.Value), Convert.ToDouble(longitude.Value), spatialUnits)); }
internal static IEnumerable <AbstractField> CreateSpatialField(SpatialField spatialField, object shapeWKT) { return(spatialField.CreateIndexableFields(shapeWKT)); }
internal static IEnumerable <AbstractField> CreateSpatialField(SpatialField spatialField, object lat, object lng) { return(CreateSpatialField(spatialField, ConvertToDouble(lat), ConvertToDouble(lng))); }
public static Sort GetSort(this IndexQuery self, IndexDefinition indexDefinition, AbstractViewGenerator viewGenerator) { var spatialQuery = self as SpatialIndexQuery; var sortedFields = self.SortedFields; if (sortedFields == null || sortedFields.Length <= 0) { if (spatialQuery == null || string.IsNullOrEmpty(self.Query) == false) { return(null); } sortedFields = new[] { new SortedField(Constants.DistanceFieldName), }; } return(new Sort(sortedFields .Select(sortedField => { if (sortedField.Field == Constants.TemporaryScoreValue) { return SortField.FIELD_SCORE; } if (sortedField.Field.StartsWith(Constants.RandomFieldName)) { var parts = sortedField.Field.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) // truly random { return new RandomSortField(Guid.NewGuid().ToString()); } return new RandomSortField(parts[1]); } if (sortedField.Field.StartsWith(Constants.CustomSortFieldName)) { var parts = sortedField.Field.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length < 2) // truly random { throw new InvalidOperationException("Cannot figure out type for custom sort"); } return new CustomSortField(parts[1], self, parts[0][parts[0].Length - 1] == '-'); } if (sortedField.Field.StartsWith(Constants.DistanceFieldName)) { SpatialField spatialField = null; Shape shape; if (sortedField.Field.Length == Constants.DistanceFieldName.Length) { if (spatialQuery == null) { throw new InvalidOperationException("Illegal Spatial Sort Parameter: Blank Spatial sort cannot be used without a spatial query"); } spatialField = viewGenerator.GetSpatialField(spatialQuery.SpatialFieldName); shape = spatialField.ReadShape(spatialQuery.QueryShape); } else { var sortParams = sortedField.Field.Split(';'); double lat, lng; if (sortParams.Length < 3 || !double.TryParse(sortParams[1], out lat) || !double.TryParse(sortParams[2], out lng)) { throw new InvalidOperationException("Illegal Spatial Sort Parameter"); } string spatialFieldName; if (sortParams.Length >= 4) { spatialFieldName = sortParams[3]; } else { if (spatialQuery == null) { spatialFieldName = Constants.DefaultSpatialFieldName; } else { spatialFieldName = spatialQuery.SpatialFieldName; } } spatialField = viewGenerator.GetSpatialField(spatialFieldName); shape = new PointImpl(lng, lat, spatialField.GetContext()); } var dsort = new SpatialDistanceFieldComparatorSource(spatialField, shape.GetCenter()); return new SortField(sortedField.Field, dsort, sortedField.Descending); } var sortOptions = GetSortOption(indexDefinition, sortedField.Field, self); if (sortOptions == null || sortOptions == SortOptions.None) { return new SortField(sortedField.Field, CultureInfo.InvariantCulture, sortedField.Descending); } if (sortOptions.Value == SortOptions.Short) { sortOptions = SortOptions.Int; } return new SortField(sortedField.Field, (int)sortOptions.Value, sortedField.Descending); }) .ToArray())); }
public override IDocumentQuery <T> WithinRadiusOf(IPosition position, Distance distance) { return(_query.WithinRadiusOf(SpatialField.NameFor(_propertySelector), distance.ConvertTo(DistanceUnit.Km).Value, position.GetCoordinate().Latitude, position.GetCoordinate().Longitude)); }
public override IDocumentQuery <T> RelatesToShape(IRavenIndexable geometry, SpatialRelation relation) { return(_query.RelatesToShape(SpatialField.NameFor(_propertySelector), _writer.Write(geometry.GetSpatial4nShape()), relation)); }
public SpatialDistanceFieldComparator(SpatialField spatialField, Point origin, int numHits) { this.spatialField = spatialField; values = new DistanceValue[numHits]; originPt = origin; }
public SpatialDistanceFieldComparatorSource(SpatialField spatialField, Point center) { this.spatialField = spatialField; this.center = center; }
public override IRavenQueryable <T> WithinRadiusOf(IPosition position, Distance distance) { return(_source.Customize(x => x.WithinRadiusOf(SpatialField.NameFor(_property), distance.ConvertTo(DistanceUnit.Km).Value, position.GetCoordinate().Latitude, position.GetCoordinate().Longitude))); }
public override IRavenQueryable <T> RelatesToShape(IRavenIndexable geometry, SpatialRelation relation) { return(_source.Customize(x => x.RelatesToShape(SpatialField.NameFor(_property), _writer.Write(geometry.GetSpatial4nShape()), relation))); }