Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Lucene.Net.Analysis.Standard;
- using Lucene.Net.Documents;
- using Lucene.Net.Index;
- using Lucene.Net.QueryParsers;
- using Lucene.Net.Search;
- using Lucene.Net.Store;
- using Version = Lucene.Net.Util.Version;
- namespace TestProgram
- {
- class Program
- {
- public class TestSim : DefaultSimilarity
- {
- public override float Tf(float freq)
- {
- return freq;
- }
- }
- public static void Main(string[] args)
- {
- var analyzer = new StandardAnalyzer(Version.LUCENE_29);
- using (var directory = new RAMDirectory())
- {
- using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
- {
- writer.SetSimilarity(new TestSim());
- var d = new Document();
- d.Add(new Field("Description", "one two", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
- writer.AddDocument(d);
- d = new Document();
- d.Add(new Field("Description", "one three", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
- writer.AddDocument(d);
- d = new Document();
- d.Add(new Field("Description", "one blah blah blah blah one", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
- writer.AddDocument(d);
- }
- // set up lucene searcher
- using (var searcher = new IndexSearcher(directory, false))
- {
- var hits_limit = 1000;
- searcher.Similarity = new TestSim();
- var parser = new QueryParser(Version.LUCENE_29, "Description", analyzer);
- var query = parser.Parse("one");
- Console.WriteLine(query.GetSimilarity(searcher).GetType());
- var hits = searcher.Search(query, null, hits_limit, Sort.RELEVANCE).ScoreDocs;
- foreach (var hit in hits)
- {
- Console.WriteLine("Hit Doc: {0}", hit.Doc);
- Console.WriteLine("Hit Score: {0}", hit.Score);
- var d = searcher.Doc(hit.Doc);
- Console.WriteLine("Contents: {0}", d.Get("Description"));
- Console.WriteLine();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement