-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpclient.php
297 lines (265 loc) · 11.4 KB
/
phpclient.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
include 'SpellCorrector.php';
// make sure browsers see this page as utf-8 encoded HTML
header('Content-Type: text/html; charset=utf-8');
$limit = 10;
$option = isset($_REQUEST['option']) ? $_REQUEST['option'] : 'default';
$query = isset($_REQUEST['q']) ? $_REQUEST['q'] : false;
$results = false;
$corrected = false;
//spell checking
if ($query) {
$corrected = false;
$queryArr = explode(" ", $query);
$corrArray = array();
for ($j = 0; $j < count($queryArr); $j++) {
ini_set('memory_limit', -1);
$correctWord = SpellCorrector::correct(trim($queryArr[$j]));
if ($correctWord != $queryArr[$j]) {
$corrected = true;
array_push($corrArray, trim($correctWord));
} else {
array_push($corrArray, $queryArr[$j]);
}
}
if ($corrected) {
$correctStr = "";
for ($i = 0; $i < count($corrArray); $i++) {
$correctStr = $correctStr . " " . $corrArray[$i];
}
// if (count($corrArray) < count($queryArr)) {
// for ($i = count($corrArray); $i < count($queryArr); $i++) {
// $correctStr = $correctStr . " " . $queryArr[$i];
// }
// }
}
}
if ($query) {
// The Apache Solr Client library should be on the include path
// which is usually most easily accomplished by placing in the
// same directory as this script ( . or current directory is a default
// php include path entry in the php.ini)
require_once('Apache/Solr/Service.php');
// create a new solr service instance - host, port, and webapp
// path (all defaults in this example)
$solr = new Apache_Solr_Service('localhost', 8983, '/solr/sample');
// if magic quotes is enabled then stripslashes will be needed
if (get_magic_quotes_gpc() == 1) {
$query = stripslashes($query);
}
$additionalParameters = array();
if ($option == 'default') {
$additionalParameters = array(
'fl' => 'title og_url id description',
);
} else {
$additionalParameters = array(
'fl' => 'title og_url id description',
'sort' => 'pageRankFile.txt desc'
);
}
// in production code you'll always want to use a try /catch for any
// possible exceptions emitted by searching (i.e. connection
// problems or a query parsing error)
try {
$results = $solr->search($query, 0, $limit, $additionalParameters);
} catch (Exception $e) {
// in production you'd probably log or email this error to an admin
// and then show a special message to the user but for this example
// we're going to show the full exception
die("<html><head><title>SEARCH EXCEPTION</title><body><pre>{$e->__toString()}</pre></body></html>");
}
}
?>
<html>
<head>
<title>PHP Solr Client Example</title>
<!--$results->__get('response')->docs[1] -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style>
body {
margin: auto;
}
form {
width: 900px;
margin-left: auto;
margin-right: auto;
text-align: center;
margin-top: 150px;
}
.content {
width: 800px;
margin-left: auto;
margin-right: auto;
}
ol li {
list-style: none;
}
</style>
</head>
<body>
<div class="container">
<form accept-charset="utf-8" id="myform" method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<div class="form-group">
<h1 for="q" class="display-3 pb-3" style="color: #616067">Search Engine</h1>
<div class="row">
<div class="col-sm-1"></div>
<div class="col-sm-10">
<input class="form-control" id="q" name="q" type="text" value="<?php echo htmlspecialchars($query, ENT_QUOTES, 'utf-8'); ?>" />
</div>
<div class="col-sm-1"></div>
</div>
</div>
<?php
if ($corrected) {
?>
<div>
<span>Did you mean: </span>
<a href="#" data-val="<?php echo $correctStr ?>" class="correctSubmit"> <?php echo $correctStr ?> </a>
</div>
<?php
}
?>
<div>Pick an Algorithm</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="option" <?php if (isset($option) && $option == "default") echo "checked" ?> value="default">
<label class="form-check-label">Default</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="option" <?php if (isset($option) && $option == "pagerank") echo "checked" ?> value="pagerank">
<label class="form-check-label">PageRank</label>
</div>
<input class="btn btn-dark" style="margin-top: 30px" type="submit" value="Search">
</form>
</div>
<?php
// display results
if ($results) {
$total = (int)$results->response->numFound;
$start = min(1, $total);
$end = min($limit, $total);
$mapFiles = array();
$dirPath = '/Users/haopengsong/572/solr-7.7.0/../reutersnews/';
?>
<div class="content">Results <?php echo $start; ?> - <?php echo $end; ?> : </div>
<ol>
<?php
// iterate result documents
foreach ($results->response->docs as $doc) {
$id = $title = $desc = $orurl = '';
$mapLoaded = false;
?>
<li>
<table class="table table-dark content mb-1" style="border-bottom: 1px solid #ccc; text-align: left; margin-top: 2px;">
<?php
// iterate document fields / values
foreach ($doc as $field => $value) {
if ($field == 'id') {
$id = $value;
} else if ($field == 'title') {
$title = $value;
} else if ($field == 'description') {
$desc = $value;
} else {
$orurl = $value;
}
}
if ($desc == '') {
$desc = 'N/A';
}
if ($orurl == '') {
if ($mapLoaded) {
$orurl = $mapFiles[$id];
} else {
$mapLoaded = true;
$row = 1;
if (($handle = fopen('URLtoHTML_reuters_news.csv', 'r')) !== false) {
while (($data = fgetcsv($handle, 1000, ',')) !== false) {
$num = count($data);
$mapFiles[$dirPath . $data[0]] = $data[1];
}
}
$orurl = $mapFiles[$id];
}
}
//snippet
$htmlContent = file_get_contents($id);
$phrases = explode(".", $htmlContent);
$snippet = "";
$queryTerms = "";
$querys = explode(" ", $query);
for ($i = 0; $i < count($querys); $i++) {
$queryTerms .= $querys[$i] . '(.*)';
}
$queryTerms = '/' . trim($queryTerms) . '/i';
foreach ($phrases as $phrase) {
$phrase = strip_tags($phrase);
if (preg_match("('|\"|;|`|~|\/|[|]|\|{|}|<|\%|>|:)", $phrase) > 0) {
continue;
}
if (preg_match($queryTerms, $phrase) >= 1) {
$snippet = $snippet . ' ' . strip_tags(trim($phrase)) . ' ';
}
if (strlen($snippet) > 170) {
break;
}
}
if (strlen($snippet) <= 1) {
$snippet = 'N/A';
} else {
for ($i = 0; $i < count($querys); $i++) {
$snippet = preg_replace('/' . $querys[$i] . '/i', '<strong style="font-weight:900; color:lemonchiffon;">' . $querys[$i] . '</strong>', $snippet);
}
}
//echo 'here';
?>
<tr>
<th>Title: </th>
<td><a style="font-size: 1.5em;" href="<?php echo $orurl; ?>"> <?php echo $title; ?></a></td>
</tr>
<tr>
<th>URL: </th>
<td><a style="color: #00ff0c; font-size:13px;" href="<?php echo $orurl; ?>"> <?php echo $orurl; ?></a></td>
</tr>
<tr>
<th>ID: </th>
<td><?php echo $id; ?></td>
</tr>
<tr>
<th>Description: </th>
<td><?php echo $desc; ?></td>
</tr>
<tr>
<th>Snippet: </th>
<td style="font-size: 14px; font-style:italic; font-weight: 100;"><?php echo '...' . $snippet . '...'; ?></td>
</tr>
</table>
</li>
<?php
}
?>
</ol>
<?php
}
?>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#q").autocomplete({
source: "autocomplete.php",
minLength: 1,
});
});
$('.correctSubmit').on('click', function(e) {
e.preventDefault();
$('#q').val($(this).data('val'));
$('#myform').submit();
});
</script>
</body>
</html>