public virtual void TestDemo_Renamed_Method() { Analyzer analyzer = new StandardAnalyzer(); // Store the index in memory: Directory directory = new RAMDirectory(); // To store an index on disk, use this instead (note that the // parameter true will overwrite the index in that directory // if one exists): //Directory directory = FSDirectory.getDirectory("/tmp/testindex", true); IndexWriter iwriter = new IndexWriter(directory, analyzer, true); iwriter.SetMaxFieldLength(25000); Document doc = new Document(); System.String text = "This is the text to be indexed."; doc.Add(new Field("fieldname", text, Field.Store.YES, Field.Index.TOKENIZED)); iwriter.AddDocument(doc); iwriter.Close(); // Now search the index: IndexSearcher isearcher = new IndexSearcher(directory); // Parse a simple query that searches for "text": Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser("fieldname", analyzer); Query query = parser.Parse("text"); Hits hits = isearcher.Search(query); Assert.AreEqual(1, hits.Length()); // Iterate through the results: for (int i = 0; i < hits.Length(); i++) { Document hitDoc = hits.Doc(i); Assert.AreEqual("This is the text to be indexed.", hitDoc.Get("fieldname")); } isearcher.Close(); directory.Close(); }
public static void Main(System.String[] argv) { try { System.String index = "index"; bool create = false; System.IO.FileInfo root = null; System.String usage = "IndexHTML [-create] [-index <index>] <root_directory>"; if (argv.Length == 0) { System.Console.Error.WriteLine("Usage: " + usage); return ; } for (int i = 0; i < argv.Length; i++) { if (argv[i].Equals("-index")) { // parse -index option index = argv[++i]; } else if (argv[i].Equals("-create")) { // parse -create option create = true; } else if (i != argv.Length - 1) { System.Console.Error.WriteLine("Usage: " + usage); return ; } else root = new System.IO.FileInfo(argv[i]); } System.DateTime start = System.DateTime.Now; if (!create) { // delete stale docs deleting = true; IndexDocs(root, index, create); } writer = new IndexWriter(index, new StandardAnalyzer(), create); writer.SetMaxFieldLength(1000000); IndexDocs(root, index, create); // add new docs System.Console.Out.WriteLine("Optimizing index..."); writer.Optimize(); writer.Close(); System.DateTime end = System.DateTime.Now; System.Console.Out.Write(end.Millisecond - start.Millisecond); System.Console.Out.WriteLine(" total milliseconds"); } catch (System.Exception e) { System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message); } }