solr空间查询

本文详细介绍了如何在Solr中配置空间搜索功能,包括定义坐标字段、修改配置文件及重启Solr服务等步骤,并提供了查询示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于在工作中使用到了solr的空间搜索功能,在此记录过程。

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

修改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.LatLonTypesfield=latlon
fq设置查询过滤器fq={!geofilt}
sort根据字段或者函数排序sort=geodist() asc


geofilt 函数:  使结果集约束在到中心点位置的最大距离(km)圆形区域内。

geodist 函数:   计算两点之间的距离。


转载于:https://my.oschina.net/jayhu/blog/368196

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值