public HttpResponseMessage Search(SearchPostedData postedData)
 {
     try
     {
         var result = SearchService.Search(postedData, this.globalConfiguration);
         return(this.Request.CreateResponse(HttpStatusCode.OK, result));
     }
     catch (Exception ex)
     {
         return(this.Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex)));
     }
 }
        public static SummarySearchResult Search(SearchPostedData searchRequest, GlobalConfiguration globalConfiguration)
        {
            if (index == null)
            {
                return(new SummarySearchResult());
            }

            SearchOptions searchOptions = new SearchOptions();

            // Turn on fuzzy search on
            searchOptions.UseCaseSensitiveSearch = false;

            var          searchQuery = searchRequest.GetQuery();
            SearchResult result;

            foreach (char specialChar in specialCharsList)
            {
                if (searchQuery.Contains(specialChar))
                {
                    foundSpecialChars.Add(specialChar);
                }
            }

            if (searchQuery.Contains(" "))
            {
                result = index.Search("\"" + searchQuery + "\"", searchOptions);
            }
            else if (foundSpecialChars.Count > 0)
            {
                foreach (char specialChar in foundSpecialChars)
                {
                    searchQuery = searchQuery.Replace(specialChar, ' ');
                }

                foundSpecialChars.Clear();
                result = index.Search("\"" + searchQuery + "\"", searchOptions);
            }
            else if (Path.HasExtension(searchQuery))
            {
                searchQuery = searchQuery.Replace(".", " ");
                result      = index.Search("\"" + searchQuery + "\"", searchOptions);
            }
            else
            {
                result = index.Search(searchQuery, searchOptions);
            }

            SummarySearchResult         summaryResult = new SummarySearchResult();
            List <SearchDocumentResult> foundFiles    = new List <SearchDocumentResult>();

            HighlightOptions options = new HighlightOptions
            {
                TermsBefore = 5,
                TermsAfter  = 5,
                TermsTotal  = 10,
            };

            for (int i = 0; i < result.DocumentCount; i++)
            {
                SearchDocumentResult searchDocumentResult = new SearchDocumentResult();

                FoundDocument           document    = result.GetFoundDocument(i);
                HtmlFragmentHighlighter highlighter = new HtmlFragmentHighlighter();
                index.Highlight(document, highlighter, options);
                FragmentContainer[] fragmentContainers = highlighter.GetResult();

                List <string> foundPhrases = new List <string>();
                for (int j = 0; j < fragmentContainers.Length; j++)
                {
                    FragmentContainer container = fragmentContainers[j];
                    string[]          fragments = container.GetFragments();
                    if (fragments.Length > 0)
                    {
                        for (int k = 0; k < fragments.Length; k++)
                        {
                            foundPhrases.Add(fragments[k].Replace("<br>", string.Empty));
                        }
                    }
                }

                searchDocumentResult.SetGuid(document.DocumentInfo.FilePath);
                searchDocumentResult.SetName(Path.GetFileName(document.DocumentInfo.FilePath));
                searchDocumentResult.SetSize(new FileInfo(document.DocumentInfo.FilePath).Length);
                searchDocumentResult.SetOccurrences(document.OccurrenceCount);
                searchDocumentResult.SetFoundPhrases(foundPhrases.ToArray());

                foundFiles.Add(searchDocumentResult);
            }

            summaryResult.SetFoundFiles(foundFiles.ToArray());
            summaryResult.SetTotalOccurences(result.OccurrenceCount);
            summaryResult.SetTotalFiles(result.DocumentCount);
            string searchDurationString = result.SearchDuration.ToString(@"ss\.ff");

            summaryResult.SetSearchDuration(searchDurationString.Equals("00.00") ? "< 1" : searchDurationString);
            summaryResult.SetIndexedFiles(Directory.GetFiles(globalConfiguration.GetSearchConfiguration().GetIndexedFilesDirectory(), "*", SearchOption.TopDirectoryOnly).Length);

            return(summaryResult);
        }