155 lines
111 KiB
HTML
155 lines
111 KiB
HTML
|
<html>
|
||
|
<head>
|
||
|
<title>Test of Indexer</title>
|
||
|
</head>
|
||
|
<body onload="load()">
|
||
|
<textarea name="output" id="output" rows="25" cols="80"></textarea>
|
||
|
<script type="text/javascript">
|
||
|
var json = [{"id":"1","name":"Coffey, Moana L.","email":"tempus.risus.Donec@eutelluseu.com","city":"College Station","state":"Maine","country":"Bosnia and Herzegovina","rank":90,"desc":"amet ante. Vivamus non lorem vitae odio sagittis semper. Nam tempor diam dictum sapien. Aenean massa. Integer vitae nibh. Donec est mauris, rhoncus id, mollis nec, cursus a, enim. Suspendisse aliquet, sem ut cursus luctus, ipsum leo elementum sem, vitae aliquam eros turpis non enim. Mauris quis turpis vitae purus gravida sagittis. Duis gravida. Praesent eu"},{"id":"2","name":"Willis, Velma K.","email":"tristique@egetmagnaSuspendisse.edu","city":"Agat","state":"Rhode Island","country":"Bahamas","rank":"50","desc":"velit. Sed malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus, a sollicitudin orci sem eget massa. Suspendisse eleifend. Cras sed leo. Cras vehicula aliquet libero. Integer in magna. Phasellus dolor elit, pellentesque a, facilisis non, bibendum sed, est. Nunc laoreet lectus quis massa. Mauris vestibulum, neque sed dictum eleifend,"},{"id":"3","name":"Roman, Carl P.","email":"dolor.sit@Quisquefringillaeuismod.edu","city":"Elmira","state":"Oklahoma","country":"Japan","rank":"83","desc":"mauris sagittis placerat. Cras dictum ultricies ligula. Nullam enim. Sed nulla ante, iaculis nec, eleifend non, dapibus rutrum, justo. Praesent luctus. Curabitur egestas nunc sed libero. Proin sed turpis nec mauris blandit mattis. Cras eget nisi dictum augue malesuada malesuada. Integer id magna et ipsum cursus vestibulum. Mauris magna. Duis dignissim tempor arcu. Vestibulum ut eros non enim commodo hendrerit. Donec porttitor tellus non magna. Nam ligula elit, pretium et, rutrum non, hendrerit id, ante."},{"id":"4","name":"Hopkins, Wesley N.","email":"rutrum.non@aliquameuaccumsan.edu","city":"Bangor","state":"Arizona","country":"Chile","rank":"13","desc":"Nunc mauris. Morbi non sapien molestie orci tincidunt adipiscing. Mauris molestie pharetra nibh. Aliquam ornare, libero at auctor ullamcorper, nisl arcu iaculis enim, sit amet ornare lectus justo eu arcu. Morbi sit amet massa. Quisque porttitor eros nec tellus. Nunc lectus pede, ultrices a, auctor non, feugiat nec, diam. Duis mi enim, condimentum eget, volutpat ornare, facilisis eget, ipsum. Donec sollicitudin adipiscing ligula. Aenean gravida nunc sed pede. Cum sociis natoque penatibus et magnis dis"},{"id":"5","name":"Nguyen, Kellie C.","email":"diam@ipsumdolorsit.ca","city":"Bradbury","state":"Illinois","country":"Portugal","rank":"72","desc":"neque. Sed eget lacus. Mauris non dui nec urna suscipit nonummy. Fusce fermentum fermentum arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Phasellus ornare. Fusce mollis. Duis sit amet diam eu dolor egestas rhoncus. Proin nisl sem, consequat nec, mollis vitae, posuere at, velit. Cras lorem lorem, luctus ut, pellentesque eget, dictum placerat, augue. Sed molestie. Sed id risus quis"},{"id":"6","name":"English, Donovan R.","email":"Phasellus.elit.pede@arcu.org","city":"Lake Forest","state":"Rhode Island","country":"Latvia","rank":"96","desc":"velit justo nec ante. Maecenas mi felis, adipiscing fringilla, porttitor vulputate, posuere vulputate, lacus. Cras interdum. Nunc sollicitudin commodo ipsum. Suspendisse non leo. Vivamus nibh dolor, nonummy ac, feugiat non, lobortis quis, pede. Suspendisse dui. Fusce diam nunc, ullamcorper eu, euismod ac, fermentum vel, mauris. Integer sem elit, pharetra ut, pharetra sed, hendrerit a, arcu. Sed et libero. Proin mi. Aliquam gravida mauris ut mi. Duis risus odio, auctor vitae, aliquet nec, imperdiet nec, leo. Morbi neque tellus, imperdiet non, vestibulum nec, euismod in, dolor. Fusce feugiat. Lorem ipsum dolor sit amet, consectetuer"},{"id":"7","name":"Hart, Daryl R.","email":"ridiculus@auctorMaurisvel.org","city":"Philadelphia","state":"Iowa","country":"Cocos (Keeling) Islands","rank":"2","desc":"neque. In ornare sagittis felis. Donec tempor, est ac mattis semper, dui lectus rutrum urna, nec luctus felis purus ac tellus. Susp
|
||
|
|
||
|
var Tidx = function()
|
||
|
{
|
||
|
// _index is the weighted, field-based reversed index used
|
||
|
// to search.
|
||
|
this._index = {};
|
||
|
this.v_rx = new RegExp('[\\w-]+', 'img');
|
||
|
|
||
|
// Adds data to index
|
||
|
this.index = function(id, field, value)
|
||
|
{
|
||
|
var f;
|
||
|
switch(typeof field){
|
||
|
// Don't allow undefined fields
|
||
|
case 'undefined': return;
|
||
|
case 'string': f = field.toLowerCase(); break;
|
||
|
default: f = String(field);
|
||
|
}
|
||
|
|
||
|
var v;
|
||
|
switch(typeof value){
|
||
|
// Don't allow undefined, null or object values
|
||
|
case 'null': case 'undefined': case 'object': return;
|
||
|
|
||
|
case 'string':
|
||
|
v = value.toLowerCase();
|
||
|
// Don't index fields one character in length
|
||
|
if(v.length < 2){ return; }
|
||
|
break;
|
||
|
|
||
|
default: v = String(value);
|
||
|
}
|
||
|
|
||
|
// Add field to field list, as needed
|
||
|
if(this._index[f] == undefined){ this._index[f] = {}; }
|
||
|
|
||
|
// Iterate over discrete searchable terms
|
||
|
var r;
|
||
|
while((r = this.v_rx.exec(value)) !== null){
|
||
|
var v = r[0].toLowerCase();
|
||
|
// If the field in question doesn't have this term already,
|
||
|
// add it.
|
||
|
if(this._index[f][v] == undefined){ this._index[f][v] = {}; }
|
||
|
|
||
|
// Add this id to the reverse index under specific field (f)
|
||
|
// and term (v), if it already exists, increment the weight
|
||
|
if(this._index[f][v][id] == undefined){
|
||
|
this._index[f][v][id] = 1;
|
||
|
} else {
|
||
|
this._index[f][v][id]++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Conducts a global search for a string (value) globally,
|
||
|
// iterating through all fields.
|
||
|
this.gsearch = function(value)
|
||
|
{
|
||
|
// Refuse empty searches
|
||
|
if(value.length < 1){ return []; }
|
||
|
|
||
|
var v = value.toLowerCase();
|
||
|
var r = {};
|
||
|
|
||
|
// Loop over every field
|
||
|
for(var f in this._index){
|
||
|
// Look for the specified search term
|
||
|
if(this._index[f][v] != undefined){
|
||
|
// If it exists, add the result to r, adding in the weight
|
||
|
for(var i in this._index[f][v]){
|
||
|
if(r[i] == undefined){ r[i] = 0; }
|
||
|
r[i] += this._index[f][v][i];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return this.sortresult(r);
|
||
|
}
|
||
|
|
||
|
|
||
|
// Conducts a field specific search for a string (value)
|
||
|
this.fsearch = function(field, value)
|
||
|
{
|
||
|
var f;
|
||
|
switch(typeof field){
|
||
|
// Don't allow undefined fields
|
||
|
case 'undefined': return;
|
||
|
case 'string': f = field.toLowerCase(); break;
|
||
|
default: f = String(field);
|
||
|
}
|
||
|
|
||
|
if(value.length < 1){ return []; }
|
||
|
var v = value.toLowerCase();
|
||
|
|
||
|
if(this._index[f] == undefined){ return []; }
|
||
|
|
||
|
if(this._index[f][v] == undefined){ return []; }
|
||
|
|
||
|
var r = {};
|
||
|
for(var i in this._index[f][v]){
|
||
|
if(r[i] == undefined){ r[i] = 0; }
|
||
|
r[i] += this._index[f][v][i];
|
||
|
}
|
||
|
|
||
|
return this.sortresult(r);
|
||
|
}
|
||
|
|
||
|
|
||
|
this.sortresult = function(o)
|
||
|
{
|
||
|
var t = [];
|
||
|
|
||
|
for(var i in o){ t.push([i, o[i]]); }
|
||
|
t.sort(function(a,b){ return b[1] - a[1]; });
|
||
|
|
||
|
var r = [];
|
||
|
for(var i in t){ r.push(t[i][0]); }
|
||
|
|
||
|
return r;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
var t = new Tidx();
|
||
|
|
||
|
function load(){
|
||
|
var output = document.getElementById('output');
|
||
|
|
||
|
var s = new Date().getTime();
|
||
|
for(var i = 0; i < json.length; i++){
|
||
|
for(var f in json[i]){
|
||
|
if(f != 'id'){ t.index(json[i]['id'], f, json[i][f]); }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
output.value = "Indexed in " + (new Date().getTime() - s) + "ms";
|
||
|
|
||
|
s = new Date().getTime();
|
||
|
var r = t.fsearch('desc', 'amet');
|
||
|
output.value = output.value + "\n" + "Searched in " + (new Date().getTime() - s) + "ms";
|
||
|
|
||
|
output.value = output.value + "\n" + r;
|
||
|
}
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|