Esempio n. 1
0
        public void Check_IndexReader_Gets_Closed()
        {
            var dir     = new RAMDirectory();
            var writer  = new IndexWriter(dir, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
            var manager = new SearcherManager(dir);

            var searcher1 = manager.GetSearcher();

            manager.ReleaseSearcher(searcher1);
            manager.ReleaseSearcher(searcher1); // The SearcherManager ctor creates an IndexReader.

            Assert.Throws <AlreadyClosedException>(() => manager.ReleaseSearcher(searcher1));
        }
Esempio n. 2
0
        public static void AutoCompleteSample()
        {
            Directory dir = new RAMDirectory();

            // Create n-edge grams for field suggestionText
            Analyzer analyzer = new EdgeNGramAnalyzer(Version);

            var indexWriter = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            var docs = CreateAutoCompleteDocuments(GetDeals());

            foreach (var doc in docs)
            {
                indexWriter.AddDocument(doc);
            }

            indexWriter.Commit();
            indexWriter.Dispose();

            var searcherManager = new SearcherManager(dir);

            // Default sort by dicount pct desc.
            var       sort           = new Sort(new SortField(DiscountPctFieldName, SortField.INT, true));
            const int maxSuggestions = 5;

            for (;;)
            {
                Console.Write("Enter a text for auto completion and press enter: ");
                var input = Console.ReadLine();

                Query query = new TermQuery(new Term(SuggestionTextFieldName, input));

                var     searcher = searcherManager.GetSearcher();
                TopDocs hits;
                try
                {
                    hits = searcher.Search(query, null, maxSuggestions, sort);
                }
                finally
                {
                    searcherManager.ReleaseSearcher(searcher);
                }

                foreach (ScoreDoc match in hits.ScoreDocs)
                {
                    Document doc = searcher.Doc(match.Doc);
                    Console.WriteLine("Matched: '{0}' in '{1}' by '{2}'",
                                      doc.Get(SuggestionTextFieldName), doc.Get(LocationNameFieldName), doc.Get(SupplierFieldName));
                }
            }
        }
Esempio n. 3
0
        public void Check_IndexReader_Get_Closed_When_Creating_New()
        {
            var dir     = new RAMDirectory();
            var writer  = new IndexWriter(dir, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
            var manager = new SearcherManager(dir);

            var searcher1 = manager.GetSearcher();

            // Change the index
            writer.AddDocument(new Document());
            writer.Commit();

            // Reopen the IndexReader
            manager.MaybeReopen();

            var searcher2 = manager.GetSearcher();

            Assert.AreNotEqual(searcher1.IndexReader, searcher2.IndexReader, "A new IndexReader was not created.");
            manager.ReleaseSearcher(searcher2);

            manager.ReleaseSearcher(searcher1);
            Assert.Throws <AlreadyClosedException>(() => manager.ReleaseSearcher(searcher1));
        }
        private string[] Search(SearcherManager manager, string searchString)
        {
            var parser = new QueryParser(LuceneVersion, FieldName, _analyzer);

            Query q = parser.Parse(searchString);

            var searcher = manager.GetSearcher();

            TopDocs hits = searcher.Search(q, null, 100, Sort.RELEVANCE);

            var resultStrings = new List <string>(hits.ScoreDocs.Length);

            resultStrings.AddRange(hits.ScoreDocs.Select(match => searcher.Doc(match.Doc)).Select(doc => doc.Get(FieldName)));

            manager.ReleaseSearcher(searcher);

            return(resultStrings.ToArray());
        }
Esempio n. 5
0
        public static void SearchSample()
        {
            Directory dir = new RAMDirectory();

            Analyzer analyzer = new StandardAnalyzer(Version);

            var indexWriter = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

            SpatialContext ctx      = SpatialContext.GEO;
            var            strategy = new PointVectorStrategy(ctx, SpartialFieldName);
            //var precision = 8; // Precision 8 means down to 19 meter - higher precision consumes more memory
            //SpatialPrefixTree grid = new GeohashPrefixTree(ctx, precision);
            //var strategy = new RecursivePrefixTreeStrategy(grid, spartialFieldName);

            var docs = CreateSearchDocuments(GetDeals(), strategy);

            foreach (var doc in docs)
            {
                indexWriter.AddDocument(doc);
            }

            indexWriter.Commit();
            indexWriter.Dispose();

            // "Current" position
            Point littleMermaid = ctx.MakePoint(12.599239, 55.692848);

            //var parser = new QueryParser(Version, "title", analyzer);
            //Query q = parser.Parse("deal");
            Query q = new MatchAllDocsQuery(); // NOTE: MatchAllDocsQuery always returns score as 1.0

            // Add distance from current point to the scoring
            q = new DistanceCustomScoreQuery(q, strategy, littleMermaid);
            //q = new RecursivePrefixTreeStrategyDistanceCustomScoreQuery(q, strategy, littleMermaid, spartialFieldName);

            // Remove everything more than 2000 km away
            var filter = strategy.MakeFilter(new SpatialArgs(SpatialOperation.Intersects,
                                                             ctx.MakeCircle(littleMermaid, DistanceUtils.Dist2Degrees(2000, DistanceUtils.EARTH_MEAN_RADIUS_KM))));

            // Ensures the most recent searcher is used without destroying the Lucene IndexReader cache (via NRT)
            var searcherManager = new SearcherManager(dir);

            var collector = new GroupTopDocsCollector(5, SupplierFieldName);

            var searcher = searcherManager.GetSearcher();

            try
            {
                searcher.Search(q, filter, collector);
            }
            finally
            {
                searcherManager.ReleaseSearcher(searcher);
            }

            var hits = collector.GroupTopDocs();

            Console.WriteLine("Found {0} document(s) that matched query '{1}':", hits.TotalHits, q);
            foreach (var match in hits.GroupScoreDocs)
            {
                Document doc = searcher.Doc(match.Doc);
                Console.WriteLine("Best match '{0}' in group '{1}' with count {2} (MaxDoc: Score {3} Location '{4}')",
                                  doc.Get(TitleFieldName), match.GroupFieldValue, match.GroupCount, match.Score, doc.Get(LocationNameFieldName));
            }
        }