-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
282 lines (196 loc) · 7.88 KB
/
index.html
File metadata and controls
282 lines (196 loc) · 7.88 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
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
<!--
PageFish - A search engine with a static web server.
2018-07-17
Peter Burkimsher
peterburk@gmail.com
Example usage for OSM data: Type a city name, click search, see the results.
How to build the index:
cc -lm geonamesToIndex.c -o geonamesToIndex
cat geonames_relations.txt | ./geonamesToIndex
Benchmarks:
> time grep -c "Paris" geonames_relations.txt
0.366s = 366 ms
Typical search time (by searching a few city names): 30 ms.
This gives a 10x speedup compared to grep.
Index building time:
> time cat geonames_relations.txt | ./geonamesToIndex
17.185s
Having many small files in the index uses more space. But storage is cheap.
Original file geonames_relations_name_id_lat_lon.txt: 25.8 MB
Index size GeoNamesIndex: 94 MB
How the hashing algorithm works:
1. Convert every character in the query to a number. (Peter = 80, 101, 116, 101, 114)
2. Add up the total of those numbers (80 + 101 + 116 + 101 + 114 = 512).
3. Get the Unicode character for that number (512 = Ȁ).
4. Load the index file that uses that character as a filename. (GeoNamesIndex/Ȁ.txt)
5. Grep the query again within that file (clashes exist, e.g. "Dover" = 68 + 111 + 118 + 101 + 114 = 512)
Why this is important:
+ The router and filesystem help the search.
+ Characters are just numbers. It's faster to search shorter strings.
+ Server-side operations cost money. Client-side JavaScript is free.
+ The index can be stored on a static file server e.g. Github.
Known limitations:
- Fuzzy matching isn't supported (but this is not a problem for Chinese) // fuzziness is off-by-one
- Some characters aren't allowed in filenames, URLs, etc. // find a place for everything
- The hash algorithm should be balanced so that index files are a similar size. // use a binary tree
Name:
• The plan is to turn this into a real search engine later.
• Page stands for Peter's Alternative Google Engine
• Fish stands for Filesystem Index Search Hash
Future work:
• Fix queries with accented characters (they don't seem to hash the same in C and JavaScript)
• Make a more balanced hashing algorithm that still limits to a single character
• Group the index into folders based on the number of characters in the query string (more important for large indexes)
• Spellcheck and fuzzy matching by running multiple searches
-->
<html>
<head>
<title>PageFish</title>
<meta charset="utf-8" />
<style>
body {
font-family: Tahoma, Geneva, sans-serif;
}
p {
margin-left: 50px;
}
.DefaultButtonOuter
{
border: 2px solid black ;
border-radius: 8px;
width: 96px;
height: 26px;
text-align: center;
/*background-color: #99999a;*/
background: #99999a; /* For browsers that do not support gradients */
/*background: -webkit-linear-gradient(left top, red, yellow);*/ /* For Safari 5.1 to 6.0 */
/*background: -o-linear-gradient(bottom right, red, yellow);*/ /* For Opera 11.1 to 12.0 */
/*background: -moz-linear-gradient(bottom right, red, yellow); */ /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom right, #dcdbde, #737374); /* Standard syntax */
}
.ButtonInner
{
border: 1px solid black ;
border-radius: 5px;
background-color: #dcdbde;
width: 88px;
height: 18px;
text-align: center;
margin: 3px;
line-height: 18px;
font-family: "Charcoal CY", "Virtue", Geneva, sans-serif;
font-size: 11px;
color: #000000;
}
.ButtonInner:active
{
background:#666666;
color: #ffffff;
}
</style>
</head>
<body>
<div>
Type a city name:
</div>
<textarea id="queryTextArea" name="text" rows="4" cols="40">Auckland</textarea>
<div id="defaultButton" class="DefaultButtonOuter" style="width:158px">
<div id="searchButton" class="ButtonInner" onclick="searchButtonClicked();" style="width:150px">
Search
</div>
</div>
<div id="searchTime">
</div>
<div id="resultsTable">
</div>
</body>
<script>
var searchQuery = "";
var searchData = "";
var startTime;
var endTime;
function replaceChars(thisString, searchString, replaceString)
{
//debugLog("replaceChars(" + thisString + "," + searchString + "," + replaceString + ")");
return thisString.split(searchString).join(replaceString);
} // end function replaceChars
function searchLoadedData(searchText)
{
if (searchText == "")
{
document.getElementById("resultsHtml").innerHTML = "Search cancelled";
return;
}
var resultsHtml = "";
var resultsArray = [];
var searchRegexString = "^" + "(.*" + searchText + ".*)";
searchRegexString = searchRegexString + "$";
var searchRegex = new RegExp(searchRegexString, "gm");
//document.write(searchRegex);
var searchResultsArray = searchData.match(searchRegex);
return searchResultsArray;
} // end function searchLoadedData
function loadFile(filename)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200)
{
searchData = xmlhttp.responseText;
var searchResultsArray = searchLoadedData(searchQuery);
endTime = Date.now();
var searchTime = endTime - startTime;
var searchResultsHtml = "No Results"
// Add maps link
if (searchResultsArray != null)
{
var numberResults = searchResultsArray.length;
for (var currentResult = 0; currentResult < numberResults; currentResult++)
{
var thisResult = searchResultsArray[currentResult];
var thisResultArray = thisResult.split(" ");
var thisResultLon = thisResultArray[2];
var thisResultLat = thisResultArray[3];
var thisResultLink = "https://www.openstreetmap.org/#map=10/" + thisResultLat + "/" + thisResultLon;
thisResultLink = "<a href=\"" + thisResultLink + "\" target=_blank>Map</a>";
searchResultsArray[currentResult] = thisResult + " " + thisResultLink;
} // end for every result
searchResultsHtml = searchResultsArray.join("</td></tr><tr><td>");
searchResultsHtml = replaceChars(searchResultsHtml, " ", "</td><td>");
searchResultsHtml = "<table><tr><td>Name</td><td>Relation ID</td><td>Longitude</td><td>Latitude</td><td>Link</td></tr><tr><td>" + searchResultsHtml + "</td></tr></table>";
}
document.getElementById("resultsTable").innerHTML = searchResultsHtml;
document.getElementById("searchTime").innerHTML = "Searched in " + searchTime + " ms";
} // end if it loaded the KML
else if (xmlhttp.status == 400) {
alert('Error loading index file ' + filename);
}
else {
alert('Error loading index file ' + filename);
}
}
};
xmlhttp.open("GET", filename, true);
xmlhttp.send();
} // end function loadFile
function searchButtonClicked()
{
startTime = Date.now();
searchQuery = document.getElementById("queryTextArea").value;
var numberCharacters = searchQuery.length;
var thisCharacterInt = 0;
var searchQueryHashInt = 0;
for (var currentCharacter = 0; currentCharacter < numberCharacters; currentCharacter++)
{
thisCharacterInt = searchQuery.charCodeAt(currentCharacter);
searchQueryHashInt = searchQueryHashInt + thisCharacterInt;
} // end for every character in the string
var searchQueryHashString = String.fromCharCode(searchQueryHashInt);
//document.getElementById("queryTextArea").value = searchQueryHashString;
var filename = "OSM/GeoNamesIndex/" + searchQueryHashString + ".txt";
console.log(filename);
loadFile(filename);
} // end function searchButtonClicked
</script>
</html>