public static void Main(System.String[] args) { try { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" }; for (int j = 0; j < docs.Length; j++) { Document d = new Document(); d.Add(Field.Text("contents", docs[j])); writer.AddDocument(d); } writer.Close(); Searcher searcher = new IndexSearcher(directory); System.String[] queries = new System.String[] { "\"a c e\"" }; Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer); parser.SetPhraseSlop(4); for (int j = 0; j < queries.Length; j++) { Query query = parser.Parse(queries[j]); System.Console.Out.WriteLine("Query: " + query.ToString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); hits = searcher.Search(query); System.Console.Out.WriteLine(hits.Length() + " total results"); for (int i = 0; i < hits.Length() && i < 10; i++) { Document d = hits.Doc(i); System.Console.Out.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents")); } } searcher.Close(); } catch (System.Exception e) { System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message); } }
private void DoTestSearch(System.IO.StringWriter out_Renamed, bool useCompoundFile) { Directory directory = new RAMDirectory(); Analyzer analyzer = new SimpleAnalyzer(); IndexWriter writer = new IndexWriter(directory, analyzer, true); writer.SetUseCompoundFile(useCompoundFile); System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" }; for (int j = 0; j < docs.Length; j++) { Document d = new Document(); d.Add(Field.Text("contents", docs[j])); writer.AddDocument(d); } writer.Close(); Searcher searcher = new IndexSearcher(directory); System.String[] queries = new System.String[] { "a b", "\"a b\"", "\"a b c\"", "a c", "\"a c\"", "\"a c e\"" }; Hits hits = null; QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer); parser.SetPhraseSlop(4); for (int j = 0; j < queries.Length; j++) { Query query = parser.Parse(queries[j]); out_Renamed.WriteLine("Query: " + query.ToString("contents")); //DateFilter filter = // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1)); //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01)); //System.out.println(filter); hits = searcher.Search(query); out_Renamed.WriteLine(hits.Length() + " total results"); for (int i = 0; i < hits.Length() && i < 10; i++) { Document d = hits.Doc(i); out_Renamed.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents")); } } searcher.Close(); }