Ejemplo n.º 1
0
        public void TestAntiM()
        {
            _searcher = new IndexSearcher(_directory, true);

            const double miles = 6.0;

            Console.WriteLine("testAntiM");
            // create a distance query
            var dq = new DistanceQueryBuilder(_lat, _lng, miles, LatField, LngField, CartesianTierPlotter.DefaltFieldPrefix, true);

            Console.WriteLine(dq);
            //create a term query to search against all documents
            Query tq = new TermQuery(new Term("metafile", "doc"));

            var  dsort = new DistanceFieldComparatorSource(dq.DistanceFilter);
            Sort sort  = new Sort(new SortField("foo", dsort, false));

            // Perform the search, using the term query, the distance filter, and the
            // distance sort
            TopDocs hits    = _searcher.Search(tq, dq.Filter, 1000, sort);
            int     results = hits.totalHits;

            ScoreDoc[] scoreDocs = hits.scoreDocs;

            // Get a list of distances
            Dictionary <int, Double> distances = dq.DistanceFilter.Distances;


            Console.WriteLine("Distance Filter filtered: " + distances.Count);
            Console.WriteLine("Results: " + results);
            Console.WriteLine("=============================");
            Console.WriteLine("Distances should be 7 " + distances.Count);
            Console.WriteLine("Results should be 7 " + results);

            Assert.AreEqual(7, distances.Count);             // fixed a store of only needed distances
            Assert.AreEqual(7, results);

            double lastDistance = 0;

            for (int i = 0; i < results; i++)
            {
                Document d = _searcher.Doc(scoreDocs[i].doc);

                String name         = d.Get("name");
                double rsLat        = NumericUtils.PrefixCodedToDouble(d.Get(LatField));
                double rsLng        = NumericUtils.PrefixCodedToDouble(d.Get(LngField));
                Double geo_distance = distances[scoreDocs[i].doc];

                double distance = DistanceUtils.GetInstance().GetDistanceMi(_lat, _lng, rsLat, rsLng);
                double llm      = DistanceUtils.GetInstance().GetLLMDistance(_lat, _lng, rsLat, rsLng);

                Console.WriteLine("Name: " + name + ", Distance " + distance);

                Assert.IsTrue(Math.Abs((distance - llm)) < 1);
                Assert.IsTrue((distance < miles));
                Assert.IsTrue(geo_distance >= lastDistance);

                lastDistance = geo_distance;
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <Service> Execute(ISession session)
        {
            var serviceType   = typeof(Service);
            var dq            = new DistanceQueryBuilder(_userLocation.Y, _userLocation.X, _distance * MeterInMiles, "Location_Latitude", "Location_Longitude", CartesianTierPlotter.DefaltFieldPrefix, false);
            var metaTermQuery = new TermQuery(new Term("metafile", "doc"));

            var  dsort = new DistanceFieldComparatorSource(dq.DistanceFilter);
            Sort sort  = new Sort(new SortField("Location", dsort, false));

            var booleanQuery = new BooleanQuery();

            booleanQuery.Add(metaTermQuery, BooleanClause.Occur.SHOULD);

            if (!string.IsNullOrWhiteSpace(_searchModel.Terms))
            {
                var analyzer    = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
                var queryParser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, new[] { "Title", "Body", "Category_Name" }, analyzer);
                var termQuery   = queryParser.Parse(_searchModel.Terms);

                booleanQuery.Add(termQuery, BooleanClause.Occur.MUST);
            }

            if (_searchModel.CategoryId != null)
            {
                var categoryQuery = new TermQuery(new Term("Category_Id", _searchModel.CategoryId.Value.ToString()));
                booleanQuery.Add(categoryQuery, BooleanClause.Occur.MUST);
            }

            if (_searchModel.Type != null)
            {
                if (_searchModel.Type.Value == (byte)ServiceType.Offering)
                {
                    serviceType = typeof(ServiceOffering);
                }
                else if (_searchModel.Type.Value == (byte)ServiceType.Request)
                {
                    serviceType = typeof(ServiceRequest);
                }
            }

            return(session.FullTextSession().CreateFullTextQuery(booleanQuery, serviceType)
                   .SetFilter(dq.Filter).SetFetchSize(PageSize).List <Service>());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Searches the lucene index with the search text.
        /// </summary>
        /// <param name="searchText">The text to search with.</param>
        /// <remarks>Syntax reference: http://lucene.apache.org/java/2_3_2/queryparsersyntax.html#Wildcard</remarks>
        /// <exception cref="SpatialSearchException">An error occured searching the lucene.net index.</exception>
        public SpatialSearchResultsModel SearchIndex(double lat, double @long, double miles)
        {
            // This check is for the benefit of the CI builds
            if (!Directory.Exists(_indexPath))
            {
                CreateIndex();
            }

            EnsureDistanceMatrixReady();

            var model = new SpatialSearchResultsModel();

            //StandardAnalyzer analyzer = new StandardAnalyzer();
            try
            {
                IndexSearcher searcher = new IndexSearcher(_indexPath, true);
                // Build query
                var dq = new DistanceQueryBuilder(lat, @long, miles, LatField, LngField, CartesianTierPlotter.DefaltFieldPrefix, true);

                //create a term query to search against all documents
                Query tq = new TermQuery(new Term("metafile", "doc"));

                var  dsort = new DistanceFieldComparatorSource(dq.DistanceFilter);
                Sort sort  = new Sort(new SortField("foo", dsort, false));

                // Perform the search, using the term query, the distance filter, and the
                // distance sort
                TopDocs    hits      = searcher.Search(tq, dq.Filter, 1000, sort);
                int        results   = hits.totalHits;
                ScoreDoc[] scoreDocs = hits.scoreDocs;

                // Get a list of distances
                Dictionary <int, Double> distances = dq.DistanceFilter.Distances;

                model.QueryLat   = lat;
                model.QueryLong  = @long;
                model.QueryMiles = miles;

                //double lastDistance = 0;
                for (int i = 0; i < results; i++)
                {
                    Document d = searcher.Doc(scoreDocs[i].doc);

                    String name         = d.Get("name");
                    double rsLat        = NumericUtils.PrefixCodedToDouble(d.Get(LatField));
                    double rsLng        = NumericUtils.PrefixCodedToDouble(d.Get(LngField));
                    int    id           = int.Parse(d.Get("PostID"));
                    Double geo_distance = distances[scoreDocs[i].doc];

                    double distance = DistanceUtils.GetInstance().GetDistanceMi(lat, @long, rsLat, rsLng);
                    double llm      = DistanceUtils.GetInstance().GetLLMDistance(lat, @long, rsLat, rsLng);

                    model.Results.Add(new SpatialResult()
                    {
                        title = name, instrumentID = id, dist = distance, lat = rsLat, lng = rsLng, resultNum = i + 1
                    });

                    //lastDistance = geo_distance;
                }
            }
            catch (Exception ex)
            {
                throw new SpatialSearchException(ex, "An error occurred while searching the spatial index.");
            }

            return(model);
        }