// composer autoloadrequire__DIR__.'/vendor/autoload.php';// if you are not using composer// require_once 'path/to/algoliasearch.php';$client=\Algolia\AlgoliaSearch\SearchClient::create('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');$index=$client->initIndex('your_index_name');
// for the default versionconstalgoliasearch=require('algoliasearch');// for the default versionimportalgoliasearchfrom'algoliasearch';// for the search only versionimportalgoliasearchfrom'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 pageconstclient=algoliasearch('AJ0P3S7DWQ','••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb');constindex=client.initIndex('your_index_name');
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.
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.
/**
* '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.
*/$ip='94.228.178.246';$results=$index->search('',['aroundLatLngViaIP'=>true,'X-Forwarded-For'=>$ip]);
1
2
3
4
5
6
7
8
results=index.search('',{aroundLatLngViaIP: true,headers: {# '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.'X-Forwarded-For':'94.228.178.246'}})
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* '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);});
1
2
3
4
5
6
7
8
9
# '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.
ip='94.228.178.246'results=index.search('',{'aroundLatLngViaIP':True,'X-Forwarded-For':ip})
1
2
3
4
5
6
7
8
9
10
11
/// '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.client.setHeader(withName:"X-Forwarded-For",to:"94.228.178.246")letquery=Query()query.aroundLatLngViaIP=trueindex.search(query,completionHandler:{(res,error)inprint(res)})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// '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.varconfiguration=newSearchConfig("AJ0P3S7DWQ","••••••••••••••••••••ce1181300d403d21311d5bca9ef1e6fb"){DefaultHeaders=newDictionary<string,string>{{"X-Forwarded-For","94.228.178.246"}}};SearchClientclient=newSearchClient(configuration);index.Search<T>(newQuery(""){AroundLatLngViaIP=true});
1
2
3
4
5
6
7
8
9
// '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(newQuery().setAroundLatLngViaIP(true),newRequestOptions().addExtraHeader("X-Forwarded-For","94.228.178.246"));
1
2
3
4
5
6
7
8
// "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.extraHeaders:=opt.ExtraHeaders(map[string]string{"X-Forwarded-For":"94.228.178.246",})res,err:=index.Search("",opt.AroundLatLngViaIP(true),extraHeaders)
1
2
3
4
5
6
7
8
9
10
11
// '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.client.execute{searchinto"myIndex"queryQuery(query=Some(""),aroundLatLngViaIP=Some(true))optionsRequestsOptions(extraHeaders=Some(Map("X-Forwarded-For"->"94.228.178.246")))}
1
2
3
4
5
6
7
8
9
10
/**
* '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.
*/valquery=query{aroundLatLngViaIP=true}index.search(query,requestOptions{headerForwardedFor("94.228.178.246")})