본문 바로가기
개발/ALGOLIA

알고리아 Managing Results(3-38)

by dev_caleb 2022. 4. 28.
728x90

 

https://www.algolia.com/doc/guides/managing-results/refine-results/geolocation/how-to/filter-results-around-a-location/

 

Filter results around a location | Algolia

Use Algolia's geo search capabilities to filter results around a location.

www.algolia.com

 

Filter Results Around a Location

 

This guide shows you how to filter results around a location. This location can be set manually or taken from the end user’s current position.

 

이 안내서는 위치 주변의 결과를 필터링하는 방법을 보여 줍니다. 이 위치는 수동으로 설정하거나 최종 사용자의 현재 위치에서 가져올 수 있습니다.

Dataset#

The guide uses a dataset of the 3,000+ biggest airports in the world.

 

[
 {
    "objectID": "3797",
    "name": "John F Kennedy Intl",
    "city": "New York",
    "country": "United States",
    "iata_code": "JFK",
    "_geoloc": {
      "lat": 40.639751,
      "lng": -73.778925
    },
    "links_count": 911
  }
]

 

Store each record’s location (latitude and longitude) in the _geoloc attribute.

First, download the airport dataset and then import it into Algolia.

각 레코드의 위치(위도 및 경도)를 _geoloc 특성에 저장합니다.
먼저 공항 데이터 세트를 다운로드한 후 알골리아로 가져옵니다.

 

 

Initialize the client

// for the default version
const algoliasearch = require('algoliasearch');

// for the default version
import algoliasearch from 'algoliasearch';

// for the search only version
import algoliasearch from 'algoliasearch/lite';

// or just use algoliasearch if you are using a <script> tag
// if you are using AMD module loader, algoliasearch will not be defined in window,
// but in the AMD modules of the page

const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');

 

Configure index settings(인덱스 설정 구성하기)

Even if you just want to sort by distance to a location, your textual relevance should also be good so that end users can refine the search with a query. To do that, you must configure the index.

The searchable attributes are: name, city, country, and iata_code.

위치까지의 거리를 기준으로 정렬하는 경우에도 텍스트 관련성이 좋아야 최종 사용자가 질의를 통해 검색을 세분화할 수 있습니다. 이렇게 하려면 인덱스를 구성해야 합니다.
검색 가능한 속성은 이름, 도시, 국가 및 iata_code입니다.

index.setSettings({
  searchableAttributes: [
    'name',
    'city',
    'country',
    'iata_code'
  ],
  customRanking: ['desc(links_count)'],
}).then(() => {
  // done
});

Custom ranking

The engine will use an airport’s number of connected airports as a ranking metric. The more connections, the better.

엔진은 공항의 연결된 공항 수를 순위 측정 기준으로 사용합니다. 연결이 많을수록 더 좋습니다.

Ranking

When filtering around a location, Algolia can also sort the results by distance from this location. This sorting by distance happens in the ranking formula’s geo criterion. If geo isn’t active, you can’t sort by distance.

 

위치 주변을 필터링할 때 Algolia는 이 위치와의 거리에 따라 결과를 정렬할 수도 있습니다. 거리에 따른 이러한 정렬은 순위 공식의 지리적 기준에서 발생합니다. Geo가 활성화되지 않으면 거리로 정렬할 수 없습니다.

 

 

Filtering around a given location(지정된 위치 주변 필터링)

To filter airports around New York City (latitude 40.71 and longitude -74.01), use the aroundLatLngparameter.

뉴욕 시 주변의 공항(위도 40.71 및 경도 -74.01)을 필터링하려면 aroundLatLng 매개변수를 사용합니다.

 

index.search('', {
  aroundLatLng: '40.71, -74.01'
}).then(({ hits }) => {
  console.log(hits);
});

An empty query ('') tells Algolia to retrieve all airports 빈 쿼리('')는 Algolia에게 모든 공항을 검색하도록 지시합니다.

 

 

Filtering around the end user’s current location(엔드유저 현재위치 주변 필터링)

As you don’t know the end user’s coordinates in advance, you can retrieve their IP address’s associated location with the aroundLatLngViaIP parameter. 최종 사용자의 좌표를 미리 알지 못하므로 LatLngVia 주변의 IP 주소와 연결된 위치를 검색할 수 있습니다.IP 매개 변수.

/**
 * '94.228.178.246' should be replaced with the actual IP you would like to search around.
 * Depending on your stack there are multiple ways to get this information.
 */

index.search('', {
  aroundLatLngViaIP: true,
  headers: {
    'X-Forwarded-For': '94.228.178.246'
  }
}).then(({ hits }) => {
  console.log(hits);
});

Filtering by radius( Radius로 필터링)

By default, the engine automatically defines a radius to filter on depending on the population density of the end user’s location.

To define the radius yourself, use the aroundRadius parameter. The bigger the radius, the less filtering you have.

This example sorts by distance to New York City, with a radius of 1,000 km.

기본적으로 엔진은 엔드유저 위치의 인구 밀도에 따라 필터링할 반경을 자동으로 정의합니다.
반지름을 직접 정의하려면 aroundRadius 매개변수를 사용합니다. 반경이 클수록 필터링이 줄어듭니다.
이 예는 반경 1,000km의 뉴욕 시까지의 거리에 따라 정렬됩니다.

index.search('', {
  aroundLatLng: '40.71, -74.01',
  aroundRadius: 1000000 // 1000 km
}).then(({ hits }) => {
  console.log(hits);
});

 

728x90

'개발 > ALGOLIA' 카테고리의 다른 글

알고리아 Managing Results(3-40)  (0) 2022.04.28
알고리아 Managing Results(3-39)  (0) 2022.04.28
알고리아 Managing Results(3-37)  (0) 2022.04.27
알고리아 Managing Results(3-36)  (0) 2022.04.27
알고리아 Managing Results(3-35)  (0) 2022.04.27