Implemented that if an integer is enter only for bank code is search, otherwise only for bank name and city

This commit is contained in:
dankito 2020-05-14 22:36:30 +02:00
parent ed5d4bb241
commit b18a9a04f4
3 changed files with 23 additions and 18 deletions

View File

@ -14,6 +14,22 @@ abstract class BankFinderBase : IBankFinder {
} }
protected abstract fun findBankByNameOrCityForNonEmptyQuery(query: String): List<BankInfo>
override fun findBankByNameBankCodeOrCity(query: String?): List<BankInfo> {
if (query.isNullOrBlank()) {
return getBankList()
}
if (query.toIntOrNull() != null) { // if query is an integer, then it can only be an bank code, but not a bank name or city
return findBankByBankCode(query)
}
return findBankByNameOrCityForNonEmptyQuery(query)
}
protected open fun loadBankListFile(): List<BankInfo> { protected open fun loadBankListFile(): List<BankInfo> {
try { try {
val bankListString = readBankListFile() val bankListString = readBankListFile()

View File

@ -21,22 +21,17 @@ open class InMemoryBankFinder() : BankFinderBase(), IBankFinder {
return getBankList().filter { it.bankCode.startsWith(query) } return getBankList().filter { it.bankCode.startsWith(query) }
} }
override fun findBankByNameBankCodeOrCity(query: String?): List<BankInfo> { override fun findBankByNameOrCityForNonEmptyQuery(query: String): List<BankInfo> {
if (query.isNullOrEmpty()) { val queryPartsLowerCase = query.toLowerCase().split(" ")
return getBankList()
}
val queryLowerCase = query.toLowerCase()
val queryPartsLowerCase = queryLowerCase.split(" ")
return getBankList().filter { bankInfo -> return getBankList().filter { bankInfo ->
checkIfAllQueryPartsMatchBank(queryPartsLowerCase, bankInfo) checkIfAllQueryPartsMatchBankNameOrCity(queryPartsLowerCase, bankInfo)
} }
} }
protected open fun checkIfAllQueryPartsMatchBank(queryPartsLowerCase: List<String>, bankInfo: BankInfo): Boolean { protected open fun checkIfAllQueryPartsMatchBankNameOrCity(queryPartsLowerCase: List<String>, bankInfo: BankInfo): Boolean {
for (queryPartLowerCase in queryPartsLowerCase) { for (queryPartLowerCase in queryPartsLowerCase) {
if (checkIfQueryMatchesBank(bankInfo, queryPartLowerCase) == false) { if (checkIfQueryMatchesBankNameOrCity(bankInfo, queryPartLowerCase) == false) {
return false return false
} }
} }
@ -44,9 +39,8 @@ open class InMemoryBankFinder() : BankFinderBase(), IBankFinder {
return true return true
} }
protected open fun checkIfQueryMatchesBank(bankInfo: BankInfo, queryLowerCase: String): Boolean { protected open fun checkIfQueryMatchesBankNameOrCity(bankInfo: BankInfo, queryLowerCase: String): Boolean {
return bankInfo.name.toLowerCase().contains(queryLowerCase) return bankInfo.name.toLowerCase().contains(queryLowerCase)
|| bankInfo.bankCode.startsWith(queryLowerCase)
|| bankInfo.city.toLowerCase().contains(queryLowerCase) || bankInfo.city.toLowerCase().contains(queryLowerCase)
} }

View File

@ -90,19 +90,14 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
return getBanksFromQuery(luceneQuery) return getBanksFromQuery(luceneQuery)
} }
override fun findBankByNameBankCodeOrCity(query: String?): List<BankInfo> { override fun findBankByNameOrCityForNonEmptyQuery(query: String): List<BankInfo> {
bankFinderWhileUpdatingIndex?.let { bankFinderWhileUpdatingIndex?.let {
return it.findBankByNameBankCodeOrCity(query) return it.findBankByNameBankCodeOrCity(query)
} }
if (query.isNullOrBlank()) {
return getBankList()
}
val luceneQuery = queries.createQueriesForSingleTerms(query.toLowerCase()) { singleTerm -> val luceneQuery = queries.createQueriesForSingleTerms(query.toLowerCase()) { singleTerm ->
listOf( listOf(
queries.fulltextQuery(BankInfoNameFieldName, singleTerm), queries.fulltextQuery(BankInfoNameFieldName, singleTerm),
queries.startsWith(BankInfoBankCodeFieldName, singleTerm),
queries.contains(BankInfoCityIndexedFieldName, singleTerm) queries.contains(BankInfoCityIndexedFieldName, singleTerm)
) )
} }