forked from imabutahersiddik/CodeStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_system.php
More file actions
101 lines (91 loc) · 3.14 KB
/
search_system.php
File metadata and controls
101 lines (91 loc) · 3.14 KB
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
<?php
class SearchSystem {
private $elasticsearch;
private $pdo;
private $currentTime = '2025-06-14 13:33:08';
public function __construct($pdo) {
$this->pdo = $pdo;
$this->elasticsearch = new \Elasticsearch\ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
}
public function indexApp($app) {
$params = [
'index' => 'apps',
'id' => $app['id'],
'body' => [
'app_name' => $app['app_name'],
'description' => $app['description'],
'web_platform' => $app['web_platform'],
'category' => $app['category'],
'tags' => explode(',', $app['tags']),
'developer_name' => $app['developer_name'],
'downloads_count' => $app['downloads_count'],
'rating' => $app['total_rating'],
'last_updated' => $app['last_updated'],
'indexed_at' => $this->currentTime
]
];
return $this->elasticsearch->index($params);
}
public function search($query, $filters = [], $page = 1, $perPage = 12) {
$params = [
'index' => 'apps',
'body' => [
'from' => ($page - 1) * $perPage,
'size' => $perPage,
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => $query,
'fields' => ['app_name^3', 'description', 'tags^2']
]
]
]
]
]
];
// Add filters
if (!empty($filters)) {
foreach ($filters as $field => $value) {
$params['body']['query']['bool']['filter'][] = [
'term' => [$field => $value]
];
}
}
// Add aggregations
$params['body']['aggs'] = [
'categories' => [
'terms' => ['field' => 'category']
],
'platforms' => [
'terms' => ['field' => 'web_platform']
],
'avg_rating' => [
'avg' => ['field' => 'rating']
]
];
$result = $this->elasticsearch->search($params);
return $this->formatSearchResults($result);
}
private function formatSearchResults($result) {
$formatted = [
'total' => $result['hits']['total']['value'],
'apps' => [],
'aggregations' => [
'categories' => $result['aggregations']['categories']['buckets'],
'platforms' => $result['aggregations']['platforms']['buckets'],
'avg_rating' => $result['aggregations']['avg_rating']['value']
]
];
foreach ($result['hits']['hits'] as $hit) {
$formatted['apps'][] = [
'id' => $hit['_id'],
'score' => $hit['_score'],
'data' => $hit['_source']
];
}
return $formatted;
}
}