Services/SearchService.php
changeset 68 e7384fb35f7a
parent 57 186c4121c7b3
child 75 ca2a145e67f3
--- a/Services/SearchService.php	Mon Dec 26 22:53:50 2011 +0100
+++ b/Services/SearchService.php	Mon Jan 23 00:48:55 2012 +0100
@@ -34,6 +34,10 @@
     
     private $doctrine;
     
+    /**
+     * doctrine object getter
+     * @return object
+     */
     public function getDoctrine()
     {
         if(is_null($this->doctrine))
@@ -42,87 +46,108 @@
         }
         return $this->doctrine;
     }
+    
+    
     /**
      *
      * Enter description here ...
-     * @param string $value
-     * @param array $conditions
-     * @param array $fields
+     * @param mixed $value : either a string or the list of fields to search into ; format : ['<field_name>' => ['weight'=><relative weight of the field>, 'value' => value]]
+     * @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operator is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value)
+     * @param array $fields : The field of field names to export, one of the list specified in the configuration file
+     * @return array []
      */
-    public function search($value, array $conditions, array $fields=null)
+    public function search($value, array $conditions = null, array $fields = null)
     {
+        if(is_null($value) || (is_string($value) && strlen($value) == 0) || count($value)==0 ) {
+            return null;
+        }
+        
+        $fielddeflist = $this->getContainer()->getParameter("wiki_tag.fields");
+        $fieldquery = array();
+        
+        // build filed query
+        if(is_string($value)) {
+            foreach ($fielddeflist as $fieldname => $fielddef) {
+                if(!is_null($fielddef) && isset($fielddef['weight'])) {
+                    $weight = $fielddef['weight'];
+                }
+                else
+                {
+                    $weight = 1.0;
+                }
+                $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
+            }
+        }
+        else {
+            foreach ($value as $fieldname => $fielddef) {
+                if(is_null($fielddef) || !isset($fielddef['value']) || strlen($fielddef['value']) == 0) {
+                    continue;
+                }
+                $valuefield = $fielddef['value'];
+                if(isset($fielddef['weight'])) {
+                    $weight = $fielddef['weight'];
+                }
+                else
+                {
+                    $weight = 1.0;
+                }
+                
+                $fieldquery[] = array("columns"=>$fieldname, "value"=>$valuefield, "weight"=>$weight);
+            }
+        }
+        
+        // buildf
         if(is_null($fields))
         {
-            $fields = $this->getContainer()->getParameter("wiki_tag.fields");
+            $fieldnamelist = array();
+            foreach ($fielddeflist as $fieldname => $fielddef) {
+                $fieldnamelist[] = $fieldname;
+            }
         }
-        $doctrine = $this->getContainer()->get('doctrine');
-        $res = $doctrine->getRepository('WikiTagBundle:Document');
-        $fieldquery = array();
-        foreach ($fields as $fieldname => $fielddef) {
-            if(isset($fielddef['weight']))
-            {
-                $weight = $fielddef['weight'];
-            }
-            else
-            {
-                $weight = 1.0;
-            }
-            $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
+        else {
+            $fieldnamelist = $fields;
         }
         
-        $score_res = $res->search($fieldquery, $conditions);
+        $doctrine = $this->getContainer()->get('doctrine');
+        $rep = $doctrine->getRepository('WikiTagBundle:Document');
+                
+        $score_res = $rep->search($fieldquery, $conditions, $fieldnamelist);
+
+        $res = array();
+        foreach($score_res as $single_res) {
+            $res_entry = array();
+            $res_entry['host_doc_id'] = $single_res[0]->getExternalId()->getId();
+            $res_entry['wikitag_doc_id'] = $single_res[0]->getId();
+            foreach($fieldnamelist as $fieldname) {
+                $accessor_name = "get".ucfirst($fieldname);
+                $res_entry[$fieldname] = $single_res[0]->{$accessor_name}();
+            }
+            $res_entry['_score'] = $single_res['score'];
+            $res_entry['wikitag_doc'] = $single_res[0];
+            $res[] = $res_entry;
+        }
         
-        return $score_res;
+        return $res;
     }
     
+    
     /**
-     * Service to reorder the tags using their notes in the index search
-     * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
+     * get a list of tags label sprted by their number of documents tagged.
+     * @param int $max_tags the max number of tags to return
+     * @return array of array of tags,
      */
-    public function reorderTagsForDocument($document)
-    {
-        $doctrine = $this->getContainer()->get('doctrine');
-        
-        $tags_score = array();
-        
-        foreach($document->getTags() as $tag)
-        {
-            $label = $tag->getTag()->getLabel();
-            
-            $score_res = $this->search($label, array("id"=>$document->getId()));
-            
-            if(count($score_res)>0)
-            {
-                $score = floatval($score_res[0]['score']);
-            }
-            else
-            {
-                $score = 0.0;
-            }
-            $tags_score[] = array($score,$tag);
-        }
-        // sort tags based on score
-        $i=1;
-        usort($tags_score, function($a, $b) {
-            return $a[0]<$b[0]?1:-1;
-        });
-
-        foreach($tags_score as $item)
-        {
-            $tag = $item[1];
-            $tag->setTagOrder($i++);
-            $tag->setIndexNote($item[0]);
-            $doctrine->getEntityManager()->persist($tag);
-        }
-        
-    }
-    
     public function getTagCloud($max_tags)
     {
         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
         return $rep->getTagCloud($max_tags);
     }
     
+    /**
+     * List the tag label containing the seed given as an argument.
+     * The seed is either at the beggining, the end of the label or at the beggining of a word.
+     * @param unknown_type $seed
+     * @return : an array containing the possible tag labels
+     */
     public function completion($seed)
     {
         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
@@ -133,7 +158,6 @@
         }
         
         return $res;
-        
     }