Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using NUnit.Framework;
- using SharpTestsEx;
- using Lucene.Net.Analysis;
- using Lucene.Net.Store;
- using Lucene.Net.Index;
- using Lucene.Net.Documents;
- using Lucene.Net.Search;
- namespace Learning.Lucene
- {
- [TestFixture]
- public class BasicOperationsTests
- {
- class Person
- {
- public string Id { get; set; }
- public string Name { get; set; }
- public string Bio { get; set; }
- public string Email { get; set; }
- public Document ToDocument()
- {
- var result = new Document();
- result.Add(new Field(
- "id", Id,
- Field.Store.YES,
- Field.Index.NOT_ANALYZED
- ));
- result.Add(new Field(
- "name", Name,
- Field.Store.YES,
- Field.Index.NO
- ));
- result.Add(new Field(
- "bio", Bio,
- Field.Store.NO,
- Field.Index.ANALYZED
- ));
- result.Add(new Field(
- "email", Email,
- Field.Store.YES,
- Field.Index.ANALYZED
- ));
- return result;
- }
- }
- private IEnumerable<Person> people = new[]
- {
- new Person
- {
- Id = "1",
- Name = "Elemar Junior",
- Bio = "Pai, Programador e Louco",
- Email = "elemarjr@gmail.com"
- },
- new Person
- {
- Id = "2",
- Name = "John Doe",
- Bio = "Something like that. Whaever?!",
- Email = "johndoe@gmail.com"
- }
- };
- private Directory directory;
- [SetUp]
- public void Init()
- {
- directory = new RAMDirectory();
- var writer = GetWriter();
- foreach (var person in people)
- writer.AddDocument(person.ToDocument());
- writer.Close();
- }
- public IndexWriter GetWriter()
- {
- return new IndexWriter(
- directory,
- new WhitespaceAnalyzer(),
- IndexWriter.MaxFieldLength.UNLIMITED
- );
- }
- [Test]
- public void TestIndexWriter()
- {
- var writer = GetWriter();
- writer.NumDocs().Should().Be(people.Count());
- }
- [Test]
- public void TestIndexReader()
- {
- var reader = IndexReader.Open(directory);
- reader.NumDocs().Should().Be(people.Count());
- reader.MaxDoc().Should().Be(people.Count());
- reader.Close();
- }
- [Test]
- public void TestDeleteBeforeOptimize()
- {
- var writer = GetWriter();
- writer.NumDocs().Should().Be(people.Count());
- writer.DeleteDocuments(new Term("id", "1"));
- writer.Commit();
- writer.HasDeletions().Should().Be.True();
- writer.MaxDoc().Should().Be(people.Count());
- writer.NumDocs().Should().Be(people.Count() - 1);
- writer.Close();
- }
- [Test]
- public void TestDeleteAfterOptimize()
- {
- var writer = GetWriter();
- writer.NumDocs().Should().Be(people.Count());
- writer.DeleteDocuments(new Term("id", "1"));
- writer.Optimize();
- writer.Commit();
- writer.HasDeletions().Should().Be.False();
- writer.MaxDoc().Should().Be(people.Count() - 1);
- writer.NumDocs().Should().Be(people.Count() - 1);
- writer.Close();
- }
- public bool EmailExists(string email)
- {
- var searcher = new IndexSearcher(directory, true);
- var term = new Term("email", email);
- var query = new TermQuery(term);
- var result = searcher.Search(query, 1).TotalHits > 0;
- searcher.Close();
- return result;
- }
- [Test]
- public void TestFind()
- {
- EmailExists("elemarjr@gmail.com").Should().Be.True();
- }
- [Test]
- public void TestUpdate()
- {
- EmailExists("elemarjr@gmail.com").Should().Be.True();
- var newPerson = new Person
- {
- Id = "1",
- Name = "Elemar Junior",
- Bio = "Pai, Programador e Louco",
- Email = "elemar@promob.com"
- };
- var writer = GetWriter();
- writer.UpdateDocument(
- new Term("id", "1"),
- newPerson.ToDocument()
- );
- writer.Close();
- EmailExists("elemarjr@gmail.com").Should().Be.False();
- EmailExists("elemar@promob.com").Should().Be.True();
- }
- }
- }
Add Comment
Please, Sign In to add comment