Skip to content

Commit

Permalink
4.0.1 auto complete (#128)
Browse files Browse the repository at this point in the history
* Added user autoComplete API implementation  (#125)
  • Loading branch information
karthik-tarento authored Aug 24, 2022
1 parent 3906ee1 commit a9eb3b8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
35 changes: 34 additions & 1 deletion src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -1215,4 +1224,28 @@ public List<String> getKmCompositeSearchPrimaryCategoryFilters() {
public void setKmCompositeSearchPrimaryCategoryFilters(String kmCompositeSearchPrimaryCategoryFilters) {
this.kmCompositeSearchPrimaryCategoryFilters = kmCompositeSearchPrimaryCategoryFilters;
}

public List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public ResponseEntity<?> parichayProfileUpdate(@RequestBody Map<String, Object>
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<String, Object> request) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/sunbird/profile/service/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public interface ProfileService {

SBApiResponse userBasicProfileUpdate(Map<String, Object> request);

SBApiResponse userAutoComplete(String searchTerm);

SBApiResponse migrateUser(Map<String, Object> request, String userToken, String authToken);
}
52 changes: 52 additions & 0 deletions src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> resultResp = new HashMap<>();
try {
List<Map<String, Object>> 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<String, Object> request) {
SBApiResponse response = ProjectUtil.createDefaultResponse(Constants.API_USER_BASIC_PROFILE_UPDATE);
String errMsg = validateBasicProfilePayload(request);
Expand Down Expand Up @@ -930,4 +961,25 @@ private String syncUserData(String userId) {
}
return errMsg;
}

public List<Map<String, Object>> getUserSearchData(String searchTerm) throws Exception {
List<Map<String, Object>> resultArray = new ArrayList<>();
Map<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public List<Map<String, Object>> 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());
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a9eb3b8

Please sign in to comment.