Solr4.7实现LBS(地理位置搜索)

来源:互联网 发布:媒体cms 编辑:程序博客网 时间:2024/05/14 10:23

solr实现LBS(地理位置搜索)有两种方式:Cartesian Tiers 笛卡尔层  和  GeoHash算法,本文主要介绍GeoHash算法实现。

实现步骤:

1. 修改schema.xml文件

<fieldtype name="geohash" class="solr.GeoHashField"/><field name="geohashTest" type="geohash" indexed="true" stored="true"/>

2.sorlJ创建索引

double latitude = 39.869664;double longitude = 116.38325;Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  SolrInputDocument doc1 = new SolrInputDocument(); doc1.addField("geohashTest",latitude+","+longitude);docs.add(doc1); server.add(docs);server.commit();

      注意 :经纬度格式:纬度,经度  

3.检索

   (1)sorlJ查询

SolrQuery query = new SolrQuery();query.addFilterQuery("{!geofilt}"); query.set("q", "*:*"); query.set("d","10"); query.set("sfield","geohashTest"); query.set("pt","40.2323,116.12"); QueryResponse rsp = server.query(query);

   (2)控制台查询

        

   
   
  
   (3)检索字段说明

      sfield:geohash对应的域名

     pt:经纬度字符串 (纬度,经度)

     d=球面距离 
  
4.其它

关于 Cartesian Tiers 笛卡尔层  和  GeoHash算法  实现原理可以参考:http://blog.csdn.net/a221133/article/details/14525197 
 

0 0