private List<Resource> GetLearningRegistryResources(bool search, NLRSearchParameters nlrSearchParameters) { //call ElasticSearch web service var elasticSearchProxy = new EsQueryProxy(); string theQuery = BuildQuery(nlrSearchParameters); EsQueryResult result = elasticSearchProxy.EsQuery(theQuery); StatusResponse statusResponse = result.Status; bool isSuccess = statusResponse.IsSuccess(); if (isSuccess && result.QueryResults != null && result.QueryResults.Count >= NationalLearningRegistryMaxResults && search) { TelerikWindowManager.RadAlert( "Search Request exceeds " + NationalLearningRegistryMaxResults.ToString() + " items. The first " + NationalLearningRegistryMaxResults.ToString() + " have been returned.", 400, 100, NationalLearningRegistry + " Response", ""); } return isSuccess ? BuildLearningRegistryResultList(result) : new List<Resource>(); }
private string BuildQuery(NLRSearchParameters nlrSearchParameters) { if (nlrSearchParameters == null) return null; if (string.IsNullOrEmpty(nlrSearchParameters.titleAndDescriptionSearchText) || nlrSearchParameters.titleAndDescriptionSearchText == "*") { nlrSearchParameters.titleAndDescriptionSearchText = string.Empty; } StringBuilder sb = new StringBuilder(); sb.Append("{\"query\": {"); sb.Append("\"bool\" : {"); sb.Append("\"must\" : [ "); if (!string.IsNullOrEmpty(nlrSearchParameters.titleAndDescriptionSearchText)) { sb.Append("{"); sb.Append("\"multi_match\": {"); sb.Append("\"query\": \"").Append(nlrSearchParameters.titleAndDescriptionSearchText).Append("\","); sb.Append("\"fields\":"); sb.Append("["); //Multi-match query on "title", "description" and "keywords" fields //sb.Append("\"title\", \"description\", \"keywords\""); sb.Append("\"title\", \"description\""); sb.Append("],"); sb.Append("\"operator\": \"or\""); sb.Append("}"); sb.Append("},"); } if (!string.IsNullOrEmpty(nlrSearchParameters.creator)) { //Multi-match query on both "creator" and "author" fields sb.Append("{\"multi_match\": {\"query\": \"") .Append(nlrSearchParameters.creator) .Append("\",\"fields\":[ \"creator\", \"author\"],\"operator\": \"or\"}},"); } if (!string.IsNullOrEmpty(nlrSearchParameters.publisher)) { sb.Append("{ \"match\": { \"publisher\" : \"").Append(nlrSearchParameters.publisher).Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.inLanguage)) { string theLanguage = MapLanguage(nlrSearchParameters.inLanguage); sb.Append("{ \"match\": { \"language\" : \"").Append(theLanguage).Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.keywords)) { //This is an "_all" query so it will try and match the search term in "all" fields sb.Append("{ \"match\": { \"_all\" : \"").Append(nlrSearchParameters.keywords).Append("\" } },"); } if (nlrSearchParameters.educationalUse.Count > 0) { string educationalUses = string.Join(" ", nlrSearchParameters.educationalUse.ToArray()); sb.Append("{ \"match\": { \"educationalUses\" : \"") .Append(educationalUses.ToLower()) .Append("\" } },"); } //if (!string.IsNullOrEmpty(nlrSearchParameters.author)) //{ // sb.Append("{ \"match\": { \"author\" : \"").Append(nlrSearchParameters.author).Append("\" } },"); //} if (nlrSearchParameters.interactivityType.Count > 0) { string interactivityTypes = string.Join(" ", nlrSearchParameters.interactivityType.ToArray()); sb.Append("{ \"match\": { \"interactivityType\" : \"") .Append(interactivityTypes.ToLower()) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.accessibilityFeature)) { sb.Append("{ \"match\": { \"accessibilityFeature\" : \"") .Append(nlrSearchParameters.accessibilityFeature) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.accessibilityHazard)) { sb.Append("{ \"match\": { \"accessibilityHazard\" : \"") .Append(nlrSearchParameters.accessibilityHazard) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.bookFormat)) { sb.Append("{ \"match\": { \"bookFormat\" : \"").Append(nlrSearchParameters.bookFormat).Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.copyrightHolder)) { sb.Append("{ \"match\": { \"copyrightHolder\" : \"") .Append(nlrSearchParameters.copyrightHolder) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.learningResourceType)) { sb.Append("{ \"match\": { \"learningResourceType\" : \"") .Append(nlrSearchParameters.learningResourceType) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.accessibilityControl)) { sb.Append("{ \"match\": { \"accessibilityControl\" : \"") .Append(nlrSearchParameters.accessibilityControl) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.typicalAgeRange)) { sb.Append("{ \"match\": { \"typicalAgeRange\" : \"") .Append(nlrSearchParameters.typicalAgeRange) .Append("\" } },"); } if (nlrSearchParameters.educationalRole.Count > 0) { string educationalRoles = string.Join(" ", nlrSearchParameters.educationalRole.ToArray()); sb.Append("{ \"match\": { \"audience\" : \"") .Append(educationalRoles.ToLower()) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.usageRightsURL)) { if (nlrSearchParameters.usageRightsURL.Equals("Custom", StringComparison.OrdinalIgnoreCase)) { sb.Append("{\"wildcard\": { \"usageRightsURL\": { \"value\": \"*\" }}},"); } else { sb.Append("{ \"match\": { \"usageRightsURL\" : \"") .Append(nlrSearchParameters.usageRightsURL) .Append("\" } },"); } } if (nlrSearchParameters.educationalRole.Count > 0) { string educationalRoles = string.Join(" ", nlrSearchParameters.educationalRole.ToArray()); sb.Append("{ \"match\": { \"audience\" : \"") .Append(educationalRoles.ToLower()) .Append("\" } },"); } if (!string.IsNullOrEmpty(nlrSearchParameters.usageRightsURL)) { if (nlrSearchParameters.usageRightsURL.Equals("Custom", StringComparison.OrdinalIgnoreCase)) { sb.Append("{\"wildcard\": { \"usageRightsURL\": { \"value\": \"*\" }}},"); } else { sb.Append("{ \"match\": { \"usageRightsURL\" : \"") .Append(nlrSearchParameters.usageRightsURL) .Append("\" } },"); } } //remove trailing comma... var index = sb.ToString().LastIndexOf(','); if (index >= 0) sb.Remove(index, 1); ////////////////////////// sb.Append("]"); sb.Append("}},\"size\": ").Append(NationalLearningRegistryMaxResults.ToString()); sb.Append("}"); return sb.ToString(); }
private NLRSearchParameters getNLRSearchParameters(string criteriaSelectionParameters, TagCriteriaSelectionParameters tagCriteriaSelectionParameters) { NLRSearchParameters theNLRSearchParameters = new NLRSearchParameters(); theNLRSearchParameters.creator = tagCriteriaSelectionParameters.Creator; theNLRSearchParameters.publisher = tagCriteriaSelectionParameters.Publisher; //theNLRSearchParameters.usageRightsURL = tagCriteriaSelectionParameters.UseRightUrl; //theNLRSearchParameters.author = tagCriteriaSelectionParameters.Author; theNLRSearchParameters.created = tagCriteriaSelectionParameters.DateCreated.ToString(); //theNLRSearchParameters.accessibilityControl //theNLRSearchParameters.accessibilityFeature //theNLRSearchParameters.accessibilityHazard //theNLRSearchParameters.bookFormat //theNLRSearchParameters.copyrightHolder theNLRSearchParameters.title = theNLRSearchParameters.description = theNLRSearchParameters.titleAndDescriptionSearchText = criteriaSelectionParameters; if (tagCriteriaSelectionParameters.Language > -1) { theNLRSearchParameters.inLanguage = Enum.Parse(typeof(LanguageType), tagCriteriaSelectionParameters.Language.ToString()).ToString(); } theNLRSearchParameters.typicalAgeRange = tagCriteriaSelectionParameters.Ages; theNLRSearchParameters.interactivityType = tagCriteriaSelectionParameters.InteractivityTypes; theNLRSearchParameters.educationalUse = tagCriteriaSelectionParameters.EducationalUses; theNLRSearchParameters.educationalRole = tagCriteriaSelectionParameters.EndUsers; theNLRSearchParameters.usageRightsURL = tagCriteriaSelectionParameters.UseRightUrlTxt; theNLRSearchParameters.keywords = tagCriteriaSelectionParameters.Keywords; theNLRSearchParameters.educationalRole = tagCriteriaSelectionParameters.EndUsers; theNLRSearchParameters.usageRightsURL = tagCriteriaSelectionParameters.UseRightUrlTxt; return theNLRSearchParameters; }