# HG changeset patch # User ymh # Date 1330034527 -3600 # Node ID ca2a145e67f350ba8d00c9ff62e9ad001f2c1c8e # Parent 901463f9b11cfce0e0361463b51b1ca95b69292b Completion service add the nb of doc + test diff -r 901463f9b11c -r ca2a145e67f3 Entity/TagRepository.php --- a/Entity/TagRepository.php Tue Feb 21 15:15:22 2012 +0100 +++ b/Entity/TagRepository.php Thu Feb 23 23:02:07 2012 +0100 @@ -44,8 +44,10 @@ public function getCompletion($seed) { $qb = $this->getEntityManager()->createQueryBuilder(); - $qb->select('DISTINCT t.label'); + $qb->select('t.label', 'COUNT(dt.id) AS nb_docs'); $qb->from('WikiTagBundle:Tag','t'); + $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag'); + $qb->addGroupBy('t.label'); $qb->where($qb->expr()->orx( $qb->expr()->like('t.label',$qb->expr()->literal(addcslashes(mysql_real_escape_string($seed),"%_")."%")), $qb->expr()->like('t.label',$qb->expr()->literal("%".addcslashes(mysql_real_escape_string($seed),"%_"))), diff -r 901463f9b11c -r ca2a145e67f3 Services/SearchService.php --- a/Services/SearchService.php Tue Feb 21 15:15:22 2012 +0100 +++ b/Services/SearchService.php Thu Feb 23 23:02:07 2012 +0100 @@ -49,12 +49,12 @@ /** + * Search. * - * Enter description here ... * @param mixed $value : either a string or the list of fields to search into ; format : ['' => ['weight'=>, '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 [] + * @return array [{'host_doc_id'=>,'wikitag_doc_id'=>, 'wikitag_doc'=>, '_score'=>,'field1'=>, 'field2'=>, }] */ public function search($value, array $conditions = null, array $fields = null) { @@ -145,16 +145,17 @@ /** * 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 + * @param string $seed + * @param boolean $doc_nb + * @return : an array containing the possible tag labels with the number of documents */ - public function completion($seed) + public function completion($seed, $doc_nb=False) { $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Tag'); $res = array(); foreach ($rep->getCompletion($seed) as $value) { - $res[] = $value['label']; + $res[] = array('label' => $value['label'], 'nb_docs' => $value['nb_docs']); } return $res; diff -r 901463f9b11c -r ca2a145e67f3 Tests/Services/SearchServiceTest.php --- a/Tests/Services/SearchServiceTest.php Tue Feb 21 15:15:22 2012 +0100 +++ b/Tests/Services/SearchServiceTest.php Thu Feb 23 23:02:07 2012 +0100 @@ -97,12 +97,20 @@ $search_service = $this->get("wiki_tag.search"); $result = $search_service->completion("tag"); + $completion_result = array( + 'Tag1' => 2, + 'Tag2' => 4, + 'Tag3' => 5, + 'Tag4' => 6 + ); $this->assertNotNull($result, "completion should not be null"); $this->assertEquals(4, count($result)); foreach ($result as $tagname) { - $this->assertEquals(0,strpos($tagname,"tag")); + $this->assertEquals(0,strpos($tagname['label'],"Tag")); + $this->assertArrayHasKey($tagname['label'], $completion_result); + $this->assertEquals($completion_result[$tagname['label']], $tagname['nb_docs']); } } diff -r 901463f9b11c -r ca2a145e67f3 phpunit.xml.dist --- a/phpunit.xml.dist Tue Feb 21 15:15:22 2012 +0100 +++ b/phpunit.xml.dist Thu Feb 23 23:02:07 2012 +0100 @@ -1,3 +1,4 @@ + - +