David Rekowski schrieb:
> Hello list.
> 
> Please find attached, a proposal for geo distance search support of solr.
> 
<snip />
> 
> Afterthought: It looks as if ezcSearchSolrHandler might be extended by
> something like filterDistance($field, $value) without affecting the
> interface, which should eleminate the flaws I pointed out in 1) and 4).

Please find attached another version of the implementation. This time by
adding filterDistance() to the solr search handler. I consider it a
cleaner approach, not tainting eq() and only suffering from the long
argument list issue in search() and buildQuery().

(The solr_geoposition.php file is the same as before, only attached for
completeness' sake)

- David

-- 
David Rekowski
mailto:david.rekow...@gmx.de

Index: handlers/solr.php
===================================================================
--- handlers/solr.php   (revision 1180804)
+++ handlers/solr.php   (working copy)
@@ -340,7 +340,7 @@
      * @param array(string=>string) $order
      * @return array
      */
-    private function buildQuery( $queryWord, $defaultField, $searchFieldList = 
array(), $returnFieldList = array(), $highlightFieldList = array(), 
$facetFieldList = array(), $limit = null, $offset = false, $order = array() )
+    private function buildQuery( $queryWord, $defaultField, $searchFieldList = 
array(), $returnFieldList = array(), $highlightFieldList = array(), 
$facetFieldList = array(), $limit = null, $offset = false, $order = array(), 
$distanceField = '', $geoDistance = null )
     {
         if ( count( $searchFieldList ) > 0 )
         {
@@ -359,6 +359,13 @@
         $returnFieldList[] = 'score';
         $queryFlags['fl'] = join( ' ', $returnFieldList );
 
+        if ($distanceField != '') {
+            $queryFlags['fq'] = '{!geofilt}';
+            $queryFlags['pt'] = $geoDistance->lat.','.$geoDistance->lng;
+            $queryFlags['d'] = $geoDistance->distance;
+            $queryFlags['sfield'] = $distanceField;
+        }
+
         if ( count( $highlightFieldList ) )
         {
             $queryFlags['hl'] = 'true';
@@ -510,9 +517,9 @@
      * @param array(string=>string) $order
      * @return stdClass
      */
-    public function search( $queryWord, $defaultField, $searchFieldList = 
array(), $returnFieldList = array(), $highlightFieldList = array(), 
$facetFieldList = array(), $limit = null, $offset = 0, $order = array() )
+    public function search( $queryWord, $defaultField, $searchFieldList = 
array(), $returnFieldList = array(), $highlightFieldList = array(), 
$facetFieldList = array(), $limit = null, $offset = 0, $order = array(), 
$distanceField = '', $geoDistance = null )
     {
-        $result = $this->sendRawGetCommand( 'select', $this->buildQuery( 
$queryWord, $defaultField, $searchFieldList, $returnFieldList, 
$highlightFieldList, $facetFieldList, $limit, $offset, $order ) );
+        $result = $this->sendRawGetCommand( 'select', $this->buildQuery( 
$queryWord, $defaultField, $searchFieldList, $returnFieldList, 
$highlightFieldList, $facetFieldList, $limit, $offset, $order, $distanceField, 
$geoDistance ) );
         if ( ( $data = json_decode( $result ) ) === null )
         {
             throw new ezcSearchInvalidResultException( $result );
@@ -575,8 +582,10 @@
         $limit = $query->limit;
         $offset = $query->offset;
         $order = $query->orderByClauses;
+        $geoDistanceField = $query->geoDistanceField;
+        $geoDistanceValue = $query->geoDistanceValue;
 
-        $res = $this->search( $queryWord, '', array(), $resultFieldList, 
$highlightFieldList, $facetFieldList, $limit, $offset, $order );
+        $res = $this->search( $queryWord, '', array(), $resultFieldList, 
$highlightFieldList, $facetFieldList, $limit, $offset, $order, 
$geoDistanceField, $geoDistanceValue );
         return $this->createResponseFromData( $query->getDefinition(), $res );
     }
 
Index: abstraction/implementations/solr.php
===================================================================
--- abstraction/implementations/solr.php        (revision 1180804)
+++ abstraction/implementations/solr.php        (working copy)
@@ -441,5 +441,10 @@
         }
         return "$clause~";
     }
+
+    public function distanceFilter( $field, ezcSearchSolrGeoDistance 
$geoDistance ) {
+      $this->geoDistanceField = $field;
+      $this->geoDistanceValue = $geoDistance;
+    }
 }
 ?>
Index: document_definition.php
===================================================================
--- document_definition.php     (revision 1180804)
+++ document_definition.php     (working copy)
@@ -51,6 +51,8 @@
      */
     const BOOLEAN = 7;
 
+    const GEOPOSITION = 8;
+
     /**
      * Contains the document type - which is the same as the class name.
      *
<?php

class ezcSearchSolrGeoDistance {

  public $lat = 0.0;
  public $lng = 0.0;
  public $distance = 10;

  public function __construct($lat, $lng, $distance) {
    $this->lat = $lat;
    $this->lng = $lng;
    $this->distance = $distance;
  }
}

Reply via email to