Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0.1 auto complete v2 #131

Open
wants to merge 3 commits into
base: cbrelease-4.0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ public class CbExtServerProperties {
@Value("${sb.data.sync.path}")
private String lmsDataSyncPath;

@Value("${sb.es.host}")
private String sbEsHost;

@Value("${sb.es.port}")
private String sbEsPort;

@Value("${sb.es.username}")
private String sbEsUser;

@Value("${sb.es.password}")
private String sbEsPassword;

@Value("${km.base.content.search}")
private String kmBaseContentSearch;

public String getUserAssessmentSubmissionDuration() {
return userAssessmentSubmissionDuration;
}
Expand Down Expand Up @@ -1248,4 +1263,44 @@ public String getEsProfileIndexWorkAllocation() {
public void setEsProfileIndexWorkAllocation(String esProfileIndexWorkAllocation) {
this.esProfileIndexWorkAllocation = esProfileIndexWorkAllocation;
}

public String getSbEsHost() {
return sbEsHost;
}

public void setSbEsHost(String sbEsHost) {
this.sbEsHost = sbEsHost;
}

public String getSbEsPort() {
return sbEsPort;
}

public void setSbEsPort(String sbEsPort) {
this.sbEsPort = sbEsPort;
}

public String getSbEsUser() {
return sbEsUser;
}

public void setSbEsUser(String sbEsUser) {
this.sbEsUser = sbEsUser;
}

public String getSbEsPassword() {
return sbEsPassword;
}

public void setSbEsPassword(String sbEsPassword) {
this.sbEsPassword = sbEsPassword;
}

public String getKmBaseContentSearch() {
return kmBaseContentSearch;
}

public void setKmBaseContentSearch(String kmBaseContentSearch) {
this.kmBaseContentSearch = kmBaseContentSearch;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sunbird/common/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public class Constants {
public static final String SYNC = "sync";
public static final String OBJECT_IDS = "objectIds";
public static final String USER = "user";
public static final String PUBLIC_COURSE_LIST = "publicCourseList";
public static final String PUBLIC_COURSE_LIST = "exploreOpenCourseList";
public static final String LAST_UPDATED_ON = "lastUpdatedOn";
public static final String DESCENDING_ORDER = "desc";

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/sunbird/common/util/IndexerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
Expand All @@ -33,7 +34,12 @@ public class IndexerService {
private Logger logger = LoggerFactory.getLogger(IndexerService.class);

@Autowired
@Qualifier("esClient")
private RestHighLevelClient esClient;

@Autowired
@Qualifier("sbEsClient")
private RestHighLevelClient sbEsClient;

/**
* @param index name of index
Expand Down Expand Up @@ -107,14 +113,13 @@ public Map<String, Object> readEntity(String index, String indexType, String ent
* @return es search response
* @throws IOException
*/
public SearchResponse getEsResult(String indexName, String type, SearchSourceBuilder searchSourceBuilder) throws IOException {
public SearchResponse getEsResult(String indexName, String type, SearchSourceBuilder searchSourceBuilder, boolean isSunbirdES) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(indexName);
if (!StringUtils.isEmpty(type))
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
return esClient.search(searchRequest, RequestOptions.DEFAULT);

return getEsResult(searchRequest, isSunbirdES);
}

public RestStatus BulkInsert(List<IndexRequest> indexRequestList) {
Expand Down Expand Up @@ -144,4 +149,12 @@ public long getDocumentCount(String index, SearchSourceBuilder searchSourceBuild
return 0l;
}
}

private SearchResponse getEsResult(SearchRequest searchRequest, boolean isSbES) throws IOException {
if(isSbES) {
return sbEsClient.search(searchRequest, RequestOptions.DEFAULT);
} else {
return esClient.search(searchRequest, RequestOptions.DEFAULT);
}
}
}
44 changes: 25 additions & 19 deletions src/main/java/org/sunbird/core/config/EsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@

@Configuration
public class EsConfig {

@Autowired
CbExtServerProperties configuration;

@Bean(destroyMethod = "close")
public RestHighLevelClient restHighLevelClient(CbExtServerProperties configuration) {

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(configuration.getEsUser(), configuration.getEsPassword()));

RestClientBuilder builder = RestClient
.builder(new HttpHost(configuration.getEsHost(), Integer.parseInt(configuration.getEsPort())))
.setHttpClientConfigCallback(
httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

return new RestHighLevelClient(builder);

}
@Autowired
CbExtServerProperties configuration;

@Bean(name = "esClient", destroyMethod = "close")
public RestHighLevelClient getCbEsRestClient(CbExtServerProperties configuration) {
return createRestClient(configuration.getEsHost(), configuration.getEsPort(), configuration.getEsUser(),
configuration.getEsPassword());
}

@Bean(name = "sbEsClient", destroyMethod = "close")
public RestHighLevelClient getSbESRestClient(CbExtServerProperties configuration) {
return createRestClient(configuration.getSbEsHost(), configuration.getSbEsPort(), configuration.getSbEsUser(),
configuration.getSbEsPassword());
}

private RestHighLevelClient createRestClient(String host, String port, String user, String password) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));

RestClientBuilder builder = RestClient.builder(new HttpHost(host, Integer.parseInt(port)))
.setHttpClientConfigCallback(
httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

return new RestHighLevelClient(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public SBApiResponse getExploreCourseList() {
responseCourseList = (Map<String, Object>) searchResponse.get(Constants.RESULT);
redisCacheMgr.putCache(Constants.PUBLIC_COURSE_LIST, responseCourseList);
response.setResult(responseCourseList);
logger.info(String.format(
"Adding open course details into Cache. CourseId Count: %s, ContentSearch Count: %s",
identifierList.size(), responseCourseList.get(Constants.COUNT)));
}
} else {
response.setResult(responseCourseList);
Expand All @@ -80,6 +83,7 @@ public SBApiResponse getExploreCourseList() {
logger.error(errMsg, e);
}
if (StringUtils.isNotEmpty(errMsg)) {
logger.error("Failed to initialize the Open Course Details to Cache. ErrMsg: " + errMsg);
response.getParams().setErrmsg(errMsg);
response.getParams().setStatus(Constants.FAILED);
response.setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -98,7 +102,7 @@ public SBApiResponse refreshCache() {
private Map<String, Object> searchContent(List<String> identifierList) {
try {
StringBuilder sbUrl = new StringBuilder(serverProperties.getKmBaseHost());
sbUrl.append(serverProperties.getKmCompositeSearchPath());
sbUrl.append(serverProperties.getKmBaseContentSearch());
Map<String, String> headers = new HashMap<String, String>();
headers.put(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
return outboundRequestHandlerService.fetchResultUsingPost(sbUrl.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ResponseEntity<?> userAutoComplete(@PathVariable("searchTerm") String sea
return new ResponseEntity<>(response, response.getResponseCode());
}

@PostMapping("/user/v1/migrate")
@PatchMapping("/user/v1/migrate")
private ResponseEntity<?> adminMigrateUser(@RequestHeader(Constants.X_AUTH_TOKEN) String userToken,
@RequestHeader(Constants.AUTH_TOKEN) String authToken, @RequestBody Map<String, Object> request) {
SBApiResponse response = profileService.migrateUser(request, userToken, authToken);
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/sunbird/profile/service/ProfileServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public SBApiResponse userAutoComplete(String searchTerm) {
response.put(Constants.RESPONSE, resultResp);
} catch (Exception e) {
response.getParams()
.setErrmsg("Exception occurred while searching the user's from user registry" + e.getMessage());
.setErrmsg("Failed to get user details from ES. Exception: " + e.getMessage());
}
return response;
}
Expand Down Expand Up @@ -920,11 +920,14 @@ private String executeMigrateUser(Map<String, Object> request, Map<String, Strin
serverConfig.getSbUrl() + serverConfig.getLmsUserMigratePath(), request, headers);
if (migrateResponse != null
&& Constants.OK.equalsIgnoreCase((String) migrateResponse.get(Constants.RESPONSE_CODE))) {
log.info(String.format("Successfully self migrated user. UserId: %s, Channel: %s",
log.info(String.format("Successfully migrated user. UserId: %s, Channel: %s",
(String) request.get(Constants.USER_ID), (String) request.get(Constants.CHANNEL)));
} else {
try {
errMsg = "Failed to Self migrate User.";
if (migrateResponse != null)
errMsg = (String) ((Map<String, Object>) migrateResponse.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE);
else
errMsg = "Failed to migrate User.";
log.warn(String.format("%s. Error: %s", errMsg, mapper.writeValueAsString(migrateResponse)));
} catch (Exception e) {
}
Expand Down Expand Up @@ -955,31 +958,33 @@ private String syncUserData(String userId) {

Map<String, Object> syncDataResp = (Map<String, Object>) outboundRequestHandlerService.fetchResultUsingPost(
serverConfig.getSbUrl() + serverConfig.getLmsDataSyncPath(), requestBody, MapUtils.EMPTY_MAP);
if (syncDataResp == null
|| !Constants.OK.equalsIgnoreCase((String) syncDataResp.get(Constants.RESPONSE_CODE))) {
if (syncDataResp != null
&& !Constants.OK.equalsIgnoreCase((String) syncDataResp.get(Constants.RESPONSE_CODE))) {
errMsg = (String) ((Map<String, Object>) syncDataResp.get(Constants.PARAMS)).get(Constants.ERROR_MESSAGE);
} else if (syncDataResp == null) {
errMsg = "Failed to call Data Sync after updating Profile for User: " + userId;
}
return errMsg;
}

public List<Map<String, Object>> getUserSearchData(String searchTerm) throws Exception {
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()) {
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[]{});
sourceBuilder.fetchSource(serverConfig.getEsAutoCompleteIncludeFields(), new String[] {});
SearchResponse searchResponse = indexerService.getEsResult(serverConfig.getEsProfileIndex(),
serverConfig.getEsProfileIndexType(), sourceBuilder);
serverConfig.getEsProfileIndexType(), sourceBuilder, true);
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 @@ -296,7 +296,7 @@ private String validateRegisterationPayload(UserRegistrationInfo userRegInfo) {

private UserRegistration getUserRegistrationDocument(Map<String, Object> mustMatch) throws Exception {
SearchResponse searchResponse = indexerService.getEsResult(serverProperties.getUserRegistrationIndex(),
serverProperties.getEsProfileIndexType(), queryBuilder(mustMatch));
serverProperties.getEsProfileIndexType(), queryBuilder(mustMatch), false);

if (searchResponse.getHits().getTotalHits() > 0) {
SearchHit hit = searchResponse.getHits().getAt(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package org.sunbird.workallocation.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
Expand All @@ -31,14 +38,23 @@
import org.sunbird.common.util.Constants;
import org.sunbird.common.util.IndexerService;
import org.sunbird.core.exception.BadRequestException;
import org.sunbird.workallocation.model.*;
import org.sunbird.workallocation.model.Child;
import org.sunbird.workallocation.model.ChildNode;
import org.sunbird.workallocation.model.CompetencyDetails;
import org.sunbird.workallocation.model.FracRequest;
import org.sunbird.workallocation.model.FracResponse;
import org.sunbird.workallocation.model.Role;
import org.sunbird.workallocation.model.RoleCompetency;
import org.sunbird.workallocation.model.SearchCriteria;
import org.sunbird.workallocation.model.WAObject;
import org.sunbird.workallocation.model.WorkAllocation;
import org.sunbird.workallocation.model.WorkAllocationDTO;
import org.sunbird.workallocation.util.FRACReqBuilder;
import org.sunbird.workallocation.util.Validator;
import org.sunbird.workallocation.util.WorkAllocationConstants;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class AllocationService {
Expand All @@ -65,9 +81,6 @@ public class AllocationService {
@Autowired
private IndexerService indexerService;

@Autowired
private EnrichmentService enrichmentService;

@Autowired
private FRACReqBuilder fracReqBuilder;

Expand Down Expand Up @@ -212,7 +225,7 @@ public Response getUsers(SearchCriteria criteria) {
Map<String, Object> result;
long totalCount = 0;
try {
SearchResponse searchResponse = indexerService.getEsResult(index, indexType, sourceBuilder);
SearchResponse searchResponse = indexerService.getEsResult(index, indexType, sourceBuilder, false);
totalCount = searchResponse.getHits().getTotalHits();
for (SearchHit hit : searchResponse.getHits()) {
allocationSearchList.add(mapper.convertValue(hit.getSourceAsMap(), WorkAllocation.class));
Expand Down Expand Up @@ -251,7 +264,7 @@ public Map<String, Object> getUserDetails(Set<String> userIds) throws IOExceptio
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query);
sourceBuilder.fetchSource(includeFields, new String[] {});
SearchResponse searchResponse = indexerService.getEsResult(configuration.getEsProfileIndex(),
configuration.getEsProfileIndexType(), sourceBuilder);
configuration.getEsProfileIndexType(), sourceBuilder, false);
for (SearchHit hit : searchResponse.getHits()) {
Map<String, Object> userResult = extractUserDetails(hit.getSourceAsMap());
if (!StringUtils.isEmpty(userResult.get("wid")))
Expand Down Expand Up @@ -308,7 +321,7 @@ public List<Map<String, Object>> getUserSearchData(String searchTerm) throws IOE
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(query);
sourceBuilder.fetchSource(includeFields, new String[] {});
SearchResponse searchResponse = indexerService.getEsResult(configuration.getEsProfileIndexWorkAllocation(),
configuration.getEsProfileIndexType(), sourceBuilder);
configuration.getEsProfileIndexType(), sourceBuilder, false);
for (SearchHit hit : searchResponse.getHits()) {
result = extractUserDetails(hit.getSourceAsMap());
resultArray.add(result);
Expand Down
Loading