From a9eb3b8ff76eec2ae3ec971ebab231259a4d50a9 Mon Sep 17 00:00:00 2001 From: Karthikeyan Rajendran <70887864+karthik-tarento@users.noreply.github.com> Date: Wed, 24 Aug 2022 16:08:18 +0530 Subject: [PATCH] 4.0.1 auto complete (#128) * Added user autoComplete API implementation (#125) --- .../common/util/CbExtServerProperties.java | 35 ++++++++++++- .../profile/controller/ProfileController.java | 6 +++ .../profile/service/ProfileService.java | 2 + .../profile/service/ProfileServiceImpl.java | 52 +++++++++++++++++++ .../service/AllocationService.java | 2 +- src/main/resources/application.properties | 5 +- 6 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java index 5fc7e181f..d4415a38d 100644 --- a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java +++ b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java @@ -321,7 +321,16 @@ public class CbExtServerProperties { @Value("${sb.org.create.path}") private String lmsOrgCreatePath; - @Value("${sb.service.user.migrate.path}") + @Value("${es.user.auto.complete.search.fields}") + private String esAutoCompleteSearchFields; + + @Value("${es.user.auto.complete.include.fields}") + private String esAutoCompleteIncludeFields; + + @Value("${es.profile.index.work.allocation}") + private String esProfileIndexWorkAllocation; + + @Value("${sb.service.user.migrate.path}") private String lmsUserMigratePath; @Value("${km.base.composite.search.fields}") @@ -1215,4 +1224,28 @@ public List getKmCompositeSearchPrimaryCategoryFilters() { public void setKmCompositeSearchPrimaryCategoryFilters(String kmCompositeSearchPrimaryCategoryFilters) { this.kmCompositeSearchPrimaryCategoryFilters = kmCompositeSearchPrimaryCategoryFilters; } + + public List getEsAutoCompleteSearchFields() { + return Arrays.asList(esAutoCompleteSearchFields.split(",", -1)); + } + + public void setEsAutoCompleteSearchFields(String esAutoCompleteSearchFields) { + this.esAutoCompleteSearchFields = esAutoCompleteSearchFields; + } + + public String[] getEsAutoCompleteIncludeFields() { + return esAutoCompleteIncludeFields.split(",", -1); + } + + public void setEsAutoCompleteIncludeFields(String esAutoCompleteIncludeFields) { + this.esAutoCompleteIncludeFields = esAutoCompleteIncludeFields; + } + + public String getEsProfileIndexWorkAllocation() { + return esProfileIndexWorkAllocation; + } + + public void setEsProfileIndexWorkAllocation(String esProfileIndexWorkAllocation) { + this.esProfileIndexWorkAllocation = esProfileIndexWorkAllocation; + } } \ No newline at end of file diff --git a/src/main/java/org/sunbird/profile/controller/ProfileController.java b/src/main/java/org/sunbird/profile/controller/ProfileController.java index edb2a037b..4c59b20df 100644 --- a/src/main/java/org/sunbird/profile/controller/ProfileController.java +++ b/src/main/java/org/sunbird/profile/controller/ProfileController.java @@ -47,6 +47,12 @@ public ResponseEntity parichayProfileUpdate(@RequestBody Map return new ResponseEntity<>(response, response.getResponseCode()); } + @GetMapping("/user/v1/autocomplete/{searchTerm}") + public ResponseEntity userAutoComplete(@PathVariable("searchTerm") String searchTerm) { + SBApiResponse response = profileService.userAutoComplete(searchTerm); + return new ResponseEntity<>(response, response.getResponseCode()); + } + @PostMapping("/user/v1/migrate") private ResponseEntity adminMigrateUser(@RequestHeader(Constants.X_AUTH_TOKEN) String userToken, @RequestHeader(Constants.AUTH_TOKEN) String authToken, @RequestBody Map request) { diff --git a/src/main/java/org/sunbird/profile/service/ProfileService.java b/src/main/java/org/sunbird/profile/service/ProfileService.java index 9662c504b..de52ddf96 100644 --- a/src/main/java/org/sunbird/profile/service/ProfileService.java +++ b/src/main/java/org/sunbird/profile/service/ProfileService.java @@ -14,5 +14,7 @@ public interface ProfileService { SBApiResponse userBasicProfileUpdate(Map request); + SBApiResponse userAutoComplete(String searchTerm); + SBApiResponse migrateUser(Map request, String userToken, String authToken); } diff --git a/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java b/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java index 616994867..a7e3ff991 100644 --- a/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java +++ b/src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java @@ -11,7 +11,12 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -488,6 +493,32 @@ public SBApiResponse userBasicInfo(String userId) { return response; } + @Override + public SBApiResponse userAutoComplete(String searchTerm) { + SBApiResponse response = new SBApiResponse(); + response.setResponseCode(HttpStatus.BAD_REQUEST); + response.getParams().setStatus(Constants.FAILED); + if (StringUtils.isEmpty(searchTerm)) { + response.getParams().setErrmsg("Invalid Search Term"); + return response; + } + + response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR); + Map resultResp = new HashMap<>(); + try { + List> userData = getUserSearchData(searchTerm); + resultResp.put(Constants.CONTENT, userData); + resultResp.put(Constants.COUNT, userData.size()); + response.setResponseCode(HttpStatus.OK); + response.getParams().setStatus(Constants.SUCCESS); + response.put(Constants.RESPONSE, resultResp); + } catch (Exception e) { + response.getParams() + .setErrmsg("Exception occurred while searching the user's from user registry" + e.getMessage()); + } + return response; + } + public SBApiResponse userBasicProfileUpdate(Map request) { SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_USER_BASIC_PROFILE_UPDATE); String errMsg = validateBasicProfilePayload(request); @@ -930,4 +961,25 @@ private String syncUserData(String userId) { } return errMsg; } + + public List> getUserSearchData(String searchTerm) throws Exception { + List> resultArray = new ArrayList<>(); + Map result; + final BoolQueryBuilder query = QueryBuilders.boolQuery(); + for(String field : serverConfig.getEsAutoCompleteSearchFields()) { + query.should(QueryBuilders.matchPhrasePrefixQuery(field, searchTerm)); + } + + final BoolQueryBuilder finalQuery = QueryBuilders.boolQuery(); + finalQuery.must(QueryBuilders.termQuery(Constants.STATUS, 1)).must(query); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(finalQuery); + sourceBuilder.fetchSource(serverConfig.getEsAutoCompleteIncludeFields(), new String[]{}); + SearchResponse searchResponse = indexerService.getEsResult(serverConfig.getEsProfileIndex(), + serverConfig.getEsProfileIndexType(), sourceBuilder); + for (SearchHit hit : searchResponse.getHits()) { + result = hit.getSourceAsMap(); + resultArray.add(result); + } + return resultArray; + } } diff --git a/src/main/java/org/sunbird/workallocation/service/AllocationService.java b/src/main/java/org/sunbird/workallocation/service/AllocationService.java index c82f11170..70f4eb19e 100644 --- a/src/main/java/org/sunbird/workallocation/service/AllocationService.java +++ b/src/main/java/org/sunbird/workallocation/service/AllocationService.java @@ -307,7 +307,7 @@ public List> getUserSearchData(String searchTerm) throws IOE .should(QueryBuilders.matchPhrasePrefixQuery("personalDetails.surname", searchTerm)); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query); sourceBuilder.fetchSource(includeFields, new String[] {}); - SearchResponse searchResponse = indexerService.getEsResult(configuration.getEsProfileIndex(), + SearchResponse searchResponse = indexerService.getEsResult(configuration.getEsProfileIndexWorkAllocation(), configuration.getEsProfileIndexType(), sourceBuilder); for (SearchHit hit : searchResponse.getHits()) { result = extractUserDetails(hit.getSourceAsMap()); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 12189d1f8..58d07d92c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -47,9 +47,12 @@ es.host=localhost es.port=9200 es.username= es.password= -es.profile.index=userprofile +es.profile.index=user_alias +es.profile.index.work.allocation=userprofile es.profile.index.type=_doc es.profile.source.fields=photo,id,employmentDetails,personalDetails +es.user.auto.complete.search.fields=profileDetails.personalDetails.primaryEmail,profileDetails.personalDetails.firstname,profileDetails.personalDetails.surname +es.user.auto.complete.include.fields=lastName,maskedPhone,rootOrgName,roles,channel,prevUsedPhone,updatedDate,stateValidated,isDeleted,organisations,managedBy,countryCode,flagsValue,id,recoveryEmail,identifier,updatedBy,phoneVerified,locationIds,recoveryPhone,rootOrgId,userId,userSubType,prevUsedEmail,emailVerified,firstName,profileLocation,createdDate,framework,tncAcceptedOn,allTncAccepted,profileDetails,createdBy,phone,profileUserType,dob,userType,tncAcceptedVersion,status #workallocation es config