solr 实现对经纬度的查询

1、solr版本

solr7+

2、solr 经纬度查询的方式

  1. 使用LatLonType(用于平面坐标,而不是大地坐标)
  2. SpatialRecursivePrefixTreeFieldType(缩写为RPT)
  3. BBoxField(用于边界索引查询)

 

2.1  使用 LatLonPointSpatialField

创建一个索引库,名叫newCore

在 conf 文件夹中找到  managed-schema 文件, 新增 以下域字段 

<field name="loc" type="location" indexed="true" stored="true" required="true" multiValued="false" /> 
<field name="id" type="string" indexed="true" stored="true" required="false" multiValued="false" />

 

测试代码  新增坐标 到solr服务器中  

	@Autowired
	private SolrClient solrClient;

    @Test
	public void addDoc11() throws SolrServerException, IOException {
		
		Random r = new Random();// 随机数
		Collection<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
		for (int i = 0; i < 10000; i++) {
			SolrInputDocument document = new SolrInputDocument();
			String id = i + "_solr";
			// 纬度范围是-90~90,经度范围-180~180
			String loc = (r.nextInt(180) - 90) + "," + (r.nextInt(360) - 180);
			document.addField("id", id);
			document.addField("loc", loc);
			docList.add(document);
		}
		solrClient.add(docList);
		solrClient.commit();
	}
	

 

生成以下数据

使用浏览器进行访问 

#距离经纬度 45,94 1000km 所有的经纬度 降序排列
http://localhost:8983/solr/newcore/select?q=*:*&fq={!geofilt}&sfield=loc&pt=45,94&d=1000&start=0&rows=10&sort=geodist()%20asc

注意的是,此数据个是 (经度,纬度) 例如 (33,72)

参考资料 1:https://www.cnblogs.com/tony-zt/p/9260017.html   solr7 的安装与使用

参考资料2 :https://blog.csdn.net/dmw412724/article/details/86520569 使用以上方式 进行sorl 的经纬查询

 

2、使用SpatialRecursivePrefixTreeFieldType 

在 conf 文件夹中找到  managed-schema 文件, 新增 以下域字段 

    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
	<field name="station_address" type="text_general" indexed="true" stored="true"/>
	<field name="station_position" type="location_rpt" indexed="true" stored="true"/>

location_rpt 的数据类型是

<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
        geo="true" distErrPct="0.025" maxDistErr="0.000009" distanceUnits="kilometers" />

测试代码

	@Autowired
	private SolrClient solrClient;

    @Test
	public void addDocRpt() throws SolrServerException, IOException {
		SolrInputDocument doc = new SolrInputDocument();  
        doc.addField("id","1");  
        doc.addField("station_address", "北京");
        String posString = "112.0541"+" "+"30.96029";
        doc.addField("station_position", posString);
        solrClient.add(doc);
        solrClient.commit();
	}

此数据入索引库的格式 是  (122.0122‘需要一个空格’30.9994)  (112.0033 34.0008)

使用浏览器进行访问

# 查询与 112.0423 30.97878 经纬度距离 3km 范围的所有点
http://localhost:8983/solr/newcore/select?q=*:*&fq={!geofilt}&sfield=station_position&pt=112.0423%2030.97878&d=3&start=0&rows=10&sort=geodist()%20asc

 

查询结果

参考资料1:http://www.itkeyword.com/doc/2543881900536538500/apache%20latLon-solr  设置排序

参考资料2:https://blog.csdn.net/larger5/article/details/79768438  solr的操作

参考资料3:http://www.yanglajiao.com/article/u010346953/69934323 solr的操作

参考资料4:https://blog.csdn.net/u014508939/article/details/85165807 使用 SpatialRecursivePrefixTreeFieldType 

参考资料5:http://www.hhlink.com/%E7%BB%8F%E7%BA%AC%E5%BA%A6/ 经纬度计算

pom文件

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-solr</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		
	</dependencies>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古月_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值