public void Does_Not_Return_Entities_Not_In_Search_Results()
        {
            // Setup
            JobSearch search = new JobSearch();
            Company   company = new Company {
                JobSearch = search
            }, company2 = new Company {
                JobSearch = search
            };
            Contact contact = new Contact {
                Company = company
            }, contact2 = new Contact {
                Company = company
            };
            Task task = new Task {
                Company = company
            }, task2 = new Task {
                Company = company
            };

            _unitOfWork.Companies.Add(company);
            _unitOfWork.Companies.Add(company2);
            _unitOfWork.Contacts.Add(contact);
            _unitOfWork.Contacts.Add(contact2);
            _unitOfWork.Tasks.Add(task);
            _unitOfWork.Tasks.Add(task2);
            _unitOfWork.Commit();

            // Result for ISearchProvider.Search() to return
            SearchProviderResult searchResult = new SearchProviderResult();

            searchResult.FoundCompanyIds.Add(company.Id);
            searchResult.FoundContactIds.Add(contact.Id);
            searchResult.FoundTaskIds.Add(task.Id);

            var mock = new Mock <ISearchProvider>();

            mock.Setup(x => x.SearchByJobSearchId("Query", search.Id)).Returns(searchResult);

            // Act
            SearchResultEntities result = new EntitySearchQuery(_unitOfWork, mock.Object).WithSearchQuery("Query").WithJobSearchId(search.Id).Execute();

            // Verify
            Assert.IsNotNull(result, "Search results returned a null result");
            Assert.IsFalse(result.Companies.Contains(company2), "Search results incorrectly contained the 2nd company");
            Assert.IsFalse(result.Contacts.Contains(contact2), "Search results incorrectly contained the 2nd contact");
            Assert.IsFalse(result.Tasks.Contains(task2), "Search results incorrectly contained the 2nd task");
        }
        public void Can_Find_Result_Entities_From_Database()
        {
            // Setup
            JobSearch search = new JobSearch();
            Company   company1 = new Company {
                JobSearch = search
            }, company2 = new Company {
                JobSearch = search
            };
            Contact contact = new Contact {
                Company = company1
            };
            Task task = new Task {
                Company = company1
            };

            _unitOfWork.Companies.Add(company1);
            _unitOfWork.Companies.Add(company2);
            _unitOfWork.Contacts.Add(contact);
            _unitOfWork.Tasks.Add(task);
            _unitOfWork.Commit();

            // Result for ISearchProvider.Search() to return
            SearchProviderResult searchResult = new SearchProviderResult();

            searchResult.FoundCompanyIds.Add(company1.Id);
            searchResult.FoundCompanyIds.Add(company2.Id);
            searchResult.FoundContactIds.Add(contact.Id);
            searchResult.FoundTaskIds.Add(task.Id);

            var mock = new Mock <ISearchProvider>();

            mock.Setup(x => x.SearchByJobSearchId("Query", search.Id)).Returns(searchResult);

            // Act
            SearchResultEntities result = new EntitySearchQuery(_unitOfWork, mock.Object).WithSearchQuery("Query").WithJobSearchId(search.Id).Execute();

            // Verify
            Assert.IsNotNull(result, "Search provider returned a null result");
            Assert.AreEqual(2, result.Companies.Count, "Incorrect number of companies was returned");
            Assert.AreEqual(1, result.Contacts.Count, "Incorrect number of contacts was returned");
            Assert.AreEqual(1, result.Tasks.Count, "incorrect number of tasks was returned");
            Assert.IsTrue(result.Companies.Contains(company1), "Returned company list did not contain the first company");
            Assert.IsTrue(result.Companies.Contains(company2), "Returned company list did not conain the second company");
            Assert.AreEqual(contact, result.Contacts[0], "Returned contact was incorrect");
            Assert.AreEqual(task, result.Tasks[0], "Returned task was incorrect");
        }
Example #3
0
        public void Search_Returns_No_Results_When_JobSearchId_Doesnt_Match()
        {
            // Setup
            Company company = new Company {
                Id = 2, Name = "Test", JobSearch = new JobSearch {
                    Id = 5
                }
            };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Test", 6);

            // Verify
            Assert.IsNotNull(result, "Search provider result was null");
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Search provider result's found company list item count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Search provider result's found contact list item count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Search provider result's found task list item count was incorrect");
        }
Example #4
0
        public void Search_Doesnt_Find_Entity_When_All_Words_Not_Matched()
        {
            // Setup
            Company company = new Company {
                Id = 2, Name = "My Name", JobSearch = new JobSearch {
                    Id = 4
                }
            };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrew Name", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
Example #5
0
        public void Search_Finds_Company_By_Notes()
        {
            // Setup
            Company company = new Company {
                Id = 2, Notes = "This is my note", JobSearch = new JobSearch {
                    Id = 4
                }
            };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("note", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
Example #6
0
        public void Search_Allows_For_Fuzzy_Searching()
        {
            // Setup
            Company company = new Company {
                Id = 2, Name = "My Name is Andrew", JobSearch = new JobSearch {
                    Id = 4
                }
            };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrw Nme", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
Example #7
0
        public void Search_Finds_Entity_With_Multiple_Search_Words_In_Separate_Fields()
        {
            // Setup
            Company company = new Company {
                Id = 2, Name = "My Name", Notes = "is Andrew", JobSearch = new JobSearch {
                    Id = 4
                }
            };

            _provider.Index(company);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Andrew Name", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(company.Id, result.FoundCompanyIds[0], "Found company id was incorrect");

            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
Example #8
0
        public void Search_Finds_Task_By_Name()
        {
            // Setup
            Task task = new Task {
                Id = 2, Name = "Name", Company = new Company {
                    JobSearch = new JobSearch {
                        Id = 4
                    }
                }
            };

            _provider.Index(task);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Name", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(1, result.FoundTaskIds.Count, "Found task count was incorrect");
            Assert.AreEqual(task.Id, result.FoundTaskIds[0], "Found task id value was incorrect");
        }
Example #9
0
        public void Search_Finds_Contact_By_MobilePhone()
        {
            // Setup
            Contact contact = new Contact {
                Id = 2, MobilePhone = "test", Company = new Company {
                    JobSearch = new JobSearch {
                        Id = 4
                    }
                }
            };

            _provider.Index(contact);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("test", 4);

            // Verify
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Found company count was incorrect");
            Assert.AreEqual(1, result.FoundContactIds.Count, "Found contact count was incorrect");
            Assert.AreEqual(contact.Id, result.FoundContactIds[0], "Found contact id value was incorrect");

            Assert.AreEqual(0, result.FoundTaskIds.Count, "Found task count was incorrect");
        }
Example #10
0
        public void Search_Returns_No_Results_With_No_Match()
        {
            // Setup
            Company company = new Company {
                Id = 2, JobSearch = new JobSearch {
                    Id = 5
                }
            };
            Contact contact = new Contact {
                Id = 3, Company = new Company {
                    JobSearch = new JobSearch {
                        Id = 5
                    }
                }
            };
            Task task = new Task {
                Id = 4, Company = new Company {
                    JobSearch = new JobSearch {
                        Id = 5
                    }
                }
            };

            _provider.Index(company);
            _provider.Index(contact);
            _provider.Index(task);

            // Act
            SearchProviderResult result = _provider.SearchByJobSearchId("Test", 5);

            // Verify
            Assert.IsNotNull(result, "Search provider result was null");
            Assert.AreEqual(0, result.FoundCompanyIds.Count, "Search provider result's found company list item count was incorrect");
            Assert.AreEqual(0, result.FoundContactIds.Count, "Search provider result's found contact list item count was incorrect");
            Assert.AreEqual(0, result.FoundTaskIds.Count, "Search provider result's found task list item count was incorrect");
        }
Example #11
0
        public SearchProviderResult SearchByJobSearchId(string searchString, int jobSearchId)
        {
            const int MAX_RESULTS = 10000;

            //const float MIN_SIMILARITY = 0.5f;
            //const int PREFIX_LENGTH = 3;

            if (string.IsNullOrWhiteSpace(searchString))
            {
                throw new ArgumentException("Provided search string is empty");
            }

            // Setup the fields to search through
            string[] searchfields = new string[]
            {
                Constants.COMPANY_CITY, Constants.COMPANY_INDUSTRY, Constants.COMPANY_METRO, Constants.COMPANY_NAME, Constants.COMPANY_NOTES,
                Constants.COMPANY_PHONE, Constants.COMPANY_STATE, Constants.COMPANY_ZIP, Constants.CONTACT_ASSISTANT, Constants.CONTACT_DIRECTPHONE,
                Constants.CONTACT_EMAIL, Constants.CONTACT_EXTENSION, Constants.CONTACT_MOBILEPHONE, Constants.CONTACT_NAME, Constants.CONTACT_NOTES,
                Constants.CONTACT_REFERREDBY, Constants.TASK_NAME, Constants.CONTACT_TITLE
            };

            // Split the search into seperate queries per word, and combine them into one major query
            var finalQuery = new BooleanQuery();
            var parser     = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, searchfields, CreateAnalyzer());

            string[] terms = searchString.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string term in terms)
            {
                finalQuery.Add(parser.Parse(term.Replace("~", "") + "~"), BooleanClause.Occur.MUST);
            }

            // Add an additional query that the document must have a matching job search id value
            finalQuery.Add(new TermQuery(new Term(Constants.JOBSEARCH_ID, jobSearchId.ToString())), BooleanClause.Occur.MUST);

            // Perform the search
            var directory = FSDirectory.Open(new DirectoryInfo(LuceneIndexBaseDirectory));
            var searcher  = new IndexSearcher(directory, true);
            var hits      = searcher.Search(finalQuery, MAX_RESULTS);

            // Go through found documents and add them to the result
            var result = new SearchProviderResult();

            foreach (var scoreDoc in hits.scoreDocs)
            {
                string id;

                // Retrieve the document, and determine if it's a company, contact, or task and add it to the correct list
                var doc = searcher.Doc(scoreDoc.doc);

                if ((id = doc.Get(Constants.COMPANY_ID)) != null)
                {
                    result.FoundCompanyIds.Add(Convert.ToInt32(id));
                }

                else if ((id = doc.Get(Constants.CONTACT_ID)) != null)
                {
                    result.FoundContactIds.Add(Convert.ToInt32(id));
                }

                else if ((id = doc.Get(Constants.TASK_ID)) != null)
                {
                    result.FoundTaskIds.Add(Convert.ToInt32(id));
                }
            }

            return(result);
        }