A basic 2D implementation of a Point.
Inheritance: System.Point
		public void testNGramPrefixGridLosAngeles()
		{
			SimpleSpatialFieldInfo fieldInfo = new SimpleSpatialFieldInfo("geo");
			SpatialContext ctx = SpatialContext.GEO_KM;
			TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx));

			Shape point = new PointImpl(-118.243680, 34.052230);

			Document losAngeles = new Document();
			losAngeles.Add(new Field("name", "Los Angeles", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
			losAngeles.Add(prefixGridStrategy.CreateField(fieldInfo, point, true, true));

			addDocumentsAndCommit(new List<Document> { losAngeles });

			// This won't work with simple spatial context...
			SpatialArgsParser spatialArgsParser = new SpatialArgsParser();
			// TODO... use a non polygon query
			//    SpatialArgs spatialArgs = spatialArgsParser.parse(
			//        "IsWithin(POLYGON((-127.00390625 39.8125,-112.765625 39.98828125,-111.53515625 31.375,-125.94921875 30.14453125,-127.00390625 39.8125)))",
			//        new SimpleSpatialContext());

			//    Query query = prefixGridStrategy.makeQuery(spatialArgs, fieldInfo);
			//    SearchResults searchResults = executeQuery(query, 1);
			//    assertEquals(1, searchResults.numFound);
		}
Exemplo n.º 2
0
		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());
		}