Spatial Search 緯度 経度 を使った空間検索

現在開発中の Solr ( solr-1.5-dev )では緯度経度を使った空間検索ができるようになっています。


銀座駅 半径1Kmの検索例

http://192.168.11.42:8080/solr/select/?q=_val_:"recip(hsin(6378.137, vector(rad(WGS_LATLON_0_latLon), rad(WGS_LATLON_1_latLon)), vector(rad(35.671989),rad(139.763965))),1,1,0)"&fl=*,score&rows=100&fq={!sfilt fl=WGS_LATLON }&pt=35.671989,139.763965&d=1
  • 赤道半径の指定を6378.137 (Km)にすることで中心点からの距離の指定や検索結果で得られるスコアをKメートル単位にすることができます。デフォルトはマイル単位です。
  • 銀座駅の緯度経度 : 35.671989, 139.763965
  • 半径指定 : d = 1(Km) の部分
  • スコアは recip()によってKメートル単位の逆数とすることで近い順にソートしてます。1/スコアで中心点からの距離が算出できます。
  • recip()を外すと遠い順にソートされます。スコアはそのまま中心点からの距離です。
  • 緯度経度を管理するフィールドタイプ : LatLonType
    <fieldType name="latLon" class="solr.LatLonType"  subFieldSuffix="_latLon"/>
  • 緯度経度を管理するフィールド : WGS_LATLON
<field name="WGS_LATLON" type="latLon" indexed="true" stored="true"/>


近いうちにもう少し分かり易く整理するつもりです。


参考ページ
http://wiki.apache.org/solr/SpatialSearch
http://lucene.apache.org/solr/api/org/apache/solr/search/SpatialFilterQParser.html
http://www.gossamer-threads.com/lists/lucene/java-dev/99960
http://stackoverflow.com/questions/2361030/syntax-for-hsin-query-in-solr
http://lucene-eurocon.org/slides/Solr-15-and-Beyond_Yonik-Seely.pdf
http://www.math.montana.edu/frankw/ccp/cases/Global-Positioning/round-earth/learn.htm