// -*- csharp -*- // LuceneDemo.cs - a simple demo app for Lucene.NET // // Copyright 2004, Silverback Software, LLC // // Permission to use, copy, modify, and distribute this software and // its documentation for any purpose, without fee, and without a // written agreement is hereby granted, provided that the above // copyright notice and this paragraph and the following two // paragraphs appear in all copies. // // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING // LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS // DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT // NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER // IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR // MODIFICATIONS. // // Compile this with: // // csc /target:exe /reference:Lucene.Net.dll LuceneDemo.cs // using System; using System.IO; using Lucene.Net.Analysis; using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; // NOTE: namespace doesn't match class using Lucene.Net.Index; using Lucene.Net.QueryParsers; // NOTE: namespace doesn't match class using Lucene.Net.Search; namespace SilverbackSoftware.LuceneDemo { /// LuceneDemo public class LuceneDemo { public static bool Building = false; /// Main public static void Main(string[] args) { string usage = "usage: LuceneDemo [index filename]|" + "[search query]"; if(args.Length != 2) { Console.WriteLine(usage); return; } switch(args[0]) { case "index": IndexWriter indexWriter = new IndexWriter( "./index.lcn", new StopAnalyzer(), true); indexWriter.AddDocument( FileDocument.Document( new FileInfo(args[1]))); indexWriter.Optimize(); indexWriter.Close(); break; case "search": Searcher searcher = new IndexSearcher("index.lcn"); Analyzer analyzer = new StandardAnalyzer(); Query query = QueryParser.Parse(args[1], "contents", analyzer); Hits hits = searcher.Search(query); Console.WriteLine("{0} hits", hits.Length()); if(hits.Length() > 0) { Console.WriteLine("{0}", hits.Doc(0).Get("path")); } break; default: Console.WriteLine(usage); return; } } } public class FileDocument { /// public static Document Document(FileInfo f) { Document doc = new Document(); doc.Add(Field.Text("path", f.FullName)); doc.Add(Field.Keyword( "modified", DateField.DateToString(f.LastWriteTime))); doc.Add(Field.Text("contents", new StreamReader(f.FullName))); return doc; } private FileDocument() { } } }