您的位置:首页 > 其它

solr空间查询

2015-02-02 14:28 423 查看
由于在工作中使用到了solr的空间搜索功能,在此记录过程。

需要定义一个坐标形式的字段

修改schema.xml文件

添加fieldType

<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>

添加field

<field name="latlon" type="location" indexed="true" stored="true"/>

2.修改data-config.xml

如,s_latitude_s 字段为经度,s_longitude_s 字段为维度

在data-config.xml加入如下脚本

<script><![CDATA[
function f1(row){
var lat_coordinate = row.get("s_latitude_s");
var log_coordinate = row.get("s_longitude_s");
if(!isNaN(lat_coordinate) && !isNaN(log_coordinate) && lat_coordinate*1 >= -90  &&  lat_coordinate*1 <= 90 ) {
var store_lat_lon = lat_coordinate + "," + log_coordinate;
row.put("latlon",store_lat_lon);
}else{
row.put("latlon","0,0");
}
return row;
}
]]></script>

在entity中引用该脚本 transformer="script:f1"

3.重启solr

4.查询示例

http://127.0.0.1:6230/goods/select/?q=*:*&fq={!geofilt}&sfield=latlon&pt=29.546327,106.530559&d=5&sort=geodist()%20asc

参数
描述示例
d范围,单位:千米d=5
pt
搜索过滤的中心点,纬度,经度坐标点pt=29.5454,106.5305
sfield空间搜索类型的字段(eg: solr.LatLonType)sfield=latlon
fq设置查询过滤器fq={!geofilt}
sort根据字段或者函数排序sort=geodist() asc
geofilt 函数: 使结果集约束在到中心点位置的最大距离(km)圆形区域内。
geodist 函数: 计算两点之间的距离。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  solr 空间查询