예제 #1
0
        public async Task <IActionResult> Search(SearchRequest searchRequest)
        {
            var token = await BlobStorageClient.GetContainerSasUriAsync(_storageConfig);

            var selectFilter = _searchClient.Model.SelectFilter;
            var search       = searchRequest.Query;

            if (!string.IsNullOrEmpty(search))
            {
                search = search.Replace("-", "").Replace("?", "");
            }

            var response = await _searchClient.Search(search, searchRequest.SearchFacets, selectFilter, searchRequest.CurrentPage);

            var searchId = await _searchClient.GetSearchId();

            var facetResults = new List <object>();
            var tagsResults  = new List <object>();

            if (response.Facets != null)
            {
                // Return only the selected facets from the Search Model
                foreach (var facetResult in response.Facets.Where(f => _searchClient.Model.Facets.Where(x => x.Name == f.Key).Any()))
                {
                    facetResults.Add(new
                    {
                        key   = facetResult.Key,
                        value = facetResult.Value
                    });
                }

                foreach (var tagResult in response.Facets.Where(t => _searchClient.Model.Tags.Where(x => x.Name == t.Key).Any()))
                {
                    tagsResults.Add(new
                    {
                        key   = tagResult.Key,
                        value = tagResult.Value
                    });
                }
            }
            var telemetryDict = new Dictionary <string, string>
            {
                { "SearchServiceName", _searchConfig.ServiceName },
                { "SearchId", searchId },
                { "IndexName", _searchConfig.IndexName },
                { "QueryTerms", search },
                { "ResultCount", response.Count.ToString() }
            };

            _telemetryClient.TrackEvent("Search", telemetryDict);

            return(new JsonResult(new DocumentResult {
                Results = response.Results, Facets = facetResults, Tags = tagsResults, Count = Convert.ToInt32(response.Count), Token = token, SearchId = searchId
            }));
        }