-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #867 from egovernments/hrms-fuzzy
Hrms fuzzy
- Loading branch information
Showing
12 changed files
with
503 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ss-services/egov-hrms/src/main/java/org/egov/hrms/repository/ElasticSearchRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.egov.hrms.repository; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.egov.hrms.config.PropertiesManager; | ||
import org.egov.hrms.web.contract.EmployeeSearchCriteria; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Repository | ||
public class ElasticSearchRepository { | ||
private PropertiesManager config; | ||
|
||
private FuzzySearchQueryBuilder queryBuilder; | ||
|
||
private RestTemplate restTemplate; | ||
|
||
private ObjectMapper mapper; | ||
|
||
@Autowired | ||
public ElasticSearchRepository(PropertiesManager config, FuzzySearchQueryBuilder queryBuilder, RestTemplate restTemplate, ObjectMapper mapper) { | ||
this.config = config; | ||
this.queryBuilder = queryBuilder; | ||
this.restTemplate = restTemplate; | ||
this.mapper = mapper; | ||
} | ||
|
||
public Object fuzzySearchEmployees(EmployeeSearchCriteria criteria, List<String> uuids) { | ||
|
||
|
||
String url = getESURL(); | ||
|
||
String searchQuery = queryBuilder.getFuzzySearchQuery(criteria, uuids); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", getESEncodedCredentials()); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
log.info("Headers: " + headers.toString()); | ||
HttpEntity<String> requestEntity = new HttpEntity<>(searchQuery, headers); | ||
ResponseEntity response = null; | ||
try { | ||
response = restTemplate.postForEntity(url, requestEntity, Object.class); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new CustomException("ES_ERROR","Failed to fetch data from ES"); | ||
} | ||
|
||
return response.getBody(); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Generates elasticsearch search url from application properties | ||
* | ||
* @return | ||
*/ | ||
private String getESURL() { | ||
|
||
StringBuilder builder = new StringBuilder(config.getEsHost()); | ||
builder.append(config.getEsPTIndex()); | ||
builder.append(config.getEsSearchEndpoint()); | ||
|
||
return builder.toString(); | ||
} | ||
|
||
public String getESEncodedCredentials() { | ||
String credentials = config.getUserName() + ":" + config.getPassword(); | ||
byte[] credentialsBytes = credentials.getBytes(); | ||
byte[] base64CredentialsBytes = Base64.getEncoder().encode(credentialsBytes); | ||
return "Basic " + new String(base64CredentialsBytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...ss-services/egov-hrms/src/main/java/org/egov/hrms/repository/FuzzySearchQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package org.egov.hrms.repository; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.egov.hrms.config.PropertiesManager; | ||
import org.egov.hrms.web.contract.EmployeeSearchCriteria; | ||
import org.egov.tracer.model.CustomException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Repository | ||
@Slf4j | ||
public class FuzzySearchQueryBuilder { | ||
private ObjectMapper mapper; | ||
|
||
private PropertiesManager config; | ||
|
||
@Autowired | ||
public FuzzySearchQueryBuilder(ObjectMapper mapper, PropertiesManager config) { | ||
this.mapper = mapper; | ||
this.config = config; | ||
} | ||
|
||
private static final String BASE_QUERY = "{\n" + | ||
" \"from\": {{OFFSET}},\n" + | ||
" \"size\": {{LIMIT}},\n" + | ||
" \"sort\": {\n" + | ||
" \"_score\": {\n" + | ||
" \"order\": \"desc\"\n" + | ||
" }\n" + | ||
" },\n" + | ||
" \"query\": {\n" + | ||
" }\n" + | ||
"}"; | ||
|
||
private static final String fuzzyQueryTemplate = "{\n" + | ||
" \"match\": {\n" + | ||
" \"{{VAR}}\": {\n" + | ||
" \"query\": \"{{PARAM}}\",\n" + | ||
" \"fuzziness\": \"{{FUZZINESS}}\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }"; | ||
|
||
private static final String wildCardQueryTemplate = "{\n" + | ||
" \"query_string\": {\n" + | ||
" \"default_field\": \"{{VAR}}\",\n" + | ||
" \"query\": \"*{{PARAM}}*\"\n" + | ||
" }\n" + | ||
" }"; | ||
|
||
private static final String filterTemplate = "\"filter\": { " + | ||
" }"; | ||
|
||
public String getFuzzySearchQuery(EmployeeSearchCriteria criteria, List<String> ids) { | ||
String finalQuery = ""; | ||
|
||
try { | ||
String baseQuery = addPagination(criteria); | ||
JsonNode node = mapper.readTree(baseQuery); | ||
ObjectNode insideMatch = (ObjectNode)node.get("query"); | ||
List<JsonNode> fuzzyClauses = new LinkedList<>(); | ||
|
||
if(criteria.getName() != null){ | ||
fuzzyClauses.add(getInnerNode(criteria.getName(),"Data.user.name",config.getNameFuziness())); | ||
} | ||
|
||
ArrayNode tenantIdArray = mapper.createArrayNode(); | ||
tenantIdArray.add(criteria.getTenantId()); | ||
ObjectNode tenantIdFilter = mapper.createObjectNode(); | ||
tenantIdFilter.putPOJO("terms", mapper.createObjectNode().putPOJO("Data.tenantId.keyword", tenantIdArray)); | ||
fuzzyClauses.add(tenantIdFilter); | ||
|
||
if (criteria.getRoles() != null && !criteria.getRoles().isEmpty()) { | ||
ArrayNode roleArray = mapper.createArrayNode(); | ||
for (String role : criteria.getRoles()) { | ||
ObjectNode roleNode = mapper.createObjectNode(); | ||
roleNode.put("term", mapper.createObjectNode().put("Data.user.roles.code.keyword", role)); | ||
roleArray.add(roleNode); | ||
} | ||
ObjectNode rolesBoolNode = mapper.createObjectNode(); | ||
rolesBoolNode.putPOJO("bool", mapper.createObjectNode().putPOJO("should", roleArray)); | ||
fuzzyClauses.add(rolesBoolNode); | ||
|
||
ObjectNode boolQuery = mapper.createObjectNode(); | ||
boolQuery.putPOJO("must", fuzzyClauses); | ||
insideMatch.putPOJO("bool", boolQuery); | ||
finalQuery = mapper.writeValueAsString(node); | ||
} | ||
} catch (Exception e) { | ||
log.error("ES_ERROR", e); | ||
throw new CustomException("JSONNODE_ERROR", "Failed to build JSON query for fuzzy search"); | ||
} | ||
|
||
log.info("finalQuery {}",finalQuery); | ||
return finalQuery; | ||
} | ||
|
||
private JsonNode getInnerNode(String param, String var, String fuziness) throws JsonProcessingException { | ||
|
||
String template; | ||
if(config.getIsSearchWildcardBased()) | ||
template = wildCardQueryTemplate; | ||
else | ||
template = fuzzyQueryTemplate; | ||
String innerQuery = template.replace("{{PARAM}}",getEscapedString(param)); | ||
innerQuery = innerQuery.replace("{{VAR}}",var); | ||
|
||
if(!config.getIsSearchWildcardBased()) | ||
innerQuery = innerQuery.replace("{{FUZZINESS}}", fuziness); | ||
|
||
JsonNode innerNode = mapper.readTree(innerQuery); | ||
return innerNode; | ||
} | ||
|
||
private String addPagination(EmployeeSearchCriteria criteria) { | ||
|
||
Long limit = config.getDefaultLimit(); | ||
Long offset = config.getDefaultOffset(); | ||
|
||
if (criteria.getLimit() != null && criteria.getLimit() <= config.getMaxSearchLimit()) | ||
limit = criteria.getLimit(); | ||
|
||
if (criteria.getLimit() != null && criteria.getLimit() > config.getMaxSearchLimit()) | ||
limit = config.getMaxSearchLimit(); | ||
|
||
if (criteria.getOffset() != null) | ||
offset = criteria.getOffset(); | ||
|
||
String baseQuery = BASE_QUERY.replace("{{OFFSET}}", offset.toString()); | ||
baseQuery = baseQuery.replace("{{LIMIT}}", limit.toString()); | ||
|
||
return baseQuery; | ||
} | ||
|
||
/** | ||
* Escapes special characters in given string | ||
* @param inputString | ||
* @return | ||
*/ | ||
private String getEscapedString(String inputString){ | ||
final String[] metaCharacters = {"\\","/","^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%"}; | ||
for (int i = 0 ; i < metaCharacters.length ; i++) { | ||
if (inputString.contains(metaCharacters[i])) { | ||
inputString = inputString.replace(metaCharacters[i], "\\\\" + metaCharacters[i]); | ||
} | ||
} | ||
return inputString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.