我正在尝试 Solr 中的空间搜索。我所做的是停止服务,然后更新schema.xml
文件collection1
以包含以下内容:
<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);
}
卷曲响应都很好,当我使用此查询检查结果时:
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>
有任何想法吗?提前致谢。