使用PHP的Spinx geodistance不会返回任何结果

时间:2013-05-06 20:01:23

标签: php sphinx

我是Sphinx搜索的新手。我阅读了手册和一些关于如何让Geodist工作并且没有结果的方法。我已经搜索了互联网和StackOverflow的解决方案,并尝试了我读过的所有内容,但仍然无法让Sphinx返回任何结果。这就是我所拥有的。任何帮助将不胜感激。

表:

带有lat和lng的位置表由Google的地理编码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 ;

查询“SELECT sidlatitudelongitude FROM locations”返回:

(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
}

我的PHP代码:

$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');

结果:

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'
)

2 个答案:

答案 0 :(得分:1)

这里已经回答: http://sphinxsearch.com/forum/view.html?id=11038

基本上,sphinx索引至少需要一个全文字段才能起作用。仅由属性构成的索引不可用。

最新版本的Sphinx会在索引时发出错误,但早期版本只是默默地创建了一个非功能性索引。

答案 1 :(得分:0)

尝试以下:

替换您的代码

$Sphinx->SetGeoAnchor('latitude', 'longitude', deg2rad(floatval($lat)), deg2rad(floatval($lng)));
$Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC');


$Sphinx->SetFilterFloatRange('@geodist', 0.0, floatval($radius));

以下

$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);