Beispiel #1
0
        internal static IList <ScoringParameter> ToQuery(this IEnumerable <string> parameters, IList <ScoringParameter> query, IEnumerable <PropertyMapping> propertyMappins)
        {
            if (!parameters.Any())
            {
                return(query);
            }

            var newQuery = query.ToList();

            foreach (var parameter in parameters)
            {
                var fi = propertyMappins.FirstOrDefault(pm => ("@" + pm.Property).Equals(parameter));

                if (fi == null)
                {
                    throw new Exception($"No mapping defined for parameter {parameter}.");
                }

                for (var i = 0; i < newQuery.Count; i++)
                {
                    newQuery[i] = new ScoringParameter(newQuery[i].Name.Replace(parameter, fi.IndexField.ToString().ToCamelCase()), newQuery[i].Values);
                }
            }

            return(newQuery);
        }
Beispiel #2
0
        public async Task <DocumentSearchResult> Search([FromUri] string searchText)
        {
            var credentials          = new SearchCredentials(AzureSearchConfig.SearchServiceApiKey);
            SearchIndexClient client = new SearchIndexClient(AzureSearchConfig.SearchServiceName
                                                             , AzureSearchConfig.SearchIndexName, credentials);

            var callersGeoLocation = LocationHelper.GetCallerLocation();
            var scoringParam       = new ScoringParameter(AzureSearchConfig.GeoScoringParameterName,
                                                          $"{callersGeoLocation.Longitude},{callersGeoLocation.Latitude}");

            var parameters = new SearchParameters
            {
                SearchMode        = SearchMode.All,
                Facets            = AzureSearchConfig.SearchIndexFacets,
                ScoringProfile    = AzureSearchConfig.ScoringProfileName,
                ScoringParameters = new[] { scoringParam }
            };

            return(await client.Documents.SearchAsync(searchText, parameters));
        }
        public async Task <IEnumerable <PersonModel> > Suspects([FromUri] string searchText)
        {
            var credentials          = new SearchCredentials(AzureSearchConfig.SearchServiceApiKey);
            SearchIndexClient client = new SearchIndexClient(AzureSearchConfig.SearchServiceName
                                                             , AzureSearchConfig.SearchIndexName, credentials);

            var callersGeoLocation = LocationHelper.GetCallerLocation();

            var scoringParam = new ScoringParameter(
                AzureSearchConfig.GeoScoringParameterName,
                new string[] { callersGeoLocation.Longitude.ToString(), callersGeoLocation.Latitude.ToString() });

            var parameters = new SearchParameters
            {
                SearchMode        = SearchMode.All,
                Facets            = AzureSearchConfig.SearchIndexFacets,
                ScoringProfile    = AzureSearchConfig.ScoringProfileName,
                ScoringParameters = new[] { scoringParam },
                Top    = 10,
                Select = new[] { "FirstName", "LastName", "EyeColor", "HairColor", "Sex", "SuspectSearchImage" }
            };

            DocumentSearchResult searchResult = await client.Documents.SearchAsync(searchText, parameters);

            var persons = searchResult.Results.Select(d =>
            {
                return(new PersonModel
                {
                    Name = d.Document["FirstName"].ToString() + d.Document["LastName"].ToString(),
                    EyeColor = d.Document["EyeColor"].ToString(),
                    HairColor = d.Document["HairColor"].ToString(),
                    Sex = d.Document["Sex"].ToString(),
                    SuspectSearchImage = d.Document["SuspectSearchImage"].ToString()
                });
            });

            return(persons);
        }
Beispiel #4
0
        private JobSearchResults SearchJobs(JobCriteria searchParms)
        {
            var credential = new SearchCredentials(AZURE_SEARCH_KEY);
            SearchServiceClient serviceClient = new SearchServiceClient(ACURE_SEARCH_SERVICE_NAME, credential);
            var indexClient = serviceClient.Indexes.GetClient("denvertechjobs");

            var sp = new SearchParameters
            {
                ScoringProfile          = (searchParms.Latituede != 0 && searchParms.Latituede != 0) ? "geo" : "newAndHighlyRated",
                IncludeTotalResultCount = true,
                SearchMode = SearchMode.All,
                //Filter = "trioType eq 'Food'"
            };

            if (searchParms.Latituede != 0 && searchParms.Latituede != 0)
            {
                var scoreParam = new ScoringParameter("currentLocation", Microsoft.Spatial.GeographyPoint.Create((double)searchParms.Latituede, (double)searchParms.Longitude));
                sp.ScoringParameters = new List <ScoringParameter> {
                    scoreParam
                };
            }

            var searchText = string.Join('|', searchParms.KeyPhrases) + "+" + string.Join('|', searchParms.Skills) + "+" + searchParms.Profession;

            DocumentSearchResult <JobDocument> response = indexClient.Documents.Search <JobDocument>(searchText, sp);

            var results = new JobSearchResults {
                TotalResults = response.Count
            };

            if (response.Results.Any())
            {
                var docs = (from d in response.Results select d.Document).ToList();
                results.ResultItems.AddRange(docs);
            }

            return(results);
        }
        public async Task <ActionResult> Index(SearchData model)
        {
            try
            {
                InitSearch();

                int page;

                if (model.paging != null && model.paging == "next")
                {
                    // Recover the facet text, and the facet check box settings.
                    RecoverFacets(model, true);

                    // Increment the page.
                    page = (int)TempData["page"] + 1;

                    // Recover the search text.
                    model.searchText = TempData["searchfor"].ToString();
                }
                else
                {
                    // First search with text.
                    // Recover the facet text, but ignore the check box settings, and use the current model settings.
                    RecoverFacets(model, false);

                    // First call. Check for valid text input, and valid scoring profile.
                    if (model.searchText == null)
                    {
                        model.searchText = "";
                    }
                    if (model.scoring == null)
                    {
                        model.scoring = "Default";
                    }
                    page = 0;
                }

                // Set empty defaults for ordering and scoring parameters.
                var    orderby       = new List <string>();
                string profile       = "";
                var    scoringParams = new List <ScoringParameter>();

                // Set the ordering based on the user's radio button selection.
                switch (model.scoring)
                {
                case "RatingRenovation":
                    orderby.Add("Rating desc");
                    orderby.Add("LastRenovationDate desc");
                    break;

                case "boostAmenities":
                {
                    profile = model.scoring;
                    var setAmenities = new List <string>();

                    // Create a string list of amenities that have been clicked.
                    for (int a = 0; a < model.facetOn.Length; a++)
                    {
                        if (model.facetOn[a])
                        {
                            setAmenities.Add(model.facetText[a]);
                        }
                    }
                    if (setAmenities.Count > 0)
                    {
                        // Only set scoring parameters if there are any.
                        var sp = new ScoringParameter("amenities", setAmenities);
                        scoringParams.Add(sp);
                    }
                    else
                    {
                        // No amenities selected, so set profile back to default.
                        profile = "";
                    }
                }
                break;

                case "renovatedAndHighlyRated":
                    profile = model.scoring;
                    break;

                default:
                    break;
                }

                // Setup the search parameters.
                var parameters = new SearchParameters
                {
                    // Set the ordering/scoring parameters.
                    OrderBy           = orderby,
                    ScoringProfile    = profile,
                    ScoringParameters = scoringParams,

                    // Select the data properties to be returned.
                    Select     = new[] { "HotelName", "Description", "Tags", "Rooms", "Rating", "LastRenovationDate" },
                    SearchMode = SearchMode.All,

                    // Skip past results that have already been returned.
                    Skip = page * GlobalVariables.ResultsPerPage,

                    // Take only the next page worth of results.
                    Top = GlobalVariables.ResultsPerPage,

                    // Include the total number of results.
                    IncludeTotalResultCount = true,
                };

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

                // Ensure TempData is stored for the next call.
                TempData["page"]      = page;
                TempData["searchfor"] = model.searchText;
                TempData["scoring"]   = model.scoring;
                SaveFacets(model, true);

                // Calculate the room rate ranges.
                for (int n = 0; n < model.resultList.Results.Count; n++)
                {
                    var cheapest  = 0d;
                    var expensive = 0d;

                    for (var r = 0; r < model.resultList.Results[n].Document.Rooms.Length; r++)
                    {
                        var rate = model.resultList.Results[n].Document.Rooms[r].BaseRate;
                        if (rate < cheapest || cheapest == 0)
                        {
                            cheapest = (double)rate;
                        }
                        if (rate > expensive)
                        {
                            expensive = (double)rate;
                        }
                    }
                    model.resultList.Results[n].Document.cheapest  = cheapest;
                    model.resultList.Results[n].Document.expensive = expensive;
                }
            }
            catch
            {
                return(View("Error", new ErrorViewModel {
                    RequestId = "1"
                }));
            }
            return(View("Index", model));
        }