public List<GeographicalSearchResult> SearchPlacePostcodes(string searchText, 
            int polygonAreaTypeId, IList<int> parentAreaTypesToInclude)
        {
            var userSearch = new SearchUserInput(searchText);

            if (userSearch.IsQueryValid)
            {
                // Search polygon area type
                bool includePolygonAreaType = parentAreaTypesToInclude.Any() && parentAreaTypesToInclude.Contains(polygonAreaTypeId);
                var resultsToInclude = includePolygonAreaType
                    ? ResultsToInclude.PlaceNamesAndPostcodesAndParentAreas
                    : ResultsToInclude.PlaceNamesAndPostcodesOnly;
                var searchResults = GetSearchResults(userSearch, polygonAreaTypeId, resultsToInclude);

                // Extra parent areas
                var extraParentAreaTypesToSearchFor = parentAreaTypesToInclude.Where(x => x != polygonAreaTypeId);
                foreach (var parentAreaTypeId in extraParentAreaTypesToSearchFor)
                {
                    var parentSearchResults = GetSearchResults(userSearch, parentAreaTypeId,
                        ResultsToInclude.ParentAreasOnly);

                    searchResults.InsertRange(0, parentSearchResults);
                }

                return searchResults;
            }

            return new List<GeographicalSearchResult>();
        }
 private static void AssertOk(SearchUserInput searchUserInput, params string[] expected)
 {
     Assert.IsTrue(searchUserInput.IsQueryValid);
     Assert.AreEqual(expected.Length, searchUserInput.Terms.Count);
     foreach (var s in expected)
     {
         Assert.IsTrue(searchUserInput.Terms.Contains(s));
     }
 }
 protected static BooleanQuery GetTextQuery(SearchUserInput searchUserInput, params string[] fieldNames)
 {
     var masterQuery = new BooleanQuery();
     foreach (string queryTerm in searchUserInput.Terms)
     {
         foreach (string fieldName in fieldNames)
         {
             var query = new WildcardQuery(new Term(fieldName, queryTerm));
             masterQuery.Add(query, searchUserInput.BooleanCombinationLogic);
         }
     }
     return masterQuery;
 }
        protected static BooleanQuery GetPlaceNameQuery(SearchUserInput searchUserInput, int parentAreaTypeId)
        {
            var masterQuery = new BooleanQuery();
            foreach (string queryTerm in searchUserInput.Terms)
            {
                var query = new WildcardQuery(new Term(FieldNames.PlaceName, queryTerm));
                masterQuery.Add(query, searchUserInput.BooleanCombinationLogic);
            }

            var parentAreaTypeIdQuery = new WildcardQuery(new Term("Parent_Area_Code_" + parentAreaTypeId, "x"));
            masterQuery.Add(parentAreaTypeIdQuery, BooleanClause.Occur.MUST_NOT);

            return masterQuery;
        }
        public List<int> SearchIndicators(string searchText)
        {
            List<int> indicatorIds = new List<int>();

            SearchUserInput searchUserInput = new SearchUserInput(searchText);

            if (searchUserInput.IsQueryValid)
            {
                BooleanQuery masterQuery = GetTextQuery(searchUserInput, "IndicatorText");

                IndexSearcher searcher = new IndexSearcher(GetIndexDirectory("indicators"), true);
                TopDocs docs = searcher.Search(masterQuery, null, MaxResultCount);

                int resultCount = docs.ScoreDocs.Length;
                for (int i = 0; i < resultCount; i++)
                {
                    ScoreDoc scoreDoc = docs.ScoreDocs[i];
                    Document doc = searcher.Doc(scoreDoc.Doc);
                    indicatorIds.Add(Int32.Parse(doc.Get("id")));
                }
            }
            return indicatorIds;
        }
        private static void AssertQueryToTerms(string searchText, params string[] termsExpected)
        {
            SearchUserInput searchUserInput = new SearchUserInput(searchText);

            var terms = searchUserInput.Terms;
            Assert.AreEqual(terms.Count, termsExpected.Length);

            foreach (var expectedTerm in termsExpected)
            {
                Assert.IsTrue(terms.Contains(expectedTerm));
            }
        }
 private static void AssertNotValid(string searchText)
 {
     var userSearchInput = new SearchUserInput(searchText);
     Assert.IsFalse(userSearchInput.IsQueryValid);
 }
        private List<GeographicalSearchResult> GetSearchResults(SearchUserInput userSearch, int polygonAreaTypeId, ResultsToInclude resultsToInclude)
        {
            var isPostcode = userSearch.ContainsAnyNumbers;

            // Setup the fields to search through
            BooleanQuery finalQuery = isPostcode
                ? GetPostcodeQuery(userSearch.SearchText, FieldNames.Postcode)
                : GetPlaceNameQuery(userSearch, polygonAreaTypeId);

            // Perform the search
            var directory = FSDirectory.Open(new DirectoryInfo(
                Path.Combine(ApplicationConfiguration.SearchIndexDirectory, "placePostcodes")));
            var searcher = new IndexSearcher(directory, true);

            //Add the sorting parameters
            var sortBy = new Sort(
                new SortField(FieldNames.PlaceTypeWeighting, SortField.INT, true)
                );
            var docs = searcher.Search(finalQuery, null, ShortResultCount, sortBy);

            List<GeographicalSearchResult> searchResults = new List<GeographicalSearchResult>();
            int resultCount = docs.ScoreDocs.Length;
            for (int i = 0; i < resultCount; i++)
            {
                ScoreDoc scoreDoc = docs.ScoreDocs[i];

                Document doc = searcher.Doc(scoreDoc.Doc);

                var geographicalSearchResult = NewGeographicalSearchResult(doc, polygonAreaTypeId, isPostcode);

                CheckPolygonAreaCodeIsDefined(polygonAreaTypeId, geographicalSearchResult);

                AssignEastingAndNorthing(doc, geographicalSearchResult);

                AddResultIfRequired(resultsToInclude, geographicalSearchResult, searchResults);
            }
            return searchResults;
        }