카테고리 없음

Elastic Search - 위치와 검색어 관련 쿼리

빠릿베짱이 2023. 6. 19. 19:00
반응형

검색어와 위치를 동시에 고려하는 경우,

검색어로만 하면, 너무 멀리 있는 장소가 검색되고,

위치만 고려하면 관련없는 장소가 검색되서

삽질 끝에 만든 방법

 

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "terms": {
                                "category.keyword": [
                                    "병원"
                                ]
                            }
                        },
                        {
                            "bool": {
                                "should": [
                                    {
                                        "match": {
                                            "place_name.ngram": "현대"
                                        }
                                    }
                                ]
                            }
                        }
                    ],
                    "minimum_should_match": 0,
                    "filter": []
                }
            },
            "boost_mode": "multiply",
            "min_score": 0,
            "functions": [
                {
                    "filter": {
                        "match": {
                            "place_name": "현대"
                        }
                    },
                    "weight": 50
                },
                {
                    "script_score": {
                        "script": {
                            "source": "double distance = doc['location'].arcDistance(37.123,127.233); int result = 100 - (int)(distance/1000/5); return result < 0 ? 0:result"
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "_score": {
                "order": "desc"
            }
        },
        {
            "_geo_distance": {
                "location": {
                    "lat": 37.123,
                    "lon": 127.233
                },
                "order": "ASC",
                "unit": "m",
                "mode": "min",
                "distance_type": "arc",
                "ignore_unmapped": true
            }
        },
        {
            "id": {
                "order": "desc"
            }
        }
    ],
    "from": 0,
    "size": 31
}
반응형