简体   繁体   English

Solr查询计算两点之间的地理距离

[英]Solr query to calculate geo distance between two points

I have latitude and longitude of two business locations and want to calculate the distance between them.我有两个营业地点的经纬度,想计算它们之间的距离。 How do i do it using solr query.我如何使用 solr 查询来做到这一点。

I got the query to get all the nearest locations from a point ( latitude , longitude ).我得到了从一个点(纬度,经度)获取所有最近位置的查询。

But want a query to get distance between two points.但是想要查询以获取两点之间的距离。

Below is the sample code snippet to get the locations from a point下面是从一个点获取位置的示例代码片段

// sample location // 样本位置

location = new double[]{lat, long}; 

SolrQuery query = new SolrQuery("{!func}geodist()");  
query.set("fq","query");  
query.set("sfield", "location");  
query.set("pt", location );  
query.setSortField("score", ORDER.asc);  
query.setFields("score", "id");  
SolrDocumentList list = solrServer.query(query).getResults();  

To return distance you should add geodist() function to fl field list query field like that:要返回距离,您应该将geodist()函数添加到fl字段列表查询字段,如下所示:

query.set("fl","*,distance:geodist()")

"distance" is the field name to return geodist() result in. “距离”是要返回geodist()结果的字段名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM