forked from hugowetterberg/solr_resource
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SolrResource.php
138 lines (125 loc) · 3.99 KB
/
SolrResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
// $Id$
/**
* Class that defines the SolrSearch resource
*/
class SolrResource {
/**
* Performs a search against the Solr index.
*/
public static function index($query='*:*', $page=0, $fields='', $params=array(), $sparams=array()) {
$res = array(
'items' => array(),
'facets' => array(),
);
$rows = isset($params['rows']) ? $params['rows'] : 20;
if ($rows > 200) {
$rows = 200;
}
$sparams = $sparams + array(
'start' => $page*$rows,
'rows' => $rows,
'qt' => 'standard',
);
$rows = $sparams['rows'];
// Get field list
if (empty($fields)) {
$fields = array('nid', 'type', 'title');
}
else if (is_string($fields)) {
$fields = split(',', $fields);
}
$sparams['fl'] = join($fields, ',');
// Get facet params
$facet_queries = array();
$facets = array();
if (isset($params['facets'])) {
$facets = split(',', $params['facets']);
$sparams['facet'] = 'true';
$sparams['facet.sort'] = 'true';
$sparams['facet.limit'] = !empty($params['facet_limit']) ? intval($params['facet_limit']) : 20;
$sparams['facet.mincount'] = 1;
$sparams['facet.field'] = $facets;
if (isset($params['facet_queries'])) {
$facet_queries = split(',', $params['facet_queries']);
$sparams['facet.query'] = $facet_queries;
}
}
// Validate sort parameter
if (isset($params['sort']) && preg_match('/^([a-z0-9_]+ (asc|desc)(,)?)+$/i', $params['sort'])) {
$sparams['sort'] = $params['sort'];
}
$solr = apachesolr_get_solr();
$response = $solr->search($query, $sparams['start'], $sparams['rows'], $sparams, 'POST');
$res['total'] = $response->response->numFound;
$res['page_size'] = $sparams['rows'];
if ($res['total']) {
foreach ($response->response->docs as $doc) {
$item = array();
foreach ($doc->getFieldNames() as $field) {
$item[$field] = $doc->getField($field);
$item[$field] = $item[$field]['value'];
}
$res['items'][] = $item;
}
}
if (isset($response->facet_counts->facet_fields)) {
foreach ($response->facet_counts->facet_fields as $facet_field => $counts) {
if (!empty($counts)) {
$res['facets'][$facet_field] = get_object_vars($counts);
switch ($facet_field) {
case 'tid':
self::getTermInfo($res['facets'][$facet_field]);
break;
case 'im_simple_geo_area':
self::getAreaInfo($res['facets'][$facet_field]);
break;
// TODO: Maybe the facet describing process should be extensible?
}
}
}
}
if (isset($response->facet_counts->facet_queries)) {
foreach ($response->facet_counts->facet_queries as $query => $counts) {
$res['query_facets'][$query] = $counts;
}
}
return $res;
}
/**
* Get tag info from a group of TID's
*
* @static
* @access private
* @param array &$facets
* @return void
*/
private static function getTermInfo(&$facets) {
$tids = array_keys($facets);
if (!empty($tids)) {
$placeholders = join(array_fill(0, count($tids), '%d'), ', ');
$res = db_query("SELECT tid, vid, name FROM {term_data} WHERE tid IN({$placeholders})", $tids);
while($t = db_fetch_object($res)) {
$facets[$t->tid] = array('name' => $t->name, 'vid' => $t->vid, 'count' => $facets[$t->tid]);
}
}
}
/**
* Get area info from a group of NID's
*
* @static
* @access private
* @param array &$facets
* @return void
*/
private static function getAreaInfo(&$facets) {
$area_ids = array_keys($facets);
if (!empty($area_ids)) {
$placeholders = join(array_fill(0, count($area_ids), '%d'), ', ');
$res = db_query("SELECT nid, title FROM {node} WHERE nid IN({$placeholders})", $area_ids);
while($a = db_fetch_object($res)) {
$facets[$a->nid] = $a->title;
}
}
}
}