Пример #1
0
        private async Task <ActionResult> runQueryAsync2(SearchData model, int page)
        {
            // Use static variables to set up the configuration and Azure service and index clients, for efficiency.
            _builder       = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            _configuration = _builder.Build();

            _serviceClient = CreateSearchServiceClient(_configuration);
            _indexClient   = _serviceClient.Indexes.GetClient("hotels");

            SearchParameters             parameters;
            DocumentSearchResult <Hotel> results;

            parameters =
                new SearchParameters()
            {
                // Add this so that "King's Palace Hotel" does not match with all hotels!
                SearchMode = SearchMode.All,

                // Enter Hotel property names into this list so only these values will be returned.
                // If Select is empty, all values will be returned, which can be inefficient.
                Select = new[] { "HotelName", "Description", "Tags", "Rooms" }
            };

            // For efficiency, the search call should ideally be asynchronous, so we use the
            // SearchAsync call rather than the Search call.
            results = await _indexClient.Documents.SearchAsync <Hotel>(model.searchText, parameters);

            if (results.Results == null)
            {
                model.resultCount = 0;
            }
            else
            {
                // Record the total number of results.
                model.resultCount = (int)results.Results.Count;

                // Calcuate the range of current page results.
                int start = page * GlobalVariables.ResultsPerPage;
                int end   = Math.Min(model.resultCount, (page + 1) * GlobalVariables.ResultsPerPage);

                for (int i = start; i < end; i++)
                {
                    // Check for hotels with no room data provided.
                    if (results.Results[i].Document.Rooms.Length > 0)
                    {
                        // Add a hotel with sample room data (an example of a "complex type").
                        model.AddHotel(results.Results[i].Document.HotelName,
                                       results.Results[i].Document.Description,
                                       (double)results.Results[i].Document.Rooms[0].BaseRate,
                                       results.Results[i].Document.Rooms[0].BedOptions,
                                       results.Results[i].Document.Tags);
                    }
                    else
                    {
                        // Add a hotel with no sample room data.
                        model.AddHotel(results.Results[i].Document.HotelName,
                                       results.Results[i].Document.Description,
                                       0d,
                                       "No room data provided",
                                       results.Results[i].Document.Tags);
                    }
                }

                // Calcuate the page count.
                model.pageCount   = (model.resultCount + GlobalVariables.ResultsPerPage - 1) / GlobalVariables.ResultsPerPage;
                model.currentPage = page;
            }
            JsonResult jr = new JsonResult((object)model);

            return(jr);
        }