Azure search service REST API (2) - Filtering

aram·2022년 8월 11일
0

인덱스 별로 필터 조건이 다른 경우

fun searchAutoComplete(
        searchTerm: String,
        index: String,
        marketingType: MarketingType?,
        consultationEnabled: Boolean?,
        distance: Float?,
        longitude: String?,
        latitude: String?
    ){
        val actionName = "search"
        Log.d("debug", "$actionName $index started.")

        var hospitalFilter = ""
        var doctorFilter = ""
        var dealFilter = ""
        var departmentFilter = ""
        var specialtyFilter = ""

        val locationFilterString = "geo.distance(Location/Point, geography'POINT(${longitude} ${latitude})') le ${distance?.roundToInt()}"

        marketingType?.let {
            if (it.name.equals("both")) {
                consultationEnabled?.let {
                    if(it) {
                        hospitalFilter = "ConsultationEnabled eq $it"
                        doctorFilter = "ConsultationEnabled eq $it"

                        if (distance != null && distance > 0f) {
                            hospitalFilter = hospitalFilter.plus("and $locationFilterString")
                        }
                    } else {
                        if (distance != null && distance > 0f) {
                            hospitalFilter = locationFilterString
                        }
                    }
                }
            } else {
                hospitalFilter = "MarketingType eq '${marketingType.value.replaceFirstChar { it.uppercase() }}'"
                doctorFilter = "Hospital/MarketingType eq '${marketingType.value.replaceFirstChar { it.uppercase() }}'"
                dealFilter = "MarketingType eq '${marketingType.value.replaceFirstChar { it.uppercase() }}'"
                departmentFilter = "MarketingType eq '${marketingType.value.replaceFirstChar { it.uppercase() }}'"
                specialtyFilter = "Department/MarketingType eq '${marketingType.value.replaceFirstChar { it.uppercase() }}'"

                if (distance != null && distance > 0f) {
                    hospitalFilter = hospitalFilter.plus("and $locationFilterString")
                }

                consultationEnabled?.let {
                    if(it) {
                        hospitalFilter = hospitalFilter.plus("and ConsultationEnabled eq $it")
                        doctorFilter = doctorFilter.plus("and ConsultationEnabled eq $it")

                        if (distance != null && distance > 0f) {
                            hospitalFilter = hospitalFilter.plus("and $locationFilterString")
                        }
                    } else {
                        if (distance != null && distance > 0f) {
                            hospitalFilter = locationFilterString
                        }
                    }
                }
            }
        }

        val searchOption = SearchOption(
            search = "Translations/Name: ${searchTerm.trim()}*",
            select = "Id, Translations",
            searchFields = "Translations/Name",
            queryType = "full",
            top = 3)

        when (index) {
            "idx-hospitals-int" -> {
                searchOption.filter = hospitalFilter
                searchHospitals(searchOption, index)
            }
            "idx-doctors-int" -> {
                searchOption.filter = doctorFilter
                searchDoctors(searchOption, index)
            }
            "idx-deals-int" -> {
                searchOption.filter = dealFilter
                searchDeals(searchOption, index)
            }
            "idx-departments-int" -> {
                searchOption.filter = departmentFilter
                searchDepartments(searchOption, index)
            }
            "idx-specialties-int" -> {
                searchOption.filter = specialtyFilter
                searchSpecialties(searchOption, index)
            }
        }
    }

0개의 댓글