コード例 #1
0
        protected virtual Query MakeQueryFromShape(IShape shape)
        {
            SpatialArgs args = new SpatialArgs(m_operation, shape);

            if (!double.IsNaN(m_distErrPct))
            {
                args.DistErrPct = m_distErrPct;
            }

            if (m_score)
            {
                ValueSource valueSource = m_strategy.MakeDistanceValueSource(shape.Center);
                return(new CustomScoreQuery(m_strategy.MakeQuery(args), new FunctionQuery(valueSource)));
            }
            else
            {
                //strategy.makeQuery() could potentially score (isn't well defined) so instead we call
                // makeFilter() and wrap

                Filter filter = m_strategy.MakeFilter(args);
                if (filter is QueryWrapperFilter queryWrapperFilter)
                {
                    return(queryWrapperFilter.Query);
                }
                else
                {
                    return(new ConstantScoreQuery(filter));
                }
            }
        }
コード例 #2
0
        public IList <SearchItem> SearchByLocation(string queryString, double longitude, double latitude, double searchRadiusKm, int maxHits = 10)
        {
            IList <SearchItem> results;

            using (var searcher = new IndexSearcher(Directory, true))
                using (var analyser = new StandardAnalyzer(LuceneVersion))
                {
                    var distance   = DistanceUtils.Dist2Degrees(searchRadiusKm, DistanceUtils.EARTH_MEAN_RADIUS_KM);
                    var searchArea = _spatialContext.MakeCircle(longitude, latitude, distance);

                    var fields = new[] { Name };
                    var parser = new MultiFieldQueryParser(LuceneVersion, fields, analyser);
                    parser.DefaultOperator = QueryParser.Operator.OR; // Allow multiple terms.
                    var query = ParseQuery(queryString, parser);

                    var spatialArgs       = new SpatialArgs(SpatialOperation.Intersects, searchArea);
                    var spatialQuery      = _strategy.MakeQuery(spatialArgs);
                    var valueSource       = _strategy.MakeRecipDistanceValueSource(searchArea);
                    var valueSourceFilter = new ValueSourceFilter(new QueryWrapperFilter(spatialQuery), valueSource, 0, 1);

                    var filteredSpatial     = new FilteredQuery(query, valueSourceFilter);
                    var spatialRankingQuery = new FunctionQuery(valueSource);

                    BooleanQuery bq = new BooleanQuery();
                    bq.Add(filteredSpatial, Occur.MUST);
                    bq.Add(spatialRankingQuery, Occur.MUST);

                    var hits = searcher.Search(bq, maxHits).ScoreDocs;

                    results = MapResultsToSearchItems(hits, searcher);
                }

            return(results);
        }
コード例 #3
0
ファイル: SpatialField.cs プロジェクト: ybdev/ravendb
        public Query MakeQuery(Query existingQuery, SpatialStrategy spatialStrategy, string shapeWKT, SpatialRelation relation,
                               double distanceErrorPct = 0.025, SpatialUnits?unitOverride = null)
        {
            SpatialOperation spatialOperation;
            var shape = ReadShape(shapeWKT, unitOverride);

            switch (relation)
            {
            case SpatialRelation.Within:
                spatialOperation = SpatialOperation.IsWithin;
                break;

            case SpatialRelation.Contains:
                spatialOperation = SpatialOperation.Contains;
                break;

            case SpatialRelation.Disjoint:
                spatialOperation = SpatialOperation.IsDisjointTo;
                break;

            case SpatialRelation.Intersects:
                spatialOperation = SpatialOperation.Intersects;
                break;

            case SpatialRelation.Nearby:
                // only sort by this, do not filter
                return(new FunctionQuery(spatialStrategy.MakeDistanceValueSource(shape.GetCenter())));

            default:
                throw new ArgumentOutOfRangeException("relation");
            }
            var args = new SpatialArgs(spatialOperation, shape)
            {
                DistErrPct = distanceErrorPct
            };

            if (existingQuery is MatchAllDocsQuery)
            {
                return(new CustomScoreQuery(spatialStrategy.MakeQuery(args), new ValueSourceQuery(spatialStrategy.MakeRecipDistanceValueSource(shape))));
            }
            return(spatialStrategy.MakeQuery(args));
        }
コード例 #4
0
        private void SearchNearDoc(int docId, int distanceInKm)
        {
            SpatialArgs args = GetArgs(docId, distanceInKm);

            var booleanQuery = new BooleanQuery
            {
                { _spatialStrategy.MakeQuery(args), Occur.MUST }
            };

            TopDocs topDocs = indexSearcher.Search(booleanQuery, 10, null);

            Assert.GreaterOrEqual(topDocs.ScoreDocs.Length, 1); //Search area is centered on a doc so at least one doc should be returned
        }
コード例 #5
0
        public static Query MakeQuery(SpatialStrategy spatialStrategy, string shapeWKT, SpatialRelation relation,
                                      double distanceErrorPct = 0.025)
        {
            SpatialOperation spatialOperation;
            Shape            shape = ShapeReadWriter.ReadShape(shapeWKT);

            switch (relation)
            {
            case SpatialRelation.Within:
                spatialOperation = SpatialOperation.IsWithin;
                break;

            case SpatialRelation.Contains:
                spatialOperation = SpatialOperation.Contains;
                break;

            case SpatialRelation.Disjoint:
                spatialOperation = SpatialOperation.IsDisjointTo;
                break;

            case SpatialRelation.Intersects:
                spatialOperation = SpatialOperation.Intersects;
                break;

            case SpatialRelation.Nearby:
                var nearbyArgs = new SpatialArgs(SpatialOperation.IsWithin, shape);
                nearbyArgs.SetDistPrecision(distanceErrorPct);
                // only sort by this, do not filter
                return(new FunctionQuery(spatialStrategy.MakeValueSource(nearbyArgs)));

            default:
                throw new ArgumentOutOfRangeException("relation");
            }
            var args = new SpatialArgs(spatialOperation, shape);

            args.SetDistPrecision(distanceErrorPct);

            return(spatialStrategy.MakeQuery(args));
        }
コード例 #6
0
        static void Main(string[] args)
        {
            int maxLength = GeohashPrefixTree.GetMaxLevelsPossible();

            strategy = new RecursivePrefixTreeStrategy(
                new GeohashPrefixTree(context, maxLength));

            var dir    = new RAMDirectory();
            var writer = new IndexWriter(dir, new SimpleAnalyzer(), true,
                                         IndexWriter.MaxFieldLength.UNLIMITED);

            AddPoint(writer, "London", -81.233040, 42.983390);
            AddPoint(writer, "East New York", -73.882360, 40.666770);
            AddPoint(writer, "Manhattan", -73.966250, 40.783430);
            AddPoint(writer, "New York City", -74.005970, 40.714270);
            AddPoint(writer, "Oslo", 10.746090, 59.912730);
            AddPoint(writer, "Bergen", 5.324150, 60.392990);
            AddPoint(writer, "Washington, D. C.", -77.036370, 38.895110);

            writer.Close();

            // Origin point - Oslo Spektrum
            const double lat    = 59.9138688;
            const double lng    = 10.752245399999993;
            const double radius = 600;
            var          query  = strategy.MakeQuery(new SpatialArgs(SpatialOperation.IsWithin,
                                                                     context.MakeCircle(lng, lat, radius)), fieldInfo);

            var searcher = new IndexSearcher(dir);
            var results  = searcher.Search(query, null, 100);

            foreach (var topDoc in results.ScoreDocs)
            {
                var name = searcher.Doc(topDoc.doc).Get("Name");
                Console.WriteLine(name);
            }
            searcher.Close();
            dir.Close();
        }
コード例 #7
0
        public static Query MakeQuery(SpatialStrategy spatialStrategy, string shapeWKT, SpatialRelation relation,
                                      double distanceErrorPct = 0.025)
        {
            SpatialOperation spatialOperation;
            var shape = ReadShape(shapeWKT);

            switch (relation)
            {
            case SpatialRelation.Within:
                spatialOperation = SpatialOperation.IsWithin;
                break;

            case SpatialRelation.Contains:
                spatialOperation = SpatialOperation.Contains;
                break;

            case SpatialRelation.Disjoint:
                spatialOperation = SpatialOperation.IsDisjointTo;
                break;

            case SpatialRelation.Intersects:
                spatialOperation = SpatialOperation.Intersects;
                break;

            case SpatialRelation.Nearby:
                // only sort by this, do not filter
                return(new FunctionQuery(spatialStrategy.MakeDistanceValueSource(shape.GetCenter())));

            default:
                throw new ArgumentOutOfRangeException("relation");
            }
            var args = new SpatialArgs(spatialOperation, shape)
            {
                DistErrPct = distanceErrorPct
            };

            return(spatialStrategy.MakeQuery(args));
        }
コード例 #8
0
        public void runTestQueries(
            IEnumerator <SpatialTestQuery> queries,
            SpatialMatchConcern concern)
        {
            while (queries.MoveNext())
            {
                SpatialTestQuery q = queries.Current;

                String        msg = q.line; //"Query: " + q.args.toString(ctx);
                SearchResults got = executeQuery(strategy.MakeQuery(q.args), 100);
                if (storeShape && got.numFound > 0)
                {
                    //check stored value is there & parses
                    assertNotNull(ctx.ReadShape(got.results[0].document.Get(strategy.GetFieldName())));
                }
                if (concern.orderIsImportant)
                {
                    var ids = q.ids.GetEnumerator();
                    foreach (var r in got.results)
                    {
                        String id = r.document.Get("id");
                        if (!ids.MoveNext())
                        {
                            Assert.Fail(msg + " :: Did not get enough results.  Expected " + q.ids + ", got: " +
                                        got.toDebugString());
                        }
                        Assert.AreEqual(ids.Current, id, "out of order: " + msg);
                    }
                    if (ids.MoveNext())
                    {
                        Assert.Fail(msg + " :: expect more results then we got: " + ids.Current);
                    }
                }
                else
                {
                    // We are looking at how the results overlap
                    if (concern.resultsAreSuperset)
                    {
                        var found = new HashSet <String>();
                        foreach (var r in got.results)
                        {
                            found.Add(r.document.Get("id"));
                        }
                        foreach (String s in q.ids)
                        {
                            if (!found.Contains(s))
                            {
                                Assert.Fail("Results are mising id: " + s + " :: " + found);
                            }
                        }
                    }
                    else
                    {
                        var found = new List <String>();
                        foreach (SearchResult r in got.results)
                        {
                            found.Add(r.document.Get("id"));
                        }

                        // sort both so that the order is not important
                        q.ids.Sort();
                        found.Sort();
                        Assert.AreEqual(q.ids.Count, found.Count);
                        for (var i = 0; i < found.Count; i++)
                        {
                            Assert.AreEqual(q.ids[i], found[i], msg);
                        }
                    }
                }
            }
        }
コード例 #9
0
 /// <summary>
 /// Make a spatial query
 /// </summary>
 /// <param name="lat"></param>
 /// <param name="lng"></param>
 /// <param name="radius">Radius, in miles</param>
 /// <returns></returns>
 public static Query MakeQuery(double lat, double lng, double radius)
 {
     return(strategy.MakeQuery(new SpatialArgs(SpatialOperation.IsWithin, RavenSpatialContext.MakeCircle(lng, lat, radius)), fieldInfo));
 }