dsgft1486 2013-06-20 04:19
浏览 82

如何检查Solr中的空间搜索是否正常工作

I'm trying out the spatial search in Solr. What I did was to stop the service, then updated the schema.xml file for collection1 to include the following:

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

And then I started the service again to update the document in 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);
}

The curl responses were both ok and when I checked the results using this query:

http://localhost:8080/solr/select?q=ss_type:(business%20OR%20user%20OR%20blog)

It returned the results:

<double name="location_0_coordinate">49.9641523</double>
<double name="location_1_coordinate">10.1378365</double>

Now my problem is have I really set this up properly? And if yes, what will be the query that I can use to check if its really returning something once I use a spatial query like the one below:

http://localhost:8080/solr/select/?q=ss_type:(business%20OR%20user%20OR%20blog)&fq={!geofilt}&sfield=location&pt=49.9641523,10.1378365&d=0

My main problem here is that I don't really know what to supply for the reference point in order for it to return a record which already exists in the Solr index.

So for example if I have the following coordinates in the index:

49.9641523,10.1378365

What will be the spatial query that would return the specific record which has that coordinate. I tried to use d=0 but nothing is returned:

<response>
<result name="response" numFound="0" start="0"/>
</response>

Any ideas? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duannian4784 2013-06-20 04:36
    关注

    You should store the latitude and longitude in the same field, separated by comma. E.g. <latitude>,<longitude>

    Field type should be location.

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

    Pass in the following parameters in your query:

    pt - The Point to use as the center of the filter. Specified as a comma separated list of doubles. It is lat,lon.

    d - The distance (in KM) from the point to the outer edge of whatever is being used to filter on

    sfield - The field with coordinate.

    The following sample gespatial query will return all locations within 5km of the specified point 49.9641523, 10.1378365

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题