我正在尝试Solr中的空间搜索。我所做的是停止服务,然后更新collection1
的schema.xml
文件以包含以下内容:
<fieldType name="location" class="solr.LatLonType" subFieldSuffix="_coordinate"/>
<dynamicField name="*_coordinate" type="tdouble" indexed="true" stored="true"/>
<field name="location" type="location" indexed="true" stored="true"/>
<field name="location_0_coordinate" type="double" indexed="true" stored="true"/>
<field name="location_1_coordinate" type="double" indexed="true" stored="true"/>
然后,我再次启动该服务以更新Solr中的文档:
$local_select = 'http://localhost:8080/solr/select?q=ss_type:(business%20OR%20user%20OR%20blog)&wt=json';
$url = $local_select;
$data = file_get_contents($url);
$r_data = json_decode($data, true);
$docs = $r_data['response']['docs'];
if(!empty($docs)){
foreach($docs as $doc){
if(!empty($doc['tm_field_business_address:postal_code'][0])){
$postal_code = urlencode($doc['tm_field_business_address:postal_code'][0]);
$api = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $postal_code . '&sensor=false';
$api_results = file_get_contents($api);
$data = json_decode($api_results, true);
if($data['status'] == 'OK'){
$location = $data['results'][0]['geometry']['location'];
unset($doc['_version_']);
$doc['location_0_coordinate'] = $location['lat'];
$doc['location_1_coordinate'] = $location['lng'];
$new_docs[] = $doc;
}
}
}
}
$local_update = 'http://localhost:8080/solr/update/json';
$local_commit = 'http://localhost:8080/solr/update?commit=true';
//update the solr index
if(!empty($new_docs)){
$json_doc = json_encode($new_docs);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array("Content-type:application/json"),
CURLOPT_URL => $local_update,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $json_doc,
));
$update_response = curl_exec($curl);
curl_close($curl);
//commit the changes
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $local_commit
));
$commit_response = curl_exec($curl);
}
curl响应都是ok的,当我使用以下查询检查结果时:
http://localhost:8080/solr/select?q=ss_type:(business%20OR%20user%20OR%20blog)
它返回结果:
<double name="location_0_coordinate">49.9641523</double>
<double name="location_1_coordinate">10.1378365</double>
现在我的问题是,我真的正确地设置了吗?如果是,当我使用像下面这样的空间查询时,我可以使用什么查询来检查它是否真的返回了什么东西:
http://localhost:8080/solr/select/?q=ss_type:(business%20OR%20user%20OR%20blog)&fq={!geofilt}&sfield=location&pt=49.9641523,10.1378365&d=0
这里我的主要问题是,我真的不知道为参考点提供什么,以便它返回Solr索引中已经存在的记录。
例如,如果我在索引中有以下坐标:
49.9641523,10.1378365
返回具有该坐标的特定记录的空间查询将是什么。我尝试使用d=0
,但没有返回任何内容:
<response>
<result name="response" numFound="0" start="0"/>
</response>
有什么想法吗?提前谢谢。
发布于 2013-06-20 04:36:26
您应该将纬度和经度存储在同一个字段中,用逗号分隔。例如,<latitude>,<longitude>
字段类型应为location
。
<dynamicField name="*_coordinate" type="location" indexed="true" stored="true"/>
在查询中传入以下参数:
pt
-用作过滤器中心的点。指定为逗号分隔的双精度列表。它是lat,lon。
d
-从点到任何用于过滤的外边的距离(以千米为单位)
sfield
-带坐标的字段。
以下示例地理空间查询将返回指定点49.9641523,10.1378365 5 5km内的所有位置
http://localhost:8080/solr/select?q=*:*&fq={!geofilt}&pt=49.9641523,10.1378365&sfield=location_coordinate&d=5
发布于 2013-06-20 06:08:25
好的,我想出来了,在schema.xml
文件中有一个默认的动态字段,它允许你存储坐标,它的字段类型是location
。因此,即使我不能访问schema.xml
文件,我仍然可以这样定义它:
$doc['locm_store'] = $location['lat'] . ',' . $location['lng'];
xml文档中的等价物是:
<field name="locm_store">123,456</store>
其中123
是纬度,456
是经度。
https://stackoverflow.com/questions/17205189
复制相似问题