using System; using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using NUnit.Framework; using VideoGameQuotes.Api.Search.Lucene; namespace VideoGameQuotes.Api.Tests { [TestFixture, Ignore] public class LuceneTests { [Test] public void Should_use_lucene() { var analyzer = new StandardAnalyzer(new [] { "the", "of", "a", "an" }); const string indexDirectory = @"c:\users\tmont\code\lucene_index_test"; var indexWriter = new Lucene.Net.Index.IndexWriter(indexDirectory, analyzer, true); indexWriter.AddDocument(CreateDocument(4, "cat")); indexWriter.AddDocument(CreateDocument(5, "catty")); indexWriter.AddDocument(CreateDocument(6, "Catherine")); indexWriter.AddDocument(CreateDocument(7, "vacate")); indexWriter.AddDocument(CreateDocument(8, "cat cat cat")); indexWriter.AddDocument(CreateDocument(9, "i'm trying to herd a cat, a cat, I say!")); indexWriter.AddDocument(CreateDocument(10, "the cat")); indexWriter.Optimize(); indexWriter.Close(); var parser = new QueryParser("text", analyzer); var query = parser.Parse("cat"); var searcher = new IndexSearcher(indexDirectory); var hits = searcher.Search(query).ToEnumerable(); foreach (var hit in hits) { Console.WriteLine("id: {0}, document: {1}, score: {2}", hit.GetId(), hit.GetDocument(), hit.GetScore()); } } private static Document CreateDocument(int id, string text) { var document = new Document(); document.Add(new Field("id", id.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED)); document.Add(new Field("text", text, Field.Store.YES, Field.Index.TOKENIZED)); return document; } } }