---
title: "使用 Apache Lucene 和 Solr 进行位置感知搜索"
author: "sb122k"
date: "2012-07-11T14:37:38Z"
tags: ["Solr", "apache latLon"]
categories: ["互联网"]
slug: "apache-lucene-solr"
draft: false
lastmod: "2012-07-11T17:43:22Z"
id: 1585035
url: "https://sb122k.iteye.com/blog/1585035"
---

[http://wiki.apache.org/solr/SpatialSearch](http://wiki.apache.org/solr/SpatialSearch)

有三点：

1\. geofilt 一个点和距离 ， 指定查询字段。返回数据

 

...&q=\*:\*&fq={!geofilt sfield=store}&pt=45.15,-93.85&d=5

...&q=\*:\*&fq={!geofilt}&sfield=store&pt=45.15,-93.85&d=5

2.bbox 一个半径为d 边框范围，

...&q=\*:\*&fq={!bbox}&sfield=store&pt=45.15,-93.85&d=5

3.geodist 排序方法。默认按照距离排序，还有比较复杂的用法没有研究，注意：

?q=\*&fq={!geofilt%20sfield=baidu\_geo}&pt=45.15,-93.85&d=5&sort=geodist()%20asc 这是错的，会报 sort param could not be parsed as a query, and is not a field that exists in the index: geodist()

?q=\*&fq={!geofilt}&sfield=baidu\_geo&pt=30.648694,104.091852&d=5&sort=geodist()%20asc  这是对的

[http://wiki.apache.org/solr/SpatialSearchDev](http://wiki.apache.org/solr/SpatialSearchDev) 有配置的实例，但是注意：

#### Example

 

```
<fieldType name="latLon" class="solr.LatLonType" subFieldSuffix="_latLon"/>
...
<field name="store_lat_lon" type="latLon" indexed="true" stored="true"/>
...
<dynamicField name="*_latLon" type="double" indexed="true" stored="false" multiValued="true"/>
```

这是错的,[http://wiki.apache.org/solr/SpatialSearch](http://wiki.apache.org/solr/SpatialSearch) 有描叙 ：

The LatLonType is the current default spatial field. Values for this type are of the form `latitude,longitude`, although behind the scenes, the latitude and longitude are indexed as separate numbers. Fields using LatLonType must be single valued (i.e. multiValued="false"). This field type does distance calculations based on Great Circle (haversine).

In addition to `geofilt`, `geodist` and `bbox`, the LatLonType supports field queries such as `field:10,20` and range queries such as `field:[10,20 TO 30,40]`.

正确的 schema.xml 配置是：

&lt;fieldtype name="latLon" class="solr.LatLonType"/&gt;

&lt;field name="geo" type="latLon"  stored="true" indexed="true"/&gt;

&lt;dynamicField name="\*\_latLon" type="double" indexed="true" stored="false" multiValued="false"/&gt;

坐标系转换

[http://www.cnblogs.com/jz1108/archive/2011/10/11/2207245.html](http://www.cnblogs.com/jz1108/archive/2011/10/11/2207245.html)

因为中国法律规定， 原始gps地图坐标不能得到， 而 goods，baidu, sogo 的参考坐标系各不相同。我存的是百度坐标系。数据库和索引里可支持多种坐标系。

百度webservice 接口 ： [http://dev.baidu.com/wiki/mapws/index.php?title=Place](http://dev.baidu.com/wiki/mapws/index.php?title=Place)