Beispiel #1
0
        public void InitializeData_DocumentNotAvailable()
        {
            IInMemoryIndex index = (IInMemoryIndex)GetIndex();

            IDocument doc        = MockDocument("doc", "Document", "doc", DateTime.Now);
            IDocument inexistent = MockDocument2("inexistent", "Inexistent", "doc", DateTime.Now);

            DumpedDocument[] documents = new DumpedDocument[] {
                new DumpedDocument(doc),
                new DumpedDocument(inexistent)
            };

            DumpedWord[] words = new DumpedWord[] {
                new DumpedWord(new Word(1, "document")),
                new DumpedWord(new Word(2, "this")),
                new DumpedWord(new Word(3, "is")),
                new DumpedWord(new Word(4, "some")),
                new DumpedWord(new Word(5, "content")),

                new DumpedWord(new Word(6, "inexistent")),
                new DumpedWord(new Word(7, "dummy")),
                new DumpedWord(new Word(8, "text")),
                new DumpedWord(new Word(9, "used")),
                new DumpedWord(new Word(10, "for")),
                new DumpedWord(new Word(11, "testing")),
                new DumpedWord(new Word(12, "purposes"))
            };

            DumpedWordMapping[] mappings = new DumpedWordMapping[] {
                new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)),
                new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)),
                new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)),
                new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)),
                new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content)),

                new DumpedWordMapping(words[5].ID, documents[1].ID, new BasicWordInfo(0, 0, WordLocation.Title)),
                new DumpedWordMapping(words[6].ID, documents[1].ID, new BasicWordInfo(0, 0, WordLocation.Content)),
                new DumpedWordMapping(words[7].ID, documents[1].ID, new BasicWordInfo(6, 1, WordLocation.Content)),
                new DumpedWordMapping(words[8].ID, documents[1].ID, new BasicWordInfo(11, 2, WordLocation.Content)),
                new DumpedWordMapping(words[9].ID, documents[1].ID, new BasicWordInfo(16, 3, WordLocation.Content)),
                new DumpedWordMapping(words[10].ID, documents[1].ID, new BasicWordInfo(20, 4, WordLocation.Content)),
                new DumpedWordMapping(words[11].ID, documents[1].ID, new BasicWordInfo(28, 5, WordLocation.Content))
            };

            index.SetBuildDocumentDelegate(delegate(DumpedDocument d) {
                if (d.Name == "doc")
                {
                    return(doc);
                }
                else
                {
                    return(null);
                }
            });

            index.InitializeData(documents, words, mappings);

            Assert.AreEqual(1, index.Search(new SearchParameters("this")).Count, "Wrong result count");
            Assert.AreEqual(0, index.Search(new SearchParameters("dummy")).Count, "Wrong result count");
        }
Beispiel #2
0
        public void InitializeData()
        {
            IInMemoryIndex index = (IInMemoryIndex)GetIndex();

            IDocument d = MockDocument("doc", "Document", "doc", DateTime.Now);

            DumpedDocument[] documents = new DumpedDocument[] { new DumpedDocument(d) };

            DumpedWord[] words = new DumpedWord[] {
                new DumpedWord(new Word(1, "document")),
                new DumpedWord(new Word(2, "this")),
                new DumpedWord(new Word(3, "is")),
                new DumpedWord(new Word(4, "some")),
                new DumpedWord(new Word(5, "content"))
            };

            DumpedWordMapping[] mappings = new DumpedWordMapping[] {
                new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)),
                new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)),
                new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)),
                new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)),
                new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content))
            };

            index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(d); });

            index.InitializeData(documents, words, mappings);

            Assert.AreEqual(1, index.TotalDocuments, "Wrong document count");
            Assert.AreEqual(5, index.TotalWords, "Wrong word count");
            Assert.AreEqual(5, index.TotalOccurrences, "Wrong occurrence count");

            SearchResultCollection res = index.Search(new SearchParameters("document content"));

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(2, res[0].Matches.Count, "Wrong matches count");

            Assert.AreEqual("document", res[0].Matches[0].Text, "Wrong match text");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location");

            Assert.AreEqual("content", res[0].Matches[1].Text, "Wrong match text");
            Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(3, res[0].Matches[1].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Content, res[0].Matches[1].Location, "Wrong match location");
        }
        public void InitializeData()
        {
            IInMemoryIndex sut = (IInMemoryIndex)GetIndex();

            IDocument d = MockDocument("doc", "Document", "doc", DateTime.Now);

            DumpedDocument[] documents = new DumpedDocument[] { new DumpedDocument(d) };

            DumpedWord[] words = new DumpedWord[] {
                new DumpedWord(new Word(1, "document")),
                new DumpedWord(new Word(2, "this")),
                new DumpedWord(new Word(3, "is")),
                new DumpedWord(new Word(4, "some")),
                new DumpedWord(new Word(5, "content"))
            };

            DumpedWordMapping[] mappings = new DumpedWordMapping[] {
                new DumpedWordMapping(words[0].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Title)),
                new DumpedWordMapping(words[1].ID, documents[0].ID, new BasicWordInfo(0, 0, WordLocation.Content)),
                new DumpedWordMapping(words[2].ID, documents[0].ID, new BasicWordInfo(5, 1, WordLocation.Content)),
                new DumpedWordMapping(words[3].ID, documents[0].ID, new BasicWordInfo(8, 2, WordLocation.Content)),
                new DumpedWordMapping(words[4].ID, documents[0].ID, new BasicWordInfo(13, 3, WordLocation.Content))
            };

            sut.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(d); });

            sut.InitializeData(documents, words, mappings);

            Assert.Equal(1, sut.TotalDocuments);
            Assert.Equal(5, sut.TotalWords);
            Assert.Equal(5, sut.TotalOccurrences);

            SearchResultCollection res = sut.Search(new SearchParameters("document content"));

            Assert.Single(res);
            Assert.Equal(2, res[0].Matches.Count);

            Assert.Equal("document", res[0].Matches[0].Text);
            Assert.Equal(0, res[0].Matches[0].FirstCharIndex);
            Assert.Equal(0, res[0].Matches[0].WordIndex);
            Assert.Equal(WordLocation.Title, res[0].Matches[0].Location);

            Assert.Equal("content", res[0].Matches[1].Text);
            Assert.Equal(13, res[0].Matches[1].FirstCharIndex);
            Assert.Equal(3, res[0].Matches[1].WordIndex);
            Assert.Equal(WordLocation.Content, res[0].Matches[1].Location);
        }
Beispiel #4
0
        public void LoadIndexAndModifications()
        {
            IInMemoryIndex index = MockInMemoryIndex();
            IDocument      doc1  = MockDocument("doc1", "Document", "doc", DateTime.Now);
            IDocument      doc2  = MockDocument2("doc2", "Article", "doc", DateTime.Now);

            BuildDocument buildDoc = new BuildDocument(delegate(DumpedDocument d) {
                return(d.Name == doc1.Name ? doc1 : doc2);
            });

            index.SetBuildDocumentDelegate(buildDoc);

            IndexStorer storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index);

            storer.LoadIndex();

            index.StoreDocument(doc1, null, "", null);
            index.StoreDocument(doc2, null, "", null);

            Assert.AreEqual(2, index.TotalDocuments, "Wrong document count");
            Assert.AreEqual(12, index.TotalWords, "Wrong word count");
            Assert.AreEqual(12, index.TotalOccurrences, "Wrong occurrence count");

            storer.Dispose();
            storer = null;

            index = MockInMemoryIndex();
            index.SetBuildDocumentDelegate(buildDoc);

            storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index);
            storer.LoadIndex();

            Assert.AreEqual(2, index.TotalDocuments, "Wrong document count");
            Assert.AreEqual(12, index.TotalWords, "Wrong word count");
            Assert.AreEqual(12, index.TotalOccurrences, "Wrong occurrence count");

            SearchResultCollection res = index.Search(new SearchParameters("document content article"));

            Assert.AreEqual(2, res.Count, "Wrong result count");
            Assert.AreEqual(2, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(1, res[1].Matches.Count, "Wrong matches count");

            Assert.AreEqual("document", res[0].Matches[0].Text, "Wrong match text");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location");

            Assert.AreEqual("content", res[0].Matches[1].Text, "Wrong match text");
            Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(3, res[0].Matches[1].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Content, res[0].Matches[1].Location, "Wrong match location");

            Assert.AreEqual("article", res[1].Matches[0].Text, "Wrong match text");
            Assert.AreEqual(0, res[1].Matches[0].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(0, res[1].Matches[0].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Title, res[1].Matches[0].Location, "Wrong match location");

            index.RemoveDocument(doc1, null);

            storer.Dispose();
            storer = null;

            index = MockInMemoryIndex();
            index.SetBuildDocumentDelegate(buildDoc);

            storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index);
            storer.LoadIndex();

            Assert.AreEqual(1, index.TotalDocuments, "Wrong document count");
            Assert.AreEqual(7, index.TotalWords, "Wrong word count");
            Assert.AreEqual(7, index.TotalOccurrences, "Wrong occurrence count");

            res = index.Search(new SearchParameters("document content article"));
            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");

            Assert.AreEqual("article", res[0].Matches[0].Text, "Wrong match text");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong match first char index");
            Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong match word index");
            Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong match location");

            index.Clear(null);

            storer.Dispose();
            storer = null;

            index = MockInMemoryIndex();
            index.SetBuildDocumentDelegate(buildDoc);

            storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index);
            storer.LoadIndex();

            Assert.AreEqual(0, index.TotalDocuments, "Wrong document count");
            Assert.AreEqual(0, index.TotalWords, "Wrong word count");
            Assert.AreEqual(0, index.TotalOccurrences, "Wrong occurrence count");
            Assert.AreEqual(0, index.Search(new SearchParameters("document")).Count, "Wrong result count");
        }