Solr 空间搜索配置
1. 在solr 目录下的找到conf 文件夹下的schema.xml .
<fields>
<!-- 在fields 元素中添加如下代码 -->
<field name="store_lat_lon" type="location" indexed="true" stored="true"/>
<dynamicField name="*_coordinate" type="double" indexed="true" stored="false" multiValued="false"/>
</fields>
2. 在solr 目录下的找到conf 文件夹下的db-data.config.xml .
把dataConfig 标签内的script 标签的内容做如下修改。
<script><![CDATA[
function f1(row){
row.put('$docBoost', 1);
var lat_coordinate = row.get("GPS_Y ");
var log_coordinate = row.get("GPS_X ");
if( lat_coordinate != null && log_coordinate != null ){
if( lat_coordinate*1 >= -90 && lat_coordinate*1 <= 90 ) {
var store_lat_lon = lat_coordinate + "," + log_coordinate;
var arr = new java.util.ArrayList()
arr.add(lat_coordinate);
arr.add(log_coordinate);
row.put("store_lat_lon",store_lat_lon);
row.put("item",arr);
}
}
return row;
}
]]></script>
3. 重新启动solr.
重建索引:http://localhost:8080/solr/ dataimport?command=full-import
4. 查询示例:
http://localhost:8080/solr/select/?q=*:*&fq={!geofilt}&sfield=store_lat_lon&pt=86.13909901,41.770741&d=5&fl=name,gps_x,gps_y,store_lat_lon&sort=geodist() asc
参数
描述
示例
d
距离(单位:km )
&d=10.0
pt
搜索过滤的中心点,纬度,经度坐标点
&pt=33.4,29.0
sfield
空间搜索类型的字段(eg: solr.LatLonType )
&sfield=store_lat_lon
fq
设置查询过滤器
fq={!geofilt}
sort
根据字段或者函数排序
&sort=geodist() asc
geofilt 函数: 使结果集约束在到中心点位置的最大距离(km )圆形区域内。
geodist 函数: 计算两点之间的距离。
注意: solr 3.4 版本以上才支持 geodist 等空间搜索函数。
参考:http://wiki.apache.org/solr/SpatialSearch
Script 形式的转换只在JDK1.6 版本或以上才支持,下面是通用的空间搜索转换的实现,支持JDK1.5 及其以上版本。
新建一个类如下所示,该类可以不继承或实现任何其他接口或者抽象类,但是transformRow 方法必须定义成下面所示,SOLR dataimport 插件会通过反射的方式去调用这个方法。
package com.besttone.transformer;
import java.util.ArrayList;
import java.util.Map;
public class LocationTransformer {
// 参考资料 http://wiki.apache.org/solr/DIHCustomTransformer
public Object transformRow(Map<String, Object> row) {
// TODO Auto-generated method stub
row.put("$docBoost", 1);
Object lat_coordinate = row.get("GPS_Y");
Object log_coordinate = row.get("GPS_X");
if (lat_coordinate != null && log_coordinate != null) {
if (Double.parseDouble(lat_coordinate.toString()) * 1 >= -90
&& Double.parseDouble(lat_coordinate.toString()) * 1 <= 90) {
String store_lat_lon = lat_coordinate.toString() + ","
+ log_coordinate.toString();
ArrayList arr = new ArrayList();
arr.add(lat_coordinate);
arr.add(log_coordinate);
row.put("store_lat_lon", store_lat_lon);
row.put("item", arr);
}
}
return row;
}
}
第二步在db-data-config.xml 中的entity 节点上配置转换类:
<entity name="zbs" transformer="com.besttone.transformer.LocationTransformer" ………..
这个类必须以全路径配置。
第三步将上面的类用jdk1.5 编译并打成JAR 包,放在solr/home 的lib 下面, 然后再solr.xml 中配置全局共享lib:
<solr persistent="false" sharedLib="lib">
这样dataimport 就能找到这个转换类,并通过反射的方式调用transformRow 实现转换.
也可以继承Transformer 抽象类来实现更高级的自定义转换类应用。
参考资料 http://wiki.apache.org/solr/DIHCustomTransformer
若要返回距离,4.0版本可以参照第一种方法,4.0以下版本可参照下面第二种方法,score返回的就是距离。
Returning the distance
Solr4.0
You can use the pseudo-field feature to return the distance along with the stored fields of each document by addingfl=geodist() to the request. Use an alias likefl=_dist_:geodist() to make the distance come back in the_dist_ pseudo-field instead. Here is an example of sorting by distance ascending and returning the distance for each document in_dist_ .
...&q=*:*&sfield=store&pt=45.15,-93.85&sort=geodist() asc&fl=_dist_:geodist()
As a temporary workaround for older Solr versions, it's possible to obtain distances by using geodist or geofilt as the only scoring part of the main query.
...&sfield=store&pt=45.15,-93.85&sort=score%20asc&q={!func}geodist()
若要显示距离并按某些字段比如名字来查询的话,那查询条件只能写在fq中,如果写在q中则会影响得分,导致score字段返回的不是距离,写法如下:
http://localhost:8089/solr/select/?q={!func}geodist()&fq={!geofilt}&fq=address:兴华南街&sfield=store_lat_lon&pt=41.78101895,123.36067849&d=5&sort=score%20asc&fl=score
查找5公里范围内并且地址在兴华南街的数据(fq=address:兴华南街 )。
版权声明:本文为博主原创文章,未经博主允许不得转载。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com