Services/SearchService.php
changeset 68 e7384fb35f7a
parent 57 186c4121c7b3
child 75 ca2a145e67f3
equal deleted inserted replaced
67:989d9e117586 68:e7384fb35f7a
    32         $this->setContainer($container);
    32         $this->setContainer($container);
    33     }
    33     }
    34     
    34     
    35     private $doctrine;
    35     private $doctrine;
    36     
    36     
       
    37     /**
       
    38      * doctrine object getter
       
    39      * @return object
       
    40      */
    37     public function getDoctrine()
    41     public function getDoctrine()
    38     {
    42     {
    39         if(is_null($this->doctrine))
    43         if(is_null($this->doctrine))
    40         {
    44         {
    41             $this->doctrine = $this->getContainer()->get('doctrine');
    45             $this->doctrine = $this->getContainer()->get('doctrine');
    42         }
    46         }
    43         return $this->doctrine;
    47         return $this->doctrine;
    44     }
    48     }
       
    49     
       
    50     
    45     /**
    51     /**
    46      *
    52      *
    47      * Enter description here ...
    53      * Enter description here ...
    48      * @param string $value
    54      * @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]]
    49      * @param array $conditions
    55      * @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)
    50      * @param array $fields
    56      * @param array $fields : The field of field names to export, one of the list specified in the configuration file
       
    57      * @return array []
    51      */
    58      */
    52     public function search($value, array $conditions, array $fields=null)
    59     public function search($value, array $conditions = null, array $fields = null)
    53     {
    60     {
       
    61         if(is_null($value) || (is_string($value) && strlen($value) == 0) || count($value)==0 ) {
       
    62             return null;
       
    63         }
       
    64         
       
    65         $fielddeflist = $this->getContainer()->getParameter("wiki_tag.fields");
       
    66         $fieldquery = array();
       
    67         
       
    68         // build filed query
       
    69         if(is_string($value)) {
       
    70             foreach ($fielddeflist as $fieldname => $fielddef) {
       
    71                 if(!is_null($fielddef) && isset($fielddef['weight'])) {
       
    72                     $weight = $fielddef['weight'];
       
    73                 }
       
    74                 else
       
    75                 {
       
    76                     $weight = 1.0;
       
    77                 }
       
    78                 $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
       
    79             }
       
    80         }
       
    81         else {
       
    82             foreach ($value as $fieldname => $fielddef) {
       
    83                 if(is_null($fielddef) || !isset($fielddef['value']) || strlen($fielddef['value']) == 0) {
       
    84                     continue;
       
    85                 }
       
    86                 $valuefield = $fielddef['value'];
       
    87                 if(isset($fielddef['weight'])) {
       
    88                     $weight = $fielddef['weight'];
       
    89                 }
       
    90                 else
       
    91                 {
       
    92                     $weight = 1.0;
       
    93                 }
       
    94                 
       
    95                 $fieldquery[] = array("columns"=>$fieldname, "value"=>$valuefield, "weight"=>$weight);
       
    96             }
       
    97         }
       
    98         
       
    99         // buildf
    54         if(is_null($fields))
   100         if(is_null($fields))
    55         {
   101         {
    56             $fields = $this->getContainer()->getParameter("wiki_tag.fields");
   102             $fieldnamelist = array();
       
   103             foreach ($fielddeflist as $fieldname => $fielddef) {
       
   104                 $fieldnamelist[] = $fieldname;
       
   105             }
    57         }
   106         }
    58         $doctrine = $this->getContainer()->get('doctrine');
   107         else {
    59         $res = $doctrine->getRepository('WikiTagBundle:Document');
   108             $fieldnamelist = $fields;
    60         $fieldquery = array();
       
    61         foreach ($fields as $fieldname => $fielddef) {
       
    62             if(isset($fielddef['weight']))
       
    63             {
       
    64                 $weight = $fielddef['weight'];
       
    65             }
       
    66             else
       
    67             {
       
    68                 $weight = 1.0;
       
    69             }
       
    70             $fieldquery[] = array("columns"=>$fieldname, "value"=>$value, "weight"=>$weight);
       
    71         }
   109         }
    72         
   110         
    73         $score_res = $res->search($fieldquery, $conditions);
   111         $doctrine = $this->getContainer()->get('doctrine');
       
   112         $rep = $doctrine->getRepository('WikiTagBundle:Document');
       
   113                 
       
   114         $score_res = $rep->search($fieldquery, $conditions, $fieldnamelist);
       
   115 
       
   116         $res = array();
       
   117         foreach($score_res as $single_res) {
       
   118             $res_entry = array();
       
   119             $res_entry['host_doc_id'] = $single_res[0]->getExternalId()->getId();
       
   120             $res_entry['wikitag_doc_id'] = $single_res[0]->getId();
       
   121             foreach($fieldnamelist as $fieldname) {
       
   122                 $accessor_name = "get".ucfirst($fieldname);
       
   123                 $res_entry[$fieldname] = $single_res[0]->{$accessor_name}();
       
   124             }
       
   125             $res_entry['_score'] = $single_res['score'];
       
   126             $res_entry['wikitag_doc'] = $single_res[0];
       
   127             $res[] = $res_entry;
       
   128         }
    74         
   129         
    75         return $score_res;
   130         return $res;
    76     }
   131     }
    77     
   132     
       
   133     
    78     /**
   134     /**
    79      * Service to reorder the tags using their notes in the index search
   135      * get a list of tags label sprted by their number of documents tagged.
    80      * @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document
   136      * @param int $max_tags the max number of tags to return
       
   137      * @return array of array of tags,
    81      */
   138      */
    82     public function reorderTagsForDocument($document)
       
    83     {
       
    84         $doctrine = $this->getContainer()->get('doctrine');
       
    85         
       
    86         $tags_score = array();
       
    87         
       
    88         foreach($document->getTags() as $tag)
       
    89         {
       
    90             $label = $tag->getTag()->getLabel();
       
    91             
       
    92             $score_res = $this->search($label, array("id"=>$document->getId()));
       
    93             
       
    94             if(count($score_res)>0)
       
    95             {
       
    96                 $score = floatval($score_res[0]['score']);
       
    97             }
       
    98             else
       
    99             {
       
   100                 $score = 0.0;
       
   101             }
       
   102             $tags_score[] = array($score,$tag);
       
   103         }
       
   104         // sort tags based on score
       
   105         $i=1;
       
   106         usort($tags_score, function($a, $b) {
       
   107             return $a[0]<$b[0]?1:-1;
       
   108         });
       
   109 
       
   110         foreach($tags_score as $item)
       
   111         {
       
   112             $tag = $item[1];
       
   113             $tag->setTagOrder($i++);
       
   114             $tag->setIndexNote($item[0]);
       
   115             $doctrine->getEntityManager()->persist($tag);
       
   116         }
       
   117         
       
   118     }
       
   119     
       
   120     public function getTagCloud($max_tags)
   139     public function getTagCloud($max_tags)
   121     {
   140     {
   122         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
   141         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
   123         return $rep->getTagCloud($max_tags);
   142         return $rep->getTagCloud($max_tags);
   124     }
   143     }
   125     
   144     
       
   145     /**
       
   146      * List the tag label containing the seed given as an argument.
       
   147      * The seed is either at the beggining, the end of the label or at the beggining of a word.
       
   148      * @param unknown_type $seed
       
   149      * @return : an array containing the possible tag labels
       
   150      */
   126     public function completion($seed)
   151     public function completion($seed)
   127     {
   152     {
   128         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
   153         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag');
   129         
   154         
   130         $res = array();
   155         $res = array();
   131         foreach ($rep->getCompletion($seed) as $value) {
   156         foreach ($rep->getCompletion($seed) as $value) {
   132             $res[] = $value['label'];
   157             $res[] = $value['label'];
   133         }
   158         }
   134         
   159         
   135         return $res;
   160         return $res;
   136         
       
   137     }
   161     }
   138     
   162     
   139         
   163         
   140 }
   164 }