Example #1
0
        public void Search_Basic_MultipleWords()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            // The mocked document has a default content
            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc1, null, "", null);

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

            Assert.Single(res);
            Assert.Equal(100F, res[0].Relevance.Value, 2);
            Assert.True(AreDocumentsEqual(doc1, res[0].Document));
            Assert.Equal(2, res[0].Matches.Count);
            Assert.Equal(0, res[0].Matches[0].FirstCharIndex);
            Assert.Equal(4, res[0].Matches[0].Text.Length);
            Assert.Equal(13, res[0].Matches[1].FirstCharIndex);
            Assert.Equal(7, res[0].Matches[1].Text.Length);
        }
Example #2
0
        public void Search_Basic_LocationRelevance_3()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);
            IDocument doc2 = MockDocument2("Doc2", "Text 2", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            index.StoreDocument(doc1, null, "", null);
            index.StoreDocument(doc2, new string[] { "blah" }, "", null);

            // "blah" is only present in the keywords of doc2
            // "document" is only present in the title of doc1
            SearchResultCollection res = index.Search(new SearchParameters("document blah"));

            Assert.AreEqual(2, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(1, res[1].Matches.Count, "Wrong matches count");
            foreach (SearchResult r in res)
            {
                if (r.Matches[0].Location == WordLocation.Keywords)
                {
                    Assert.AreEqual(42.8, r.Relevance.Value, 0.1, "Wrong relevance for content");
                }
                else if (r.Matches[0].Location == WordLocation.Title)
                {
                    Assert.AreEqual(57.1, r.Relevance.Value, 0.1, "Wrong relevance for keywords");
                }
            }
        }
Example #3
0
        public void Search_WithOptions_AtLeastOneWord()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc1, null, "", null);

            SearchResultCollection res = sut.Search(new SearchParameters("this content", SearchOptions.AllWords));

            Assert.Single(res);
            Assert.Equal(0, res[0].Matches[0].FirstCharIndex);
            Assert.Equal(4, res[0].Matches[0].Text.Length);
            Assert.Equal(13, res[0].Matches[1].FirstCharIndex);
            Assert.Equal(7, res[0].Matches[1].Text.Length);

            res = sut.Search(new SearchParameters("this stuff", SearchOptions.AtLeastOneWord));

            Assert.Single(res);
        }
Example #4
0
        public void Search_Basic_LocationRelevance_2()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);
            IDocument doc2 = MockDocument2("Doc2", "Text 2", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc1, null, "", null);
            sut.StoreDocument(doc2, new string[] { "blah" }, "", null);

            // "blah" is only present in the keywords of doc2
            // "content" is only present in the content of doc1
            SearchResultCollection res = sut.Search(new SearchParameters("content blah"));

            Assert.Equal(2, res.Count);
            Assert.Single(res[0].Matches);
            Assert.Single(res[1].Matches);
            foreach (SearchResult r in res)
            {
                if (r.Matches[0].Location == WordLocation.Content)
                {
                    Assert.Equal(40.0, r.Relevance.Value, 1);
                }
                else if (r.Matches[0].Location == WordLocation.Keywords)
                {
                    Assert.Equal(60.0, r.Relevance.Value, 1);
                }
            }
        }
Example #5
0
        public void Clear()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

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

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }
            sut.StoreDocument(doc, null, PlainTextDocumentContent, null);

            bool eventFired = false;

            if (imIndex != null)
            {
                imIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e)
                {
                    eventFired = true;
                };
            }

            sut.Clear(null);

            if (imIndex != null)
            {
                Assert.True(eventFired, "IndexChanged event not fired");
            }
            Assert.Equal(0, sut.TotalDocuments);
            Assert.Equal(0, sut.TotalWords);
            Assert.Equal(0, sut.TotalOccurrences);
            Assert.Empty(sut.Search(new SearchParameters("document")));
        }
Example #6
0
        public void Search_WithOptions_AtLeastOneWord()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

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

            SearchResultCollection res = index.Search(new SearchParameters("this content", SearchOptions.AllWords));

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong start index for 'this'");
            Assert.AreEqual(4, res[0].Matches[0].Text.Length, "Wrong length for 'index'");
            Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong start index for 'content'");
            Assert.AreEqual(7, res[0].Matches[1].Text.Length, "Wrong length for 'content'");

            res = index.Search(new SearchParameters("this stuff", SearchOptions.AtLeastOneWord));

            Assert.AreEqual(1, res.Count, "Wrong result count");
        }
Example #7
0
        public void Search_Basic_MultipleWords()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            // The mocked document has a default content
            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

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

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

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(100F, res[0].Relevance.Value, 0.01F, "Wrong relevance value");
            Assert.IsTrue(AreDocumentsEqual(doc1, res[0].Document), "Wrong document");
            Assert.AreEqual(2, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong start index for match at position 0");
            Assert.AreEqual(4, res[0].Matches[0].Text.Length, "Wrong length for match at position 0");
            Assert.AreEqual(13, res[0].Matches[1].FirstCharIndex, "Wrong start index for match at position 1");
            Assert.AreEqual(7, res[0].Matches[1].Text.Length, "Wrong length for match at position 1");
        }
Example #8
0
        public void ReplaceDocument()
        {
            // This test checks that IndexStorer properly discards the old document when a new copy is stored,
            // even when the title and date/time are different

            IInMemoryIndex index = MockInMemoryIndex();
            DateTime       dt1   = DateTime.Now.AddDays(-1);
            IDocument      doc1  = MockDocument("doc1", "Document", "doc", dt1);
            IDocument      doc2  = MockDocument2("doc1", "Article", "doc", DateTime.Now);

            index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.DateTime == dt1 ? doc1 : doc2); });

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

            storer.LoadIndex();

            index.StoreDocument(doc1, null, "", null);
            Assert.AreEqual(1, index.TotalDocuments, "Wrong document count");
            index.StoreDocument(doc2, null, "", null);
            Assert.AreEqual(1, index.TotalDocuments, "Wrong document count");

            storer.Dispose();
            storer = null;

            index = MockInMemoryIndex();
            index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.DateTime == dt1 ? doc1 : doc2); });

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

            Assert.AreEqual(1, index.TotalDocuments, "Wrong document count");
        }
Example #9
0
        public void Constructor()
        {
            IInMemoryIndex index  = MockInMemoryIndex();
            IndexStorer    storer = new IndexStorer(documentsFile, wordsFile, mappingsFile, index);

            Assert.AreEqual(index, storer.Index, "Wrong index");
        }
Example #10
0
        public void InitializeData_NullMappings()
        {
            IInMemoryIndex index = (IInMemoryIndex)GetIndex();

            index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); });
            index.InitializeData(new DumpedDocument[0], new DumpedWord[0], null);
        }
Example #11
0
        public void DataCorrupted()
        {
            IInMemoryIndex index = MockInMemoryIndex();
            IDocument      doc1  = MockDocument("doc1", "Document", "doc", DateTime.Now);
            IDocument      doc2  = MockDocument2("doc2", "Article", "doc", DateTime.Now);

            index.SetBuildDocumentDelegate(delegate(DumpedDocument d) { return(d.Name == doc1.Name ? doc1 : doc2); });

            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;

            File.Create(documentsFile).Close();

            index = MockInMemoryIndex();

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

            Assert.IsTrue(storer.DataCorrupted, "DataCorrupted should be true");
            Assert.AreEqual(0, index.TotalDocuments, "Wrong document count");
        }
Example #12
0
        public void Clear()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

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

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            index.StoreDocument(doc, null, PlainTextDocumentContent, null);

            bool eventFired = false;

            if (imIndex != null)
            {
                imIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e) {
                    eventFired = true;
                };
            }

            index.Clear(null);

            if (imIndex != null)
            {
                Assert.IsTrue(eventFired, "IndexChanged event not fired");
            }

            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");
        }
Example #13
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");
        }
Example #14
0
        public void InitializeData_NullMappings()
        {
            IInMemoryIndex index = (IInMemoryIndex)GetIndex();

            index.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); });
            Assert.That(() => index.InitializeData(new DumpedDocument[0], new DumpedWord[0], null), Throws.ArgumentNullException);
        }
        public void InitializeData_Document_NoBuildDelegate()
        {
            IInMemoryIndex sut = (IInMemoryIndex)GetIndex();
            var            ex  = Assert.Throws <InvalidOperationException>(() => sut.InitializeData(new DumpedDocument[0], new DumpedWord[0], new DumpedWordMapping[0]));

            Assert.Equal("InitializeData can be invoked only when the BuildDocument delegate is set.", ex.Message);
        }
Example #16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IndexStorerBase" /> class.
        /// </summary>
        /// <param name="index">The index to manage.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="index"/> is <c>null</c>.</exception>
        public IndexStorerBase(IInMemoryIndex index)
        {
            if(index == null) throw new ArgumentNullException("index");

            this.index = index;
            indexChangedHandler = new EventHandler<IndexChangedEventArgs>(IndexChangedHandler);
            this.index.IndexChanged += indexChangedHandler;
        }
        public void InitializeData_Mappings_Null()
        {
            IInMemoryIndex sut = (IInMemoryIndex)GetIndex();

            sut.SetBuildDocumentDelegate(delegate(DumpedDocument doc) { return(null); });
            var ex = Assert.Throws <ArgumentNullException>(() => sut.InitializeData(new DumpedDocument[0], new DumpedWord[0], null));

            Assert.Equal("Value cannot be null.\r\nParameter name: mappings", ex.Message);
        }
Example #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IndexStorerBase" /> class.
        /// </summary>
        /// <param name="index">The index to manage.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="index"/> is <c>null</c>.</exception>
        public IndexStorerBase(IInMemoryIndex index)
        {
            if (index == null)
            {
                throw new ArgumentNullException("index");
            }

            this.index               = index;
            indexChangedHandler      = new EventHandler <IndexChangedEventArgs>(IndexChangedHandler);
            this.index.IndexChanged += indexChangedHandler;
        }
Example #19
0
        public void Size()
        {
            IInMemoryIndex index = MockInMemoryIndex();

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

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

            storer.LoadIndex();

            Assert.IsTrue(storer.Size > 0, "Size should be greater than zero");
        }
Example #20
0
        public void Search_Basic()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            // The mocked document has a default content
            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);
            IDocument doc2 = MockDocument("Doc2", "Document 2", "ptdoc", DateTime.Now);
            IDocument doc3 = MockDocument("Doc3", "Document 3", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

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

            SearchResultCollection res1 = index.Search(new SearchParameters("specifications"));
            SearchResultCollection res2 = index.Search(new SearchParameters("this"));

            Assert.AreEqual(0, res1.Count, "Wrong result count");
            Assert.AreEqual(3, res2.Count, "Wrong result count");

            // Matches are in unpredictable order
            bool doc1Found = false, doc2Found = false, doc3Found = false;

            foreach (SearchResult r in res2)
            {
                if (AreDocumentsEqual(r.Document, doc1))
                {
                    doc1Found = true;
                }
                else if (AreDocumentsEqual(r.Document, doc2))
                {
                    doc2Found = true;
                }
                else if (AreDocumentsEqual(r.Document, doc3))
                {
                    doc3Found = true;
                }

                Assert.AreEqual(1, r.Matches.Count, "Wrong match count");
                Assert.AreEqual(0, r.Matches[0].FirstCharIndex, "Wrong start index");
                Assert.AreEqual(4, r.Matches[0].Text.Length, "Wrong length");
                Assert.AreEqual(33.3333F, r.Relevance.Value, 0.01F, "Wrong relevance value");
            }

            Assert.IsTrue(doc1Found, "Doc1 not found in results");
            Assert.IsTrue(doc2Found, "Doc2 not found in results");
            Assert.IsTrue(doc3Found, "Doc3 not found in results");
        }
Example #21
0
        public void Search_Filtered()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            // The mocked document has a default content
            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);
            IDocument doc2 = MockDocument("Doc2", "Document 2", "htmldoc", DateTime.Now);
            IDocument doc3 = MockDocument("Doc3", "Document 3", "odoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc1, null, "", null);
            sut.StoreDocument(doc2, null, "", null);
            sut.StoreDocument(doc3, null, "", null);

            SearchResultCollection res = sut.Search(new SearchParameters("this", "ptdoc", "htmldoc"));

            Assert.Equal(2, res.Count);

            // Matches are in unpredictable order
            bool doc1Found = false, doc2Found = false, doc3Found = false;

            foreach (SearchResult r in res)
            {
                if (AreDocumentsEqual(r.Document, doc1))
                {
                    doc1Found = true;
                }
                else if (AreDocumentsEqual(r.Document, doc2))
                {
                    doc2Found = true;
                }
                else if (AreDocumentsEqual(r.Document, doc3))
                {
                    doc3Found = true;
                }

                Assert.Single(r.Matches);
                Assert.Equal(0, r.Matches[0].FirstCharIndex);
                Assert.Equal(4, r.Matches[0].Text.Length);
                Assert.Equal(50F, r.Relevance.Value, 2);
            }

            Assert.True(doc1Found, "Doc1 not found in results");
            Assert.True(doc2Found, "Doc2 not found in results");
            Assert.False(doc3Found, "Doc3 found in results");
        }
Example #22
0
        public void RemoveDocument()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);
            IDocument doc2 = MockDocument("Doc2", "Document 2", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

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

            bool eventFired = false;

            if (imIndex != null)
            {
                imIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e)
                {
                    eventFired = e.Document == doc1 && e.Change == IndexChangeType.DocumentRemoved;
                    if (eventFired)
                    {
                        Assert.Equal("Doc1", e.ChangeData.Document.Name);
                        Assert.Single(e.ChangeData.Words);
                        Assert.Equal(6, e.ChangeData.Mappings.Count);
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 0 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 1 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 2 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 3 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 0 && m.Location == WordLocation.Title.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 1 && m.Location == WordLocation.Title.Location); }) != null, "Mappings does not contain a word");
                    }
                };
            }

            Assert.Equal(2, sut.TotalDocuments);

            sut.RemoveDocument(doc1, null);
            Assert.Equal(1, sut.TotalDocuments);

            IDocument doc3 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            sut.StoreDocument(doc3, null, "", null);
            Assert.Equal(2, sut.TotalDocuments);

            sut.RemoveDocument(doc1, null);
            Assert.Equal(1, sut.TotalDocuments);
        }
Example #23
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);
        }
Example #25
0
        // Documents file binary format
        // Reserved(8bytes) Count(int) Entries...
        //        ID(int) Name(string) Title(string) TypeTag(string) DateTime(long)
        // Words file binary format
        // Reserved(8bytes) Count(int) Entries...
        //        ID(int) Text(string)
        // Mappings file binary format
        // Reserved(8bytes) Count(int) Entries...
        //        WordID(int) DocumentID(int) FirstCharIndex(int) WordIndex(int) Location(int)
        /// <summary>
        /// Initializes a new instance of the <see cref="IndexStorer" /> class.
        /// </summary>
        /// <param name="documentsFile">The file that contains the documents list.</param>
        /// <param name="wordsFile">The file that contains the words list.</param>
        /// <param name="mappingsFile">The file that contains the index mappings data.</param>
        /// <param name="index">The index to manage.</param>
        public IndexStorer(string documentsFile, string wordsFile, string mappingsFile, IInMemoryIndex index)
            : base(index)
        {
            if(documentsFile == null) throw new ArgumentNullException("documentsFile");
            if(wordsFile == null) throw new ArgumentNullException("wordsFile");
            if(mappingsFile == null) throw new ArgumentNullException("mappingsFile");

            if(documentsFile.Length == 0) throw new ArgumentException("Documents File cannot be empty", "documentsFile");
            if(wordsFile.Length == 0) throw new ArgumentException("Words File cannot be emtpy", "wordsFile");
            if(mappingsFile.Length == 0) throw new ArgumentException("Mappings File cannot be empty", "mappingsFile");

            this.documentsFile = documentsFile;
            this.wordsFile = wordsFile;
            this.mappingsFile = mappingsFile;

            InitFiles();
        }
Example #26
0
        public void Search_WithOptions_ExactPhrase(string query, int results)
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

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

            SearchResultCollection res = index.Search(new SearchParameters(query, SearchOptions.ExactPhrase));

            Assert.AreEqual(results, res.Count, "Wrong result count");
        }
Example #27
0
        public void Search_WithOptions_AllWords(string query, int results)
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc1, null, "", null);

            SearchResultCollection res = sut.Search(new SearchParameters(query, SearchOptions.AllWords));

            Assert.Equal(results, res.Count);
        }
Example #28
0
        public void Search_Basic_Location()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc1 = MockDocument("Doc1", "Document 1", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            index.StoreDocument(doc1, new string[] { "development" }, "", null);

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

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(WordLocation.Title, res[0].Matches[0].Location, "Wrong location");

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

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(WordLocation.Content, res[0].Matches[0].Location, "Wrong location");

            res = index.Search(new SearchParameters("development"));

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(WordLocation.Keywords, res[0].Matches[0].Location, "Wrong location");

            IDocument doc2 = MockDocument2("Doc2", "Text 2", "ptdoc", DateTime.Now);

            index.StoreDocument(doc2, new string[] { "document" }, "", null);

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

            Assert.AreEqual(2, res.Count, "Wrong result count");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong matches count");
            Assert.AreEqual(1, res[1].Matches.Count, "Wrong matches count");
        }
Example #29
0
        public void Search_ExactPhrase()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc = MockDocument4("Doc", "Document", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            index.StoreDocument(doc, null, "", null);

            SearchResultCollection res = index.Search(new SearchParameters("content repeated content blah blah", SearchOptions.ExactPhrase));

            Assert.AreEqual(0, res.Count, "Wrong result count");

            res = index.Search(new SearchParameters("repeated content", SearchOptions.ExactPhrase));
            Assert.AreEqual(1, res.Count, "Wrong result count");
        }
Example #30
0
        public void Search_ExactPhrase()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

            IDocument doc = MockDocument4("Doc", "Document", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            sut.StoreDocument(doc, null, "", null);

            SearchResultCollection res = sut.Search(new SearchParameters("content repeated content blah blah", SearchOptions.ExactPhrase));

            Assert.Empty(res);

            res = sut.Search(new SearchParameters("repeated content", SearchOptions.ExactPhrase));
            Assert.Single(res);
        }
Example #31
0
        public void StoreDocument()
        {
            IIndex         sut     = GetIndex();
            IInMemoryIndex imIndex = sut as IInMemoryIndex;

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

            bool eventFired = false;

            if (imIndex != null)
            {
                imIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e)
                {
                    eventFired = e.Document == doc && e.Change == IndexChangeType.DocumentAdded;
                    if (eventFired)
                    {
                        Assert.Equal("Doc", e.ChangeData.Document.Name);
                        Assert.Equal(5, e.ChangeData.Words.Count);
                        Assert.Equal(5, e.ChangeData.Mappings.Count);
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 0 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 1 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 2 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 3 && m.Location == WordLocation.Content.Location); }) != null, "Mappings does not contain a word");
                        Assert.True(e.ChangeData.Mappings.Find(delegate(DumpedWordMapping m) { return(m.WordIndex == 0 && m.Location == WordLocation.Title.Location); }) != null, "Mappings does not contain a word");
                    }
                };

                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            Assert.Equal(5, sut.StoreDocument(doc, null, PlainTextDocumentContent, null));
            Assert.Equal(5, sut.TotalWords);
            Assert.Equal(5, sut.TotalOccurrences);
            Assert.Equal(1, sut.TotalDocuments);
            if (imIndex != null)
            {
                Assert.True(eventFired, "Event not fired");
            }
        }
Example #32
0
        public void Search_Basic_SingleResultWord()
        {
            IIndex         index   = GetIndex();
            IInMemoryIndex imIndex = index as IInMemoryIndex;

            IDocument doc = MockDocument3("Doc", "Document", "ptdoc", DateTime.Now);

            if (imIndex != null)
            {
                imIndex.IndexChanged += AutoHandlerForDocumentStorage;
            }

            index.StoreDocument(doc, null, "", null);

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

            Assert.AreEqual(1, res.Count, "Wrong result count");
            Assert.IsTrue(AreDocumentsEqual(doc, res[0].Document), "Wrong document");
            Assert.AreEqual(100, res[0].Relevance.Value, "Wrong relevance");
            Assert.AreEqual(1, res[0].Matches.Count, "Wrong match count");
            Assert.AreEqual(0, res[0].Matches[0].FirstCharIndex, "Wrong first char index");
            Assert.AreEqual(0, res[0].Matches[0].WordIndex, "Wrong word index");
            Assert.AreEqual("todo", res[0].Matches[0].Text, "Wrong word text");
        }
        /// <summary>
        /// Initializes the Provider.
        /// </summary>
        /// <param name="host">The Host of the Provider.</param>
        /// <param name="config">The Configuration data, if any.</param>
        /// <exception cref="ArgumentNullException">If <b>host</b> or <b>config</b> are <c>null</c>.</exception>
        /// <exception cref="InvalidConfigurationException">If <b>config</b> is not valid or is incorrect.</exception>
        public void Init(IHostV30 host, string config)
        {
            if(host == null) throw new ArgumentNullException("host");
            if(config == null) throw new ArgumentNullException("config");

            this.host = host;

            if(!LocalProvidersTools.CheckWritePermissions(host.GetSettingValue(SettingName.PublicDirectory))) {
                throw new InvalidConfigurationException("Cannot write into the public directory - check permissions");
            }

            if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), PagesDirectory))) {
                Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), PagesDirectory));
            }
            if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), MessagesDirectory))) {
                Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), MessagesDirectory));
            }
            if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), SnippetsDirectory))) {
                Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), SnippetsDirectory));
            }
            if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), ContentTemplatesDirectory))) {
                Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), ContentTemplatesDirectory));
            }
            if(!Directory.Exists(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), DraftsDirectory))) {
                Directory.CreateDirectory(Path.Combine(host.GetSettingValue(SettingName.PublicDirectory), DraftsDirectory));
            }

            bool upgradeNeeded = false;

            if(!File.Exists(GetFullPath(NamespacesFile))) {
                File.Create(GetFullPath(NamespacesFile)).Close();
            }

            upgradeNeeded = VerifyIfPagesFileNeedsAnUpgrade();

            if(!File.Exists(GetFullPath(PagesFile))) {
                File.Create(GetFullPath(PagesFile)).Close();
            }
            else if(upgradeNeeded) {
                VerifyAndPerformUpgradeForPages();
            }

            if(!File.Exists(GetFullPath(CategoriesFile))) {
                File.Create(GetFullPath(CategoriesFile)).Close();
            }
            else if(upgradeNeeded) {
                VerifyAndPerformUpgradeForCategories();
            }

            if(!File.Exists(GetFullPath(NavigationPathsFile))) {
                File.Create(GetFullPath(NavigationPathsFile)).Close();
            }
            else if(upgradeNeeded) {
                VerifyAndPerformUpgradeForNavigationPaths();
            }

            // Prepare search index
            index = new StandardIndex();
            index.SetBuildDocumentDelegate(BuildDocumentHandler);
            indexStorer = new IndexStorer(GetFullPath(IndexDocumentsFile),
                GetFullPath(IndexWordsFile),
                GetFullPath(IndexMappingsFile),
                index);
            indexStorer.LoadIndex();

            if(indexStorer.DataCorrupted) {
                host.LogEntry("Search Engine Index is corrupted and needs to be rebuilt\r\n" +
                    indexStorer.ReasonForDataCorruption.ToString(),	LogEntryType.Warning, null, this);
            }
        }