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

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
 

 

     

猜你喜欢

转载自mtou.iteye.com/blog/2035280