public LukeSearchResult <T> Query <T>(Query query, int n) { lock (syncRoot) { if (_searcher != null && !_searcher.IndexReader.IsCurrent() && _activeSearches == 0) { _searcher.Dispose(); _searcher = null; } if (_searcher == null) { _searcher = new IndexSearcher((_writer ?? (_writer = CreateWriter(indexName))).GetReader()); } } LukeSearchResult <T> results; Interlocked.Increment(ref _activeSearches); try { results = _searcher.Query <T>(query, n); } finally { Interlocked.Decrement(ref _activeSearches); } return(results); }
static void Main(string[] args) { // Initialize Everything // ---------------------------------------------------------------- var watch = new Stopwatch(); var writer = GetWriter(); var analyzer = new StandardAnalyzer(Version.LUCENE_29); var query = new TermQuery(new Term("PropString", "teststring")); int iteratorCount; IndexReader reader; IndexSearcher searcher; List <TestClass1> testObjects; List <TestClass1> objectResults; List <Document> documentResults; List <long> Times = new List <long>(); // Create Test Objects // ---------------------------------------------------------------- testObjects = new List <TestClass1>(); for (int i = 0; i < ObjectCount; i++) { testObjects.Add(new TestClass1(i)); } // Test speed of writes // ---------------------------------------------------------------- watch.Reset(); watch.Start(); writer.Write(testObjects.Take(1), analyzer); watch.Stop(); Console.WriteLine("First Write: {0}ms", watch.ElapsedMilliseconds); iteratorCount = IteratorCount; while (iteratorCount-- > 0) { watch.Reset(); watch.Start(); writer.Write(testObjects, analyzer); watch.Stop(); Times.Add(watch.ElapsedMilliseconds); //Console.WriteLine("{0} Objects Written in {1}ms and {2:#,#} ticks", ObjectCount, watch.ElapsedMilliseconds, watch.ElapsedTicks); } Console.WriteLine("Average After Cached: {0}ms", Times.Skip(1).Average()); Times.Clear(); reader = writer.GetReader(); searcher = new IndexSearcher(reader); // Test speed of reads // ---------------------------------------------------------------- iteratorCount = IteratorCount; while (iteratorCount-- > 0) { watch.Reset(); watch.Start(); objectResults = searcher.Query <TestClass1>(query, ObjectCount).ToList(); watch.Stop(); Times.Add(watch.ElapsedMilliseconds); //Console.WriteLine("{0} Objects Read in {1}ms and {2:#,#} ticks", objectResults.Count, watch.ElapsedMilliseconds, watch.ElapsedTicks); } Console.WriteLine("First Read: {0}ms", Times[0]); Console.WriteLine("Average After Cached: {0}ms", Times.Average()); Times.Clear(); // delete all writer.DeleteAll(); Console.WriteLine("-------------- Native Lucene Methods ---------------"); watch.Reset(); watch.Start(); writer.AddDocument(TestClass1.ToDocument(testObjects[0])); watch.Stop(); Console.WriteLine("First Write: {0}ms", watch.ElapsedMilliseconds); iteratorCount = IteratorCount; while (iteratorCount-- > 0) { watch.Reset(); watch.Start(); foreach (var testObject in testObjects) { writer.AddDocument(TestClass1.ToDocument(testObject)); } watch.Stop(); Times.Add(watch.ElapsedMilliseconds); } Console.WriteLine("Average After Cached: {0}ms", Times.Average()); Times.Clear(); iteratorCount = IteratorCount; while (iteratorCount-- > 0) { watch.Reset(); watch.Start(); documentResults = searcher.Search(query, ObjectCount).ScoreDocs.Select(sd => reader.Document(sd.doc)).ToList(); watch.Stop(); Times.Add(watch.ElapsedMilliseconds); } Console.WriteLine("First Read: {0}ms", Times[0]); Console.WriteLine("Average After Cached: {0}ms", Times.Skip(1).Average()); Times.Clear(); // delete all writer.DeleteAll(); Console.ReadLine(); }