设为首页 收藏本站
查看: 1038|回复: 0

[经验分享] Solr 空间搜索配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-12 09:05:25 | 显示全部楼层 |阅读模式
                     Solr 空间搜索配置
  
  1.          solr目录下的找到conf文件夹下的schema.xml.
     
         <fields>   
              <!-- fields元素中添加如下代码 -->   
              <field name=&quot;store_lat_lon&quot; type=&quot;location&quot; indexed=&quot;true&quot; stored=&quot;true&quot;/>
              <dynamicField name=&quot;*_coordinate&quot; type=&quot;double&quot; indexed=&quot;true&quot; stored=&quot;false&quot; multiValued=&quot;false&quot;/>
  
        </fields>
  
  
  2.          solr目录下的找到conf文件夹下的db-data.config.xml .
      dataConfig标签内的script标签的内容做如下修改。
     
      <script><![CDATA[
                  function f1(row){
                      row.put('$docBoost', 1);
                      var lat_coordinate = row.get(&quot;GPS_Y &quot;);
                      var log_coordinate = row.get(&quot;GPS_X &quot;);
                      if( lat_coordinate != null &&  log_coordinate != null ){
                        if(  lat_coordinate*1 >= -90  &&  lat_coordinate*1 <= 90 ) {
                               var store_lat_lon = lat_coordinate &#43; &quot;,&quot; &#43; log_coordinate;
                                   var arr = new java.util.ArrayList()
                                   arr.add(lat_coordinate);
                                   arr.add(log_coordinate);
                                   row.put(&quot;store_lat_lon&quot;,store_lat_lon);
                                   row.put(&quot;item&quot;,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(&quot;$docBoost&quot;, 1);
                Object lat_coordinate = row.get(&quot;GPS_Y&quot;);
                Object log_coordinate = row.get(&quot;GPS_X&quot;);
  
                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() &#43; &quot;,&quot;
                                            &#43; log_coordinate.toString();
                              ArrayList arr = new ArrayList();
                              arr.add(lat_coordinate);
                              arr.add(log_coordinate);
                              row.put(&quot;store_lat_lon&quot;, store_lat_lon);
                              row.put(&quot;item&quot;, arr);
                       }
                }
                return row;
         }
  }
  
  第二步在db-data-config.xml中的entity节点上配置转换类:
  <entity name=&quot;zbs&quot; transformer=&quot;com.besttone.transformer.LocationTransformer&quot;  ………..
  这个类必须以全路径配置。
  
  第三步将上面的类用jdk1.5编译并打成JAR包,放在solr/homelib下面,然后再solr.xml中配置全局共享lib:
  <solr persistent=&quot;false&quot; sharedLib=&quot;lib&quot;>
  
  这样dataimport就能找到这个转换类,并通过反射的方式调用transformRow实现转换.
  
  也可以继承Transformer抽象类来实现更高级的自定义转换类应用。
  
  参考资料 http://wiki.apache.org/solr/DIHCustomTransformer
  
  若要返回距离,4.0版本可以参照第一种方法,4.0以下版本可参照下面第二种方法,score返回的就是距离。
Returning the distance
  
DSC0000.png 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

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-138153-1-1.html 上篇帖子: Solr之SolrCloud配置mmseg4j同义词 下篇帖子: 在Solr4.10配置IKAnalyzer 同义词、扩展词库、停顿词详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表