Advertisement
Guest User

TestLucene

a guest
Jan 14th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using Lucene.Net.Analysis.Standard;
  3. using Lucene.Net.Documents;
  4. using Lucene.Net.Index;
  5. using Lucene.Net.QueryParsers;
  6. using Lucene.Net.Search;
  7. using Lucene.Net.Store;
  8. using Version = Lucene.Net.Util.Version;
  9.  
  10. namespace TestProgram
  11. {
  12.     class Program
  13.     {
  14.         public class TestSim : DefaultSimilarity
  15.         {
  16.             public override float Tf(float freq)
  17.             {
  18.                return freq;
  19.             }
  20.         }
  21.  
  22.         public static void Main(string[] args)
  23.         {
  24.             var analyzer = new StandardAnalyzer(Version.LUCENE_29);
  25.             using (var directory = new RAMDirectory())
  26.             {
  27.                 using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
  28.                 {
  29.                     writer.SetSimilarity(new TestSim());
  30.  
  31.                     var d = new Document();
  32.                     d.Add(new Field("Description", "one two", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
  33.                     writer.AddDocument(d);
  34.  
  35.                     d = new Document();
  36.                     d.Add(new Field("Description", "one three", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
  37.                     writer.AddDocument(d);
  38.  
  39.                     d = new Document();
  40.                     d.Add(new Field("Description", "one blah blah blah blah one", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
  41.                     writer.AddDocument(d);
  42.                 }
  43.  
  44.                 // set up lucene searcher
  45.                 using (var searcher = new IndexSearcher(directory, false))
  46.                 {
  47.                     var hits_limit = 1000;
  48.  
  49.                     searcher.Similarity = new TestSim();
  50.  
  51.                     var parser = new QueryParser(Version.LUCENE_29, "Description", analyzer);
  52.                     var query = parser.Parse("one");
  53.  
  54.                     Console.WriteLine(query.GetSimilarity(searcher).GetType());
  55.  
  56.                     var hits = searcher.Search(query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
  57.  
  58.                     foreach (var hit in hits)
  59.                     {
  60.                         Console.WriteLine("Hit Doc: {0}", hit.Doc);
  61.                         Console.WriteLine("Hit Score: {0}", hit.Score);
  62.  
  63.                         var d = searcher.Doc(hit.Doc);
  64.                         Console.WriteLine("Contents: {0}", d.Get("Description"));
  65.                         Console.WriteLine();
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement