public SearchResult ToEySearchResult()
 {
     return(new SearchResult
     {
         DocId = docId,
         FileName = docFileName,
         FileUrl = docUrl + AzureStorageHelper.GenerateSasUrl(docFileName),
         Categories = Categories,
         LastUpdate = InsertDate,
         Score = Score,
         Highlights = Highlights.content.ToArray()
     });
 }
        public static SearchResult[] DoSearch(SearchRequest request)
        {
            var indexClient = GetSearchIndexClient();

            // Set up search parameters
            var sp = new SearchParameters();

            // Set up highlights
            var hlFields = new List <string> {
                "content"
            };

            sp.HighlightFields  = hlFields;
            sp.HighlightPreTag  = "<b>";
            sp.HighlightPostTag = "</b>";

            // Set mode to 'all' (must match all terms/clauses). Default is 'any'
            sp.SearchMode = SearchMode.All;

            // Use the lucerne query engine
            sp.QueryType = QueryType.Full;

            // Filter by docid if requested
            if (!string.IsNullOrEmpty(request.DocId))
            {
                sp.Filter = "docId eq '" + request.DocId + "'";
            }

            // Filter by clientid if requested
            if (!string.IsNullOrEmpty(request.ClientId))
            {
                // add the and clause if needed
                if (!string.IsNullOrEmpty(sp.Filter))
                {
                    sp.Filter += " and ";
                }
                sp.Filter = "clientId eq '" + request.ClientId + "'";
            }

            // Filter by category(s) if requested
            if (request.Categories != null)
            {
                for (int i = 0; i < request.Categories.Length; i++)
                {
                    // start the filter expression
                    if (i == 0)
                    {
                        // add the and clause if needed
                        if (!string.IsNullOrEmpty(sp.Filter))
                        {
                            sp.Filter += " and ";
                        }
                        sp.Filter += "(";
                    }

                    // Add category filter
                    sp.Filter += "category eq '" + request.Categories[i];

                    // end expression or add 'and'
                    if (i == request.Categories.Length - 1)
                    {
                        // end expression
                        sp.Filter += ")";
                    }
                    else
                    {
                        sp.Filter += " and ";
                    }
                }
            }

            // Do not return the content
            sp.Select = new List <string> {
                "docId", "docFileName", "docUrl", "categories", "insertDate"
            };

            // Perform the search
            DocumentSearchResult <Document> response = null;

            try
            {
                response = indexClient.Documents.Search <Document>(request.SearchTerm, sp);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error doing search. " + ex.Message);
            }

            var ret = new List <SearchResult>();

            if (response != null)
            {
                foreach (SearchResult <Document> result in response.Results)
                {
                    var sResult = new SearchResult
                    {
                        Score      = result.Score,
                        DocId      = (string)result.Document["docId"],
                        FileName   = (string)result.Document["docFileName"],
                        FileUrl    = (string)result.Document["docUrl"],
                        Categories = (IEnumerable <string>)result.Document["categories"],
                        LastUpdate = ((DateTimeOffset)result.Document["insertDate"]).DateTime
                    };
                    if (result.Highlights != null && result.Highlights.ContainsKey("content"))
                    {
                        sResult.Highlights = result.Highlights["content"].ToArray();
                    }

                    sResult.FileUrl += AzureStorageHelper.GenerateSasUrl(sResult.FileName);
                    ret.Add(sResult);
                }
            }

            return(ret.ToArray());
        }