Skip to content

Commit

Permalink
Pool: Replace opensearch with database fuzzysearch with review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SujitMBRDI committed Oct 19, 2023
1 parent e6a4d31 commit a156145
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,8 @@ interface LegalEntityRepository : PagingAndSortingRepository<LegalEntity, Long>,

fun findByUpdatedAtAfter(updatedAt: Instant, pageable: Pageable): Page<LegalEntity>

/*@Query("SELECT DISTINCT p FROM LegalEntity p WHERE p.legalName.value = :value")
fun findByLegalNameValue(value:String, pageable: Pageable): Page<LegalEntity>*/

/*@Query("SELECT DISTINCT p, levenshtein_distance(p.legalName.value, :value) AS distance\n" +
"FROM LegalEntity p\n" +
"WHERE p.legalName.value LIKE :value\n" +
"ORDER BY distance DESC\n")
fun findByLegalNameValue(value: String, pageable: Pageable): Page<LegalEntity>*/

/*@Query(value = "SELECT p.*, SIMILARITY(p.legalName.value, :value) AS score " +
"FROM LegalEntity p " +
"WHERE SIMILARITY(p.legalName.value, :value) > 0.3 " + // Adjust the similarity threshold as needed
"ORDER BY score DESC",
countQuery = "SELECT COUNT(*) FROM LegalEntity p WHERE SIMILARITY(p.legalName.value, :value) > 0.3", // Count query
nativeQuery = true)
fun findByLegalNameValue(value:String, pageable: Pageable): Page<LegalEntity>*/

/*fun findByLegalNameValue(value: String, pageable: Pageable): Page<LegalEntity>*/

@Query("SELECT DISTINCT p FROM LegalEntity p WHERE LOWER(p.legalName.value) LIKE LOWER(CONCAT('%', :value, '%'))")
@Query("SELECT p FROM LegalEntity p WHERE LOWER(p.legalName.value) LIKE LOWER(CONCAT('%', :value, '%')) " +
"ORDER BY CASE WHEN LOWER(p.legalName.value) = LOWER(:value) THEN 0 WHEN LENGTH(p.legalName.value) <= LENGTH(:value) THEN 1 ELSE 2 END, LENGTH(p.legalName.value) DESC")
fun findByLegalNameValue(value: String, pageable: Pageable): Page<LegalEntity>

@Query("SELECT DISTINCT i.legalEntity FROM LegalEntityIdentifier i WHERE i.type = :type AND upper(i.value) = upper(:idValue)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ interface LogisticAddressRepository : PagingAndSortingRepository<LogisticAddress
pageable: Pageable
): Page<LogisticAddress>

@Query("SELECT DISTINCT a FROM LogisticAddress a WHERE LOWER(a.name) LIKE LOWER(CONCAT('%', :addressName, '%'))")
@Query("SELECT a FROM LogisticAddress a WHERE LOWER(a.name) LIKE LOWER(CONCAT('%', :addressName, '%')) " +
"ORDER BY CASE WHEN LOWER(a.name) = LOWER(:addressName) THEN 0 WHEN LENGTH(a.name) <= LENGTH(:addressName) THEN 1 ELSE 2 END, LENGTH(a.name) DESC")
fun findByName(addressName: String, pageable: Pageable): Page<LogisticAddress>

@Query("SELECT DISTINCT a FROM LogisticAddress a LEFT JOIN FETCH a.legalEntity LEFT JOIN FETCH a.legalEntity.legalAddress WHERE a IN :addresses")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,19 @@ class BusinessPartnerSearchService(
searchRequest: BusinessPartnerSearchRequest,
paginationRequest: PaginationRequest
): PageDto<Pair<Float, LegalEntity>> {
logger.debug { "Search index for legal entities" }
val value = searchRequest.partnerProperties.legalName.toString()
val legalEntityPage = legalEntityRepository.findByLegalNameValue(value, PageRequest.of(paginationRequest.page, paginationRequest.size))
logger.debug { "Search for legal entities" }
val value = searchRequest.partnerProperties.legalName
val legalEntityPage = legalEntityRepository.findByLegalNameValue(value!!, PageRequest.of(paginationRequest.page, paginationRequest.size))
return PageDto(
totalElements = legalEntityPage.totalElements,
totalPages = legalEntityPage.totalPages,
page = paginationRequest.page,
content = legalEntityPage.content.map { Pair(legalEntityPage.totalElements - levenshteinDistance(it.legalName.value, value), it) },
content = legalEntityPage.content.map { Pair(0f, it) }, //assign 0 score as no open search has been conducted and ordering maintained based on length
contentSize = legalEntityPage.content.size
)

}

private fun levenshteinDistance(text1: String, text2: String): Float {
val len1 = text1.length
val len2 = text2.length
val d = Array(len1 + 1) { Array(len2 + 1) { IntArray(3) } }

for (i in 1 until len1 + 1) {
d[i][0] = intArrayOf(i, i, i)
}
for (j in 1 until len2 + 1) {
d[0][j] = intArrayOf(j, j, j)
}

for (i in 1 until len1 + 1) {
for (j in 1 until len2 + 1) {
if (text1[i - 1] == text2[j - 1]) {
d[i][j] = d[i - 1][j - 1].copyOf()
} else {
d[i][j] = intArrayOf(d[i - 1][j][0] + 1, d[i][j - 1][1] + 1, d[i - 1][j - 1][2] + 1)
}
}
}

return d[len1][len2][2].toFloat() / len1.toFloat()
}

/**
* @see BusinessPartnerSearchService.searchLegalEntities
*
Expand Down Expand Up @@ -171,15 +146,14 @@ class BusinessPartnerSearchService(
searchRequest: AddressPartnerSearchRequest,
paginationRequest: PaginationRequest
): PageDto<Pair<Float, LogisticAddress>> {
logger.debug { "Search index for addresses" }
val addressName = searchRequest.name.toString()
val addressPage = logisticAddressRepository.findByName(addressName, PageRequest.of(paginationRequest.page, paginationRequest.size))

logger.debug { "Search for addresses" }
val addressName = searchRequest.name
val addressPage = logisticAddressRepository.findByName(addressName!!, PageRequest.of(paginationRequest.page, paginationRequest.size))
return PageDto(
totalElements = addressPage.totalElements,
totalPages = addressPage.totalPages,
page = paginationRequest.page,
content = addressPage.content.map { Pair(addressPage.totalElements - levenshteinDistance(it.name.toString(), addressName), it) },
content = addressPage.content.map { Pair(0f, it) }, //assign 0 score as no open search has been conducted and ordering maintained based on length
contentSize = addressPage.content.size
)

Expand Down

0 comments on commit a156145

Please sign in to comment.