public static Open ( |
||
path | ||
return |
static void Main(string[] args) { //Analyzer analyzer = new StandardAnalyzer(); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_23); Directory directory = null; //directory.CreateOutput(@"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory"); while (true) { Console.Write("id:"); string id = Console.ReadLine().ToString(); Console.Write("title:"); string title = Console.ReadLine().ToString(); Console.Write("content:"); string content = Console.ReadLine().ToString(); Console.WriteLine("================================="); string indexPath = @"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory"; directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath)); IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); AddDocument(writer, id, title, content); writer.Optimize(); writer.Dispose(); } //AddDocument(writer,"1","name","ada"); //AddDocument(writer, "2","SQL Server 2008 的发布ada", "SQL Server 2008 的新特性"); //AddDocument(writer, "3","ASP.Net MVC框架配置与分析", "而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions"); }
static void Main(string[] args) { Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_23); //IndexWriter writer = new IndexWriter("IndexDirectory", analyzer, true); //AddDocument(writer, "SQL Server 2008 的发布", "SQL Server 2008 的新特性"); //AddDocument(writer, "ASP.Net MVC框架配置与分析", "而今,微软推出了新的MVC开发框架,也就是Microsoft ASP.NET 3.5 Extensions"); //writer.Optimize(); //writer.Close(); while (true) { Console.Write("key:"); string key = Console.ReadLine().ToString(); string indexPath = @"D:\workspace\C#\lucene\lucene\lucene\IndexDirectory"; Directory directory = null; directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath)); IndexSearcher searcher = new IndexSearcher(directory); MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_23, new string[] { "title", "content" }, analyzer); Query query = parser.Parse(key); TopDocs hits = searcher.Search(query, (Filter)null, 1000); foreach (ScoreDoc hit in hits.ScoreDocs) { Document doc = searcher.Doc(hit.Doc); string id = doc.Get("id"); string title = doc.Get("title"); string content = doc.Get("content"); Console.WriteLine("title:{0},content:{1},id:{2}", title, content, id); } searcher.Dispose(); Console.WriteLine("================================="); } Console.ReadKey(); }
public static int Main(System.String[] args) { bool doFix = false; var onlySegments = new List <string>(); System.String indexPath = null; int i = 0; while (i < args.Length) { if (args[i].Equals("-fix")) { doFix = true; i++; } else if (args[i].Equals("-segment")) { if (i == args.Length - 1) { System.Console.Out.WriteLine("ERROR: missing name for -segment option"); return(1); } onlySegments.Add(args[i + 1]); i += 2; } else { if (indexPath != null) { System.Console.Out.WriteLine("ERROR: unexpected extra argument '" + args[i] + "'"); return(1); } indexPath = args[i]; i++; } } if (indexPath == null) { System.Console.Out.WriteLine("\nERROR: index path not specified"); System.Console.Out.WriteLine("\nUsage: java Lucene.Net.Index.CheckIndex pathToIndex [-fix] [-segment X] [-segment Y]\n" + "\n" + " -fix: actually write a new segments_N file, removing any problematic segments\n" + " -segment X: only check the specified segments. This can be specified multiple\n" + " times, to check more than one segment, eg '-segment _2 -segment _a'.\n" + " You can't use this with the -fix option\n" + "\n" + "**WARNING**: -fix should only be used on an emergency basis as it will cause\n" + "documents (perhaps many) to be permanently removed from the index. Always make\n" + "a backup copy of your index before running this! Do not run this tool on an index\n" + "that is actively being written to. You have been warned!\n" + "\n" + "Run without -fix, this tool will open the index, report version information\n" + "and report any exceptions it hits and what action it would take if -fix were\n" + "specified. With -fix, this tool will remove any segments that have issues and\n" + "write a new segments_N file. This means all documents contained in the affected\n" + "segments will be removed.\n" + "\n" + "This tool exits with exit code 1 if the index cannot be opened or has any\n" + "corruption, else 0.\n"); return(1); } if (!AssertsOn()) { System.Console.Out.WriteLine("\nNOTE: testing will be more thorough if you run java with '-ea:Lucene.Net...', so assertions are enabled"); } if (onlySegments.Count == 0) { onlySegments = null; } else if (doFix) { System.Console.Out.WriteLine("ERROR: cannot specify both -fix and -segment"); return(1); } System.Console.Out.WriteLine("\nOpening index @ " + indexPath + "\n"); Directory dir = null; try { dir = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath)); } catch (Exception t) { Console.Out.WriteLine("ERROR: could not open directory \"" + indexPath + "\"; exiting"); Console.Out.WriteLine(t.StackTrace); return(1); } var checker = new CheckIndex(dir); var tempWriter = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Console.Out.Encoding) { AutoFlush = true }; checker.SetInfoStream(tempWriter); Status result = checker.CheckIndex_Renamed_Method(onlySegments, null); if (result.missingSegments) { return(1); } if (!result.clean) { if (!doFix) { System.Console.Out.WriteLine("WARNING: would write new segments file, and " + result.totLoseDocCount + " documents would be lost, if -fix were specified\n"); } else { Console.Out.WriteLine("WARNING: " + result.totLoseDocCount + " documents will be lost\n"); Console.Out.WriteLine("NOTE: will write new segments file in 5 seconds; this will remove " + result.totLoseDocCount + " docs from the index. THIS IS YOUR LAST CHANCE TO CTRL+C!"); for (var s = 0; s < 5; s++) { System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 1000)); System.Console.Out.WriteLine(" " + (5 - s) + "..."); } Console.Out.WriteLine("Writing..."); checker.FixIndex(result, null); Console.Out.WriteLine("OK"); Console.Out.WriteLine("Wrote new segments file \"" + result.newSegments.GetCurrentSegmentFileName() + "\""); } } System.Console.Out.WriteLine(""); int exitCode; if (result != null && result.clean == true) { exitCode = 0; } else { exitCode = 1; } return(exitCode); }
public virtual void TestNorms() { // tmp dir System.String tempDir = System.IO.Path.GetTempPath(); if (tempDir == null) { throw new System.IO.IOException("java.io.tmpdir undefined, cannot run test"); } // test with a single index: index1 System.IO.DirectoryInfo indexDir1 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex1")); Directory dir1 = FSDirectory.Open(indexDir1); IndexWriter.Unlock(dir1); norms = new System.Collections.ArrayList(); modifiedNorms = new System.Collections.ArrayList(); CreateIndex(dir1); DoTestNorms(dir1); // test with a single index: index2 System.Collections.ArrayList norms1 = norms; System.Collections.ArrayList modifiedNorms1 = modifiedNorms; int numDocNorms1 = numDocNorms; norms = new System.Collections.ArrayList(); modifiedNorms = new System.Collections.ArrayList(); numDocNorms = 0; System.IO.DirectoryInfo indexDir2 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex2")); Directory dir2 = FSDirectory.Open(indexDir2); CreateIndex(dir2); DoTestNorms(dir2); // add index1 and index2 to a third index: index3 System.IO.DirectoryInfo indexDir3 = new System.IO.DirectoryInfo(System.IO.Path.Combine(tempDir, "lucenetestindex3")); Directory dir3 = FSDirectory.Open(indexDir3); CreateIndex(dir3); IndexWriter iw = new IndexWriter(dir3, anlzr, false, IndexWriter.MaxFieldLength.LIMITED); iw.SetMaxBufferedDocs(5); iw.MergeFactor = 3; iw.AddIndexesNoOptimize(new Directory[] { dir1, dir2 }); iw.Optimize(); iw.Close(); norms1.AddRange(norms); norms = norms1; modifiedNorms1.AddRange(modifiedNorms); modifiedNorms = modifiedNorms1; numDocNorms += numDocNorms1; // test with index3 VerifyIndex(dir3); DoTestNorms(dir3); // now with optimize iw = new IndexWriter(dir3, anlzr, false, IndexWriter.MaxFieldLength.LIMITED); iw.SetMaxBufferedDocs(5); iw.MergeFactor = 3; iw.Optimize(); iw.Close(); VerifyIndex(dir3); dir1.Close(); dir2.Close(); dir3.Close(); }
public FaultyFSDirectory(System.IO.DirectoryInfo dir) { fsDir = FSDirectory.Open(dir); interalLockFactory = fsDir.LockFactory; }
public virtual void TestLazyPerformance() { System.String tmpIODir = AppSettings.Get("tempDir", Path.GetTempPath()); System.String path = tmpIODir + System.IO.Path.DirectorySeparatorChar.ToString() + "lazyDir" + Guid.NewGuid(); System.IO.DirectoryInfo file = new System.IO.DirectoryInfo(path); _TestUtil.RmDir(file); FSDirectory tmpDir = FSDirectory.Open(file); Assert.IsTrue(tmpDir != null); IndexWriter writer = new IndexWriter(tmpDir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED, null); writer.UseCompoundFile = false; writer.AddDocument(testDoc, null); writer.Close(); Assert.IsTrue(fieldInfos != null); FieldsReader reader; long lazyTime = 0; long regularTime = 0; int length = 50; ISet <string> lazyFieldNames = Support.Compatibility.SetFactory.CreateHashSet <string>(); lazyFieldNames.Add(DocHelper.LARGE_LAZY_FIELD_KEY); SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(Support.Compatibility.SetFactory.CreateHashSet <string>(), lazyFieldNames); for (int i = 0; i < length; i++) { reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos, null); Assert.IsTrue(reader != null); Assert.IsTrue(reader.Size() == 1); Document doc; doc = reader.Doc(0, null, null); //Load all of them Assert.IsTrue(doc != null, "doc is null and it shouldn't be"); IFieldable field = doc.GetFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); Assert.IsTrue(field.IsLazy == false, "field is lazy"); System.String value_Renamed; long start; long finish; start = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); //On my machine this was always 0ms. value_Renamed = field.StringValue(null); finish = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be"); Assert.IsTrue(field != null, "field is null and it shouldn't be"); regularTime += (finish - start); reader.Dispose(); reader = null; doc = null; //Hmmm, are we still in cache??? System.GC.Collect(); reader = new FieldsReader(tmpDir, TEST_SEGMENT_NAME, fieldInfos, null); doc = reader.Doc(0, fieldSelector, null); field = doc.GetFieldable(DocHelper.LARGE_LAZY_FIELD_KEY); Assert.IsTrue(field.IsLazy == true, "field is not lazy"); start = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); //On my machine this took around 50 - 70ms value_Renamed = field.StringValue(null); finish = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be"); lazyTime += (finish - start); reader.Dispose(); } System.Console.Out.WriteLine("Average Non-lazy time (should be very close to zero): " + regularTime / length + " ms for " + length + " reads"); System.Console.Out.WriteLine("Average Lazy Time (should be greater than zero): " + lazyTime / length + " ms for " + length + " reads"); }
public virtual void TestFSDirectoryFilter() { CheckDirectoryFilter(FSDirectory.Open(new System.IO.DirectoryInfo("test"))); }