Solr 空间查询
URL:
http://127.0.0.1:8080/solr/usercard/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on&&fq={!geofilt%20sfield=location}&pt=39.885,116.384034&d=1
OfficialDoc:
For example, to sort your results by ascending distance, enter ...&q=*:*&fq={!geofilt}&sfield=store&pt
. =45.15,-93.85&d=50&sort=geodist asc
To return the distance as the document score, enter ...&q={!func}geodist()&sfield=store&pt=45.15,-. 93.85&sort=score+asc
精确按照点和距离搜索,geofilt函数为圆形计算:
&q={!func}geodist()&fq={!geofilt}&sfield=latilongitude&pt=45.15,93.85&d=5&sort=score+asc
geofilt函数为方形距离计算:
&q={!func}geodist()&fq={!bbox}&sfield=latilongitude&pt=45.15,93.85&d=5&sort=score+asc
SolrJ:
solrQuery.setQuery("{!func}geodist()"); // 以名称为score作为距离返回 使sfield及pt参数生效
// solrQuery.set("spatial", true);
solrQuery.addFilterQuery("{!geofilt}"); // 限定距离,使"d"参数生效
solrQuery.set("sfield", "latilongitude"); // 设置查询的经纬度字段
solrQuery.set("pt", latilongitude); // 设置查询 点
solrQuery.set("d", distance); // 设置查询半径
solrQuery.setSort("score", SolrQuery.ORDER.asc); // score在{!func}geodist()函数下含义为距离,按照最近距离排序
// solrQuery.setSort("geodist()", SolrQuery.ORDER.asc);//被setSort("score", SolrQuery.ORDER.asc)替代
// 追加其它条件
// solrQuery.addFilterQuery(queryString); // 可有可没有...
// 设置返回字段,根据需求定制, score为距离
query.setFields("latilongitude","score");
参考: http://wiki.apache.org/solr/SpatialSearch