Beispiel #1
0
        public void testNGramPrefixGridLosAngeles()
        {
            SpatialContext ctx = SpatialContext.GEO;
            TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo");

            Shape point = ctx.MakePoint(-118.243680, 34.052230);

            Document losAngeles = new Document();

            losAngeles.Add(new Field("name", "Los Angeles", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
            foreach (var indexableField in prefixGridStrategy.CreateIndexableFields(point))
            {
                losAngeles.Add(indexableField);
            }
            losAngeles.Add(new Field(prefixGridStrategy.GetFieldName(), ctx.ToString(point), Field.Store.YES, Field.Index.NO));

            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(
            //        "Intersects(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);
        }
        public virtual void TestNGramPrefixGridLosAngeles()
        {
            SpatialContext ctx = SpatialContext.GEO;
            TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo");

            Spatial4n.Core.Shapes.IShape point = ctx.MakePoint(-118.243680, 34.052230);

            Document losAngeles = new Document();

            losAngeles.Add(new StringField("name", "Los Angeles", Field.Store.YES));
            foreach (IndexableField field in prefixGridStrategy.CreateIndexableFields(point))
            {
                losAngeles.Add(field);
            }
            losAngeles.Add(new StoredField(prefixGridStrategy.FieldName, point.toString()));//just for diagnostics

            addDocumentsAndCommit(Arrays.AsList(losAngeles));

            // This won't work with simple spatial context...
            SpatialArgsParser spatialArgsParser = new SpatialArgsParser();
            // TODO... use a non polygon query
            //    SpatialArgs spatialArgs = spatialArgsParser.parse(
            //        "Intersects(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);
        }
		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);
		}
Beispiel #4
0
 /// <summary>
 /// Get Test Queries
 /// </summary>
 /// <param name="parser"></param>
 /// <param name="ctx"></param>
 /// <param name="name"></param>
 /// <param name="in"></param>
 /// <returns></returns>
 public static IEnumerator <SpatialTestQuery> getTestQueries(
     SpatialArgsParser parser,
     SpatialContext ctx,
     String name,
     Stream @in)
 {
     return(new SpatialTestQueryLineReader(new StreamReader(@in, Encoding.UTF8), parser, ctx));
 }
Beispiel #5
0
        /**
         * Get Test Queries.  The InputStream is closed.
         */
        public static IEnumerator <SpatialTestQuery> GetTestQueries(
            SpatialArgsParser parser,
            SpatialContext ctx,
            string name,
            Stream @in)
        {
            IList <SpatialTestQuery> results = new JCG.List <SpatialTestQuery>();

            TextReader bufInput = new StreamReader(@in, Encoding.UTF8);

            try
            {
                String line;
                for (int lineNumber = 1; (line = bufInput.ReadLine()) != null; lineNumber++)
                {
                    SpatialTestQuery test = new SpatialTestQuery();
                    test.line       = line;
                    test.lineNumber = lineNumber;

                    try
                    {
                        // skip a comment
                        if (line.StartsWith("[", StringComparison.Ordinal))
                        {
                            int idx2 = line.IndexOf(']');
                            if (idx2 > 0)
                            {
                                line = line.Substring(idx2 + 1);
                            }
                        }

                        int             idx = line.IndexOf('@');
                        StringTokenizer st  = new StringTokenizer(line.Substring(0, idx - 0));
                        while (st.MoveNext())
                        {
                            test.ids.Add(st.Current.Trim());
                        }
                        test.args = parser.Parse(line.Substring(idx + 1).Trim(), ctx);
                        results.Add(test);
                    }
                    catch (Exception ex)
                    {
                        throw RuntimeException.Create("invalid query line: " + test.line, ex);
                    }
                }
            }
            finally
            {
                bufInput.Dispose();
            }
            return(results.GetEnumerator());
        }
Beispiel #6
0
        private void Search()
        {
            IndexReader   indexReader   = DirectoryReader.Open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            Sort          idSort        = new Sort(new SortField("id", SortFieldType.INT32));

            //--Filter by circle (<= distance from a point)
            {
                //Search with circle
                //note: SpatialArgs can be parsed from a string
                SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects,
                                                   ctx.MakeCircle(-80.0, 33.0, DistanceUtils.Dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
                Filter  filter = strategy.MakeFilter(args);
                TopDocs docs   = indexSearcher.Search(new MatchAllDocsQuery(), filter, 10, idSort);
                AssertDocMatchedIds(indexSearcher, docs, 2);
                //Now, lets get the distance for the 1st doc via computing from stored point value:
                // (this computation is usually not redundant)
                Document doc1    = indexSearcher.Doc(docs.ScoreDocs[0].Doc);
                String   doc1Str = doc1.GetField(strategy.FieldName).GetStringValue();
                //assume doc1Str is "x y" as written in newSampleDocument()
                int    spaceIdx    = doc1Str.IndexOf(' ');
                double x           = double.Parse(doc1Str.Substring(0, spaceIdx - 0), CultureInfo.InvariantCulture);
                double y           = double.Parse(doc1Str.Substring(spaceIdx + 1), CultureInfo.InvariantCulture);
                double doc1DistDEG = ctx.CalcDistance(args.Shape.Center, x, y);
                assertEquals(121.6d, DistanceUtils.Degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1);
                //or more simply:
                assertEquals(121.6d, doc1DistDEG * DistanceUtils.DEG_TO_KM, 0.1);
            }
            //--Match all, order by distance ascending
            {
                IPoint      pt          = ctx.MakePoint(60, -50);
                ValueSource valueSource = strategy.MakeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);    //the distance (in km)
                Sort        distSort    = new Sort(valueSource.GetSortField(false)).Rewrite(indexSearcher); //false=asc dist
                TopDocs     docs        = indexSearcher.Search(new MatchAllDocsQuery(), 10, distSort);
                AssertDocMatchedIds(indexSearcher, docs, 4, 20, 2);
                //To get the distance, we could compute from stored values like earlier.
                // However in this example we sorted on it, and the distance will get
                // computed redundantly.  If the distance is only needed for the top-X
                // search results then that's not a big deal. Alternatively, try wrapping
                // the ValueSource with CachingDoubleValueSource then retrieve the value
                // from the ValueSource now. See LUCENE-4541 for an example.
            }
            //demo arg parsing
            {
                SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects,
                                                   ctx.MakeCircle(-80.0, 33.0, 1));
                SpatialArgs args2 = new SpatialArgsParser().Parse("Intersects(BUFFER(POINT(-80 33),1))", ctx);
                assertEquals(args.toString(), args2.toString());
            }

            indexReader.Dispose();
        }
Beispiel #7
0
        public void TestArgParser()
        {
            SpatialArgsParser parser = new SpatialArgsParser();

            String      arg      = SpatialOperation.IsWithin + "(-10 -20 10 20)";
            SpatialArgs outValue = parser.Parse(arg, ctx);

            Assert.AreEqual(SpatialOperation.IsWithin, outValue.Operation);
            Rectangle bounds = (Rectangle)outValue.Shape;

            Assert.AreEqual(-10.0, bounds.GetMinX(), 0D);
            Assert.AreEqual(10.0, bounds.GetMaxX(), 0D);

            // Disjoint should not be scored
            arg      = SpatialOperation.IsDisjointTo + " (-10 10 -20 20)";
            outValue = parser.Parse(arg, ctx);
            Assert.AreEqual(SpatialOperation.IsDisjointTo, outValue.Operation);

            try
            {
                parser.Parse(SpatialOperation.IsDisjointTo + "[ ]", ctx);
                Assert.True(false, "spatial operations need args");
            }
            catch (Exception)
            {
                //expected
            }

            try
            {
                parser.Parse("XXXX(-10 10 -20 20)", ctx);
                Assert.True(false, "unknown operation!");
            }
            catch (Exception)
            {
                //expected
            }
        }
Beispiel #8
0
 public SpatialTestQueryLineReader(StreamReader r, SpatialArgsParser parser, SpatialContext ctx)
     : base(r)
 {
     this.parser = parser;
     this.ctx    = ctx;
 }