예제 #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         searcherManager?.Dispose();
         analyzer?.Dispose();
         writer?.Dispose();
     }
 }
예제 #2
0
        public SearchResults Search(string queryString)
        {
            int resultsPerPage = 10;
            var analyzer       = SetupAnalyzer();
            var queryParser    = SetupQueryParser(analyzer);
            IEnumerable <FieldDefinition> fields = new List <FieldDefinition> {
                new FieldDefinition {
                    Name = "title", isDefault = true
                },
                new FieldDefinition {
                    Name = "description", isDefault = false
                }
            };
            // Query query = BuildQuery(queryString,queryParser); // BuildQuery(queryString, fields); //
            Query query;

            if (queryString.EndsWith('~'))
            {
                query = BuildQuery(queryString, queryParser);
            }
            else
            {
                query = BuildQuery(queryString, fields);
            }

            using (var writer = new IndexWriter(_directory,
                                                new IndexWriterConfig(MATCH_LUCENE_VERSION, analyzer)))
            {
                var searchManager = new SearcherManager(writer, true, null);
                searchManager.MaybeRefreshBlocking();
                IndexSearcher searcher = searchManager.Acquire();

                try
                {
                    TopDocs topDocs = searcher.Search(query, resultsPerPage);
                    return(CompileResults(searcher, topDocs));
                }
                finally
                {
                    searchManager?.Release(searcher);
                    searchManager?.Dispose();
                    searcher = null;
                    analyzer?.Dispose();
                    ReleaseWriteLock();
                }
            }
        }
예제 #3
0
 public void Dispose()
 {
     searchManager?.Dispose();
     analyzer?.Dispose();
     writer?.Dispose();
 }
예제 #4
0
 public void Dispose()
 {
     indexWriter?.Dispose();
     searcherManager?.Dispose();
 }
예제 #5
0
        public override void Build(IInputEnumerator enumerator)
        {
            if (m_searcherMgr != null)
            {
                m_searcherMgr.Dispose();
                m_searcherMgr = null;
            }

            if (writer != null)
            {
                writer.Dispose();
                writer = null;
            }

            AtomicReader r       = null;
            bool         success = false;

            try
            {
                // First pass: build a temporary normal Lucene index,
                // just indexing the suggestions as they iterate:
                writer = new IndexWriter(dir, GetIndexWriterConfig(matchVersion, GetGramAnalyzer(), OpenMode.CREATE));
                //long t0 = System.nanoTime();

                // TODO: use threads?
                BytesRef text;
                while (enumerator.MoveNext())
                {
                    text = enumerator.Current;
                    BytesRef payload;
                    if (enumerator.HasPayloads)
                    {
                        payload = enumerator.Payload;
                    }
                    else
                    {
                        payload = null;
                    }

                    Add(text, enumerator.Contexts, enumerator.Weight, payload);
                }

                //System.out.println("initial indexing time: " + ((System.nanoTime()-t0)/1000000) + " msec");
                if (commitOnBuild)                      //LUCENENET specific -Support for LUCENE - 5889.
                {
                    Commit();
                }
                m_searcherMgr = new SearcherManager(writer, true, null);
                success       = true;
            }
            finally
            {
                if (success)
                {
                    IOUtils.Dispose(r);
                }
                else
                {
                    IOUtils.DisposeWhileHandlingException(writer, r);
                    writer = null;
                }
            }
        }
예제 #6
0
 public void Dispose()
 {
     _nrtReopenThread.Dispose();
     _searcherManager.Dispose();
     _writer.Dispose();
 }
예제 #7
0
        public virtual void TestMaybeRefreshBlockingLock()
        {
            // make sure that maybeRefreshBlocking releases the lock, otherwise other
            // threads cannot obtain it.
            Directory dir = NewDirectory();
            RandomIndexWriter w = new RandomIndexWriter(Random(), dir);
            w.Dispose();

            SearcherManager sm = new SearcherManager(dir, null);

            ThreadClass t = new ThreadAnonymousInnerClassHelper2(this, sm);
            t.Start();
            t.Join();

            // if maybeRefreshBlocking didn't release the lock, this will fail.
            Assert.IsTrue(sm.MaybeRefresh(), "failde to obtain the refreshLock!");

            sm.Dispose();
            dir.Dispose();
        }
예제 #8
0
 public virtual void TestListenerCalled()
 {
     Directory dir = NewDirectory();
     IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
     AtomicBoolean afterRefreshCalled = new AtomicBoolean(false);
     SearcherManager sm = new SearcherManager(iw, false, new SearcherFactory());
     sm.AddListener(new RefreshListenerAnonymousInnerClassHelper(this, afterRefreshCalled));
     iw.AddDocument(new Document());
     iw.Commit();
     Assert.IsFalse(afterRefreshCalled.Get());
     sm.MaybeRefreshBlocking();
     Assert.IsTrue(afterRefreshCalled.Get());
     sm.Dispose();
     iw.Dispose();
     dir.Dispose();
 }
예제 #9
0
        public virtual void TestEnsureOpen()
        {
            Directory dir = NewDirectory();
            (new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null))).Dispose();
            SearcherManager sm = new SearcherManager(dir, null);
            IndexSearcher s = sm.Acquire();
            sm.Dispose();

            // this should succeed;
            sm.Release(s);

            try
            {
                // this should fail
                sm.Acquire();
            }
            catch (AlreadyClosedException e)
            {
                // ok
            }

            try
            {
                // this should fail
                sm.MaybeRefresh();
            }
            catch (AlreadyClosedException e)
            {
                // ok
            }
            dir.Dispose();
        }
예제 #10
0
 public virtual void TestCloseTwice()
 {
     // test that we can close SM twice (per IDisposable's contract).
     Directory dir = NewDirectory();
     (new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null))).Dispose();
     SearcherManager sm = new SearcherManager(dir, null);
     sm.Dispose();
     sm.Dispose();
     dir.Dispose();
 }
        // LUCENE-5461
        public virtual void TestCRTReopen()
        {
            //test behaving badly

            //should be high enough
            int maxStaleSecs = 20;

            //build crap data just to store it.
            string s = "        abcdefghijklmnopqrstuvwxyz     ";
            char[] chars = s.ToCharArray();
            StringBuilder builder = new StringBuilder(2048);
            for (int i = 0; i < 2048; i++)
            {
                builder.Append(chars[Random().Next(chars.Length)]);
            }
            string content = builder.ToString();

            SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
            Directory dir = new NRTCachingDirectory(NewFSDirectory(CreateTempDir("nrt")), 5, 128);
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new MockAnalyzer(Random()));
            config.SetIndexDeletionPolicy(sdp);
            config.SetOpenMode(IndexWriterConfig.OpenMode_e.CREATE_OR_APPEND);
            IndexWriter iw = new IndexWriter(dir, config);
            SearcherManager sm = new SearcherManager(iw, true, new SearcherFactory());
            TrackingIndexWriter tiw = new TrackingIndexWriter(iw);
            ControlledRealTimeReopenThread<IndexSearcher> controlledRealTimeReopenThread = new ControlledRealTimeReopenThread<IndexSearcher>(tiw, sm, maxStaleSecs, 0);

            controlledRealTimeReopenThread.SetDaemon(true);
            controlledRealTimeReopenThread.Start();

            IList<Thread> commitThreads = new List<Thread>();

            for (int i = 0; i < 500; i++)
            {
                if (i > 0 && i % 50 == 0)
                {
                    Thread commitThread = new Thread(new RunnableAnonymousInnerClassHelper(this, sdp, dir, iw));
                    commitThread.Start();
                    commitThreads.Add(commitThread);
                }
                Document d = new Document();
                d.Add(new TextField("count", i + "", Field.Store.NO));
                d.Add(new TextField("content", content, Field.Store.YES));
                long start = DateTime.Now.Millisecond;
                long l = tiw.AddDocument(d);
                controlledRealTimeReopenThread.WaitForGeneration(l);
                long wait = DateTime.Now.Millisecond - start;
                Assert.IsTrue(wait < (maxStaleSecs * 1000), "waited too long for generation " + wait);
                IndexSearcher searcher = sm.Acquire();
                TopDocs td = searcher.Search(new TermQuery(new Term("count", i + "")), 10);
                sm.Release(searcher);
                Assert.AreEqual(1, td.TotalHits);
            }

            foreach (Thread commitThread in commitThreads)
            {
                commitThread.Join();
            }

            controlledRealTimeReopenThread.Dispose();
            sm.Dispose();
            iw.Dispose();
            dir.Dispose();
        }