I'm new to Sphinx search. I read the manual and a few how-tos on how to get Geodist working and am getting no results. I've scoured the internet and StackOverflow for solutions and tried everything I've read, and I still can't get Sphinx to return any results. Here's what I've got. Any help would be appreciated.
The Table:
The locations table with lat and lng is populated by Google's geocode API.
CREATE TABLE IF NOT EXISTS `locations` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(75) NOT NULL,
`latitude` float(10,6) DEFAULT NULL,
`longitude` float(10,6) DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
The Query “SELECT sid
, latitude
, longitude
FROM locations
” returns:
(1, 29.8986, -95.4831),
(3, 30, -95.25),
(4, 47.7313, -122.177),
(5, 33.8811, -98.4325),
(6, 52.12, -0.474118),
(7, 30.0134, -95.4576),
(8, 29.8702, -95.5705),
(9, 39.8691, 116.461),
(10, 29.8159, -95.9282),
(11, 42.9572, -83.8272),
(12, 29.7887, -95.6587),
(13, 29.7036, -95.5511),
(14, 33.5873, -102.378),
(15, 29.6157, -98.4823),
(16, 43.649, -79.3838),
(17, 35.0512, -97.937),
(18, 31.8823, -102.317),
(19, 29.9379, -95.4114),
(20, 29.884, -95.5827),
(21, 40.7971, -79.744),
(22, 30.0218, -95.4069),
(23, 32.4501, -97.2549),
(24, 42.7305, -94.6788),
(25, 30.239, -96.4416);
Sphinx.conf
source Location
{
type = mysql
sql_host = localhost
sql_user = sphinx
sql_pass = password
sql_db = database
sql_port = 3306
sql_query_pre = set names utf8
sql_query_pre = set session query_cache_type=OFF
sql_query = select sid, radians(longitude) as longitude, radians(latitude) as latitude,
name from locations
sql_attr_float = longitude
sql_attr_float = latitude
sql_attr_uint = sid
#my unique id is sid rather than id
sql_ranged_throttle = 0
sql_query_info = select * from locations where sid = $id
}
{
source = Location
path = /var/lib/sphinxsearch/data/Location
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
charset_type = utf-8
charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
ignore_chars = U+00AD
html_strip = 0
enable_star = 0
}
My PHP Code:
$Sphinx = new SphinxClient();
$Sphinx->setServer(SPINX_SERVER, SPINX_PORT);
$Sphinx->SetConnectTimeout(1);
$Sphinx->SetArrayResult(true);
$Sphinx->SetMatchMode(SPH_MATCH_ALL);
$Sphinx->SetLimits(0,100);
//The lat and lng input is typically provided by google geocode api but I wrote these in static to keep things simple.
$lat = (float) 29.7601927;
$lng = (float) -95.3693896;
$radius = 10000;
$Sphinx->SetGeoAnchor('latitude', 'longitude', deg2rad(floatval($lat)),
deg2rad(floatval($lng)));
$Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC');
$Sphinx->SetFilterFloatRange('@geodist', 0.0, floatval($radius));
$result = $Sphinx->Query('', 'Location');
Results:
Array(
'error' => '',
'warning' => '',
'status' => (int) 0,
'fields' => array(),
'attrs' => array(
'longitude' => (int) 5,
'latitude' => (int) 5,
'@geodist' => (int) 5
),
'total' => '0',
'total_found' => '0',
'time' => '0.000'
)
Try below:
Replace your code
$Sphinx->SetGeoAnchor('latitude', 'longitude', deg2rad(floatval($lat)), deg2rad(floatval($lng)));
$Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC');
$Sphinx->SetFilterFloatRange('@geodist', 0.0, floatval($radius));
with below
$Sphinx->SetGeoAnchor('latitude', 'longitude', (float) deg2rad($lat), (float) deg2rad($lng));
$Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC');
$circle = (float) $_radius * 1.61;
$Sphinx->SetFilterFloatRange('@geodist', 0.0, $circle);
This has been answered here: http://sphinxsearch.com/forum/view.html?id=11038
Basically, a sphinx index needs at least one full-text field to be functional. An index solely made of attributes is not usable.
Latest version of Sphinx will emit an error at indexing time, but earlier versions just silently created a non-functional index.