2013-06-20 36 views
0

我正在尝试Solr中的空间搜索。 我所做的就是要停止该服务,然后更新了schema.xml文件collection1包括以下内容:如何检查空间搜索是否在Solr中工作

<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> 

任何想法?提前致谢。

回答

1

您应该将纬度和经度存储在相同的字段中,并用逗号分隔。例如。 <latitude>,<longitude>

字段类型应为location

<dynamicField name="*_coordinate" type="location" indexed="true" stored="true"/> 

通在查询的参数如下:

pt - 该点作为滤波器的中心来使用。指定为逗号分隔的双打列表。它是经纬度。

d - 从点的距离(单位:公里)到任何被用来对

sfield过滤器的外边缘 - 以坐标的字段。

下面的示例gespatial查询将返回内的指定点49.9641523 5km的所有位置,10.1378365

http://localhost:8080/solr/select?q=*:*&fq={!geofilt}&pt=49.9641523,10.1378365&sfield=location_coordinate&d=5 
+0

所以像这样:'$ doc ['location_coordinate'] = $ location ['lat']。 ','。 $ location ['lng']'? – user225269

+0

我收到一个错误,如果我做了上面的一个:'错误添加字段'location_coordinate'='51.3223777,-0.1358427'msg =对于输入字符串:\“51.3223777,-0.1358427 \”“,”code“:400} }' – user225269

+0

字段类型必须是'location' – Stanley

0

好,我想通了,居然有一个在schema.xml文件中的默认动态字段,它允许你存储坐标,它的字段类型为location。所以,即使我有到schema.xml文件没有访问我仍然可以将其定义是这样的:

$doc['locm_store'] = $location['lat'] . ',' . $location['lng']; 

XML文档中的等效是:

<field name="locm_store">123,456</store> 

哪里123是纬度和456是经度。