Browse Source

Added better examples and created new README

master
Christopher Ramey 12 years ago
committed by cdramey
parent
commit
38a9078e8e
  1. 1
      README
  2. 78
      README.md
  3. 6
      TODO
  4. 111
      example/advanced.html
  5. 96
      example/basic.html
  6. 0
      example/test.json
  7. 123
      test.html

1
README

@ -1 +0,0 @@
Tidx is a simple full text indexing library for Javascript

78
README.md

@ -0,0 +1,78 @@
Tidx
====
Tidx is a simple Javascript full text indexing library. Tidx is
BSD licensed.
basic usage
-----------
First, create a new Tidx object:
var idx = new Tidx();
Next, add your data to the index with the index function:
idx.index(true, 1, 'Name', 'John D. Smith');
The index function accepts four arguments: The first is a boolean
which is if the data to be added to the index should be tokenized (that is,
broken down into discrete searchable terms), the second is the unique
identifier for the data (which will be returned when you search), a field
name associated with the data (or null if you don't have a field) and the
value you wish to add to the index. In the above example, since we've enabled
tokenization, the value would be indexed as three terms "john", "d" and
"smith".
Searching your index is easy:
var results = idx.search(true, 'john');
The search function accepts two arguments: A boolean argument of true
if you wish your search terms to be logically conjuct (as if connected
by AND) or false if you want your terms to be logically disjunct (as if
connected by OR). The second argument is your search terms. The search
function supports multiword terms when surrounded by quotes "John Smith",
and fields can be specified by name if using a semi-colon (e.g. "name:john")
The returned results of the search function is an array of IDs ordered by
frequency of terms from your search.
Fully functional examples of usage can be found in the examples directory.
advanced usage
--------------
Tidx also supports chaining searches together. This is useful in instances
where you may have multiple points of entry for a single search, and the
results should be an aggregate of each. After your index is initialized
and data has been added (see above), you can then search it using raw_search:
var raw_results = {};
var term_count = idx.raw_search('John', raw_results);
Additional searches may then be added in the same way:
term_count += idx.raw_search('Smith', raw_results);
Field specific, single term scans may also be included:
term_count += idx.field_scan('Name', 'David', raw_results);
Finally, the results can be ordered and filtered:
var results = idx.order(raw_results, term_count);
If you'd like the results of your search terms to be logically conjunct
(as if connected by AND), specific the term count you collected from search.
If you'd like them to be logically disjunct (as if connected by OR), specify
a term count of zero.

6
TODO

@ -1,9 +1,3 @@
Coming soon
===========
Better README
Clearer function comments
Maybe one day
=============
Larger/More extensive set of test data

111
example/advanced.html

@ -0,0 +1,111 @@
<html>
<head>
<title>Tidx Test</title>
<style type="text/css">
#result_table td { vertical-align: top; }
td { vertical-align: bottom; }
th { text-align: left; }
#output { font-style: italic; }
</style>
</head>
<body>
<table>
<tr>
<td><label for="search">Multiterm search:</label></td>
<td><input type="text" id="search" name="search" size="50" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td><label for="email">Email search:</label></td>
<td><input type="text" id="email" name="email" size="50" /></td>
<td><input type="button" id="btn_search" value="Search"/></td>
<td><div id="output"></div></td>
</tr>
</table>
<br/>
<table id="result_table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Rank</th>
<th>Description</th>
</tr>
</thead>
<tbody id="result_tbody"></tbody>
</table>
<script type="text/javascript" src="../tidx.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var idx = new Tidx();
var json = null;
$(function(){
var output = $('#output');
// Load test JSON
output.html('Loading .. ');
$.getJSON('test.json', function(data){
json = data;
var s = new Date().getTime();
for(var i = 0; i < json.length; i++){
for(var f in json[i]){
switch(f){
case 'id': break; // Don't index the ID
case 'email': idx.index(false, i, f, json[i][f]); break;
default: idx.index(true, i, f, json[i][f]); break;
}
}
}
output.html('test.json indexed in ' + (new Date().getTime() - s) + "ms\n");
});
// Setup the search button
$('#btn_search').click(function(){
var s = new Date().getTime();
var raw_results = {};
// Conduct the multiterm search
var term_count = idx.raw_search($('#search').val(), raw_results);
// Add in the field specific scan of 'email'
term_count += idx.field_scan('email', $('#email').val(), raw_results);
// Process the results
var results = idx.order(raw_results, term_count);
var rtbl = $('#result_tbody').empty();
for(var i in results){
var row = json[results[i]];
rtbl.append(
'<tr>' +
'<td>' + row['id'] + '</td>' +
'<td>' + row['name'] + '</td>' +
'<td>' + row['email'] + '</td>' +
'<td>' + row['city'] + '</td>' +
'<td>' + row['state'] + '</td>' +
'<td>' + row['country'] + '</td>' +
'<td>' + row['rank'] + '</td>' +
'<td>' + row['desc'] + '</td>' +
'</tr>'
);
}
});
// Enter should work to search, too
$('#search').keydown(function(e){
if(e.which == 13){ $('#btn_search').click(); }
}).focus();
});
</script>
</body>
</html>

96
example/basic.html

@ -0,0 +1,96 @@
<html>
<head>
<title>Tidx Test</title>
<style type="text/css">
#result_table td { vertical-align: top; }
td { vertical-align: bottom; }
th { text-align: left; }
#output { font-style: italic; }
</style>
</head>
<body>
<table>
<tr>
<td><label for="search">Multiterm search:</label></td>
<td><input type="text" id="search" name="search" size="50" /></td>
<td><input type="button" id="btn_search" value="Search"/></td>
<td><div id="output"></div></td>
</tr>
</table>
<br/>
<table id="result_table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Rank</th>
<th>Description</th>
</tr>
</thead>
<tbody id="result_tbody"></tbody>
</table>
<script type="text/javascript" src="../tidx.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var idx = new Tidx();
var json = null;
$(function(){
var output = $('#output');
// Load test JSON
output.html('Loading .. ');
$.getJSON('test.json', function(data){
json = data;
var s = new Date().getTime();
for(var i = 0; i < json.length; i++){
for(var f in json[i]){
// Index all the fields in json except ID
if(f != 'id'){ idx.index(true, i, f, json[i][f]); }
}
}
output.html('test.json indexed in ' + (new Date().getTime() - s) + "ms\n");
});
// Setup the search button
$('#btn_search').click(function(){
var s = new Date().getTime();
var results = idx.search(true, $('#search').val());
output.html('Search completed in ' + (new Date().getTime() - s) + "ms\n");
var rtbl = $('#result_tbody').empty();
for(var i in results){
var row = json[results[i]];
rtbl.append(
'<tr>' +
'<td>' + row['id'] + '</td>' +
'<td>' + row['name'] + '</td>' +
'<td>' + row['email'] + '</td>' +
'<td>' + row['city'] + '</td>' +
'<td>' + row['state'] + '</td>' +
'<td>' + row['country'] + '</td>' +
'<td>' + row['rank'] + '</td>' +
'<td>' + row['desc'] + '</td>' +
'</tr>'
);
}
});
// Enter should work to search, too
$('#search').keydown(function(e){
if(e.which == 13){ $('#btn_search').click(); }
}).focus();
});
</script>
</body>
</html>

0
test.json → example/test.json

123
test.html

@ -1,123 +0,0 @@
<html>
<head>
<title>Tidx Test</title>
<style type="text/css">
td { vertical-align: top; }
th { text-align: left; }
</style>
</head>
<body>
<table>
<tr>
<td><label for="or_search">Logical OR Search:</label></td>
<td><input type="text" id="or_search" name="search" size="50" /></td>
<td><input type="button" id="btn_or_search" value="Search"/></td>
</tr>
<tr>
<td><label for="and_search">Logical AND Search:</label></td>
<td><input type="text" id="and_search" name="search" size="50" /></td>
<td><input type="button" id="btn_and_search" value="Search" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2">
<textarea name="output" id="output" rows="10" cols="45"></textarea>
</td>
</tr>
</table>
<br/>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Rank</th>
<th>Description</th>
</tr>
</thead>
<tbody id="result_table"></tbody>
</table>
<script type="text/javascript" src="tidx.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var t = new Tidx();
var json = null;
$(function(){
var output = $('#output');
// Load test JSON
output.val('Loading test.json .. ');
$.getJSON('test.json', function(data){
json = data;
output.val(output.val() + "loaded\nIndexing test.json .. ");
var s = new Date().getTime();
for(var i = 0; i < json.length; i++){
for(var f in json[i]){
switch(f){
case 'id': break;
case 'email': t.index(false, i, f, json[i][f]); break;
default: t.index(true, i, f, json[i][f]); break;
}
}
}
output.val(output.val() + 'indexed in ' + (new Date().getTime() - s) + "ms\n");
});
// Setup the search buttons
$('#btn_or_search').click(function(){
dosearch(false, $('#or_search').val());
});
$('#btn_and_search').click(function(){
dosearch(true, $('#and_search').val());
});
// Enter should work to search, too
$('#or_search').keydown(function(e){
if(e.which == 13){ $('#btn_or_search').click(); }
});
$('#and_search').keydown(function(e){
if(e.which == 13){ $('#btn_and_search').click(); }
}).focus();
});
function dosearch(and_search, value){
var output = $('#output');
output.val(output.val() + 'Searching .. ');
var s = new Date().getTime();
var results = t.search(and_search, value);
output.val(output.val() + 'completed in ' + (new Date().getTime() - s) + "ms\n");
var rtbl = $('#result_table').empty();
for(var idx in results){
var i = results[idx];
rtbl.append(
'<tr>' +
'<td>' + json[i]['id'] + '</td>' +
'<td>' + json[i]['name'] + '</td>' +
'<td>' + json[i]['email'] + '</td>' +
'<td>' + json[i]['city'] + '</td>' +
'<td>' + json[i]['state'] + '</td>' +
'<td>' + json[i]['country'] + '</td>' +
'<td>' + json[i]['rank'] + '</td>' +
'<td>' + json[i]['desc'] + '</td>' +
'</tr>'
);
}
}
</script>
</body>
</html>