1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * solr actions. |
---|
5 | * |
---|
6 | * @package cpc |
---|
7 | * @subpackage solr |
---|
8 | * @author Your name here |
---|
9 | * @version SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $ |
---|
10 | */ |
---|
11 | class solrActions extends sfActions |
---|
12 | { |
---|
13 | /** |
---|
14 | * Executes index action |
---|
15 | * |
---|
16 | * @param sfRequest $request A request object |
---|
17 | */ |
---|
18 | public function executeSearch(sfWebRequest $request) |
---|
19 | { |
---|
20 | if ($search = $request->getParameter('search')) { |
---|
21 | return $this->redirect('solr/search?query='.$search); |
---|
22 | } |
---|
23 | $this->query = $request->getParameter('query'); |
---|
24 | |
---|
25 | $query = preg_replace('/\*/', '', $this->query); |
---|
26 | |
---|
27 | if (!strlen($query)) { |
---|
28 | $query = '*'; |
---|
29 | } |
---|
30 | |
---|
31 | $nb = 20; |
---|
32 | $deb = ($request->getParameter('page', 1) - 1) * $nb ; |
---|
33 | $fq = ''; |
---|
34 | $this->facet = array(); |
---|
35 | |
---|
36 | $this->selected = array(); |
---|
37 | if ($on = $request->getParameter('object_name')) { |
---|
38 | $this->selected['object_name'][$on] = 1; |
---|
39 | $fq .= " object_name:$on"; |
---|
40 | } |
---|
41 | if ($tags = $request->getParameter('tag')) { |
---|
42 | foreach(explode(',', $tags) as $tag) { |
---|
43 | $this->selected['tag'][$tag] = 1; |
---|
44 | $fq .= ' tag:"'.$tag.'"'; |
---|
45 | } |
---|
46 | } |
---|
47 | //Récupère les résultats auprès de SolR |
---|
48 | $s = new SolrConnector(); |
---|
49 | $params = array('hl'=>'true', 'fl' => 'id,object_id,object_name', 'hl.fragsize'=>500, "facet"=>"true", "facet.field"=>array("object_name","tag"), "facet.date" => "date", "facet.date.start"=>"2007-05-01T00:00:00Z", "facet.date.end"=>"NOW", "facet.date.gap"=>"+1MONTH", 'fq' => $fq); |
---|
50 | if ($date = $request->getParameter('date')) { |
---|
51 | $dates = explode(',', $date); |
---|
52 | $date = array_pop($dates); |
---|
53 | $period = 'MONTH'; |
---|
54 | if (count($dates) == 1) |
---|
55 | $period = 'DAY'; |
---|
56 | $query .= ' date:['.$date.' TO '.$date.'+1'.$period.']'; |
---|
57 | $this->selected['date'][$date] = 1; |
---|
58 | $params['facet.date.start']=$date; |
---|
59 | $params['facet.date.end'] = $date.'+1'.$period; |
---|
60 | $params['facet.date.gap'] = '+1DAY'; |
---|
61 | } |
---|
62 | $results = $s->search($query, $params, $deb, $nb); |
---|
63 | //Reconstitut les résultats |
---|
64 | $this->results = $results['response']; |
---|
65 | for($i = 0 ; $i < count($this->results['docs']) ; $i++) { |
---|
66 | $res = $this->results['docs'][$i]; |
---|
67 | $this->results['docs'][$i]['highlighting'] = implode('...', $results['highlighting'][$res['id']]['text']); |
---|
68 | // $this->results['docs'][$i]['object'] = Doctrine::getTable($res['object_name'])->find($res['object_id']); |
---|
69 | } |
---|
70 | $this->results['end'] = $deb + $nb; |
---|
71 | $this->results['page'] = $deb/$nb + 1; |
---|
72 | if ($this->results['end'] > $this->results['numFound']) { |
---|
73 | $this->results['end'] = $this->results['numFound'] + 1; |
---|
74 | } |
---|
75 | |
---|
76 | //Prépare les facets |
---|
77 | $this->facet['type']['prefix'] = ''; |
---|
78 | $this->facet['type']['facet_field'] = 'object_name'; |
---|
79 | $this->facet['type']['name'] = 'Types'; |
---|
80 | $this->facet['type']['values'] = $results['facet_counts']['facet_fields']['object_name']; |
---|
81 | |
---|
82 | $tags = $results['facet_counts']['facet_fields']['tag']; |
---|
83 | $this->facet['tag']['prefix'] = ''; |
---|
84 | $this->facet['tag']['facet_field'] = 'tag'; |
---|
85 | $this->facet['tag']['name'] = 'Tags'; |
---|
86 | $this->facet['parlementaire']['prefix'] = 'parlementaire='; |
---|
87 | $this->facet['parlementaire']['facet_field'] = 'tag'; |
---|
88 | $this->facet['parlementaire']['name'] = 'Parlementaire'; |
---|
89 | foreach($tags as $tag => $nb ) { |
---|
90 | if (!$nb) |
---|
91 | continue; |
---|
92 | if (!preg_match('/=/', $tag)) |
---|
93 | $this->facet['tag']['values'][$tag] = $nb; |
---|
94 | if (preg_match('/^parlementaire=(.*)/', $tag, $matches)) { |
---|
95 | $this->facet['parlementaire']['values'][$matches[1]] = $nb; |
---|
96 | } |
---|
97 | } |
---|
98 | if ($results['response']['numFound']) { |
---|
99 | $this->fdates = array(); |
---|
100 | $this->fdates['max'] = 1; |
---|
101 | foreach($results['facet_counts']['facet_dates']['date'] as $date => $nb) { |
---|
102 | if (preg_match('/^20/', $date)) { |
---|
103 | $pc = $nb/$results['response']['numFound']; |
---|
104 | $this->fdates['values'][$date] = array('nb' => $nb, 'pc' => $pc); |
---|
105 | if ($this->fdates['max'] < $pc) { |
---|
106 | $this->fdates['max'] = $pc; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|