a full text indexing library for javascript
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

111 lines
2.9 KiB

  1. <html>
  2. <head>
  3. <title>Tidx Test</title>
  4. <style type="text/css">
  5. #result_table td { vertical-align: top; }
  6. td { vertical-align: bottom; }
  7. th { text-align: left; }
  8. #output { font-style: italic; }
  9. </style>
  10. </head>
  11. <body>
  12. <table>
  13. <tr>
  14. <td><label for="search">Multiterm search:</label></td>
  15. <td><input type="text" id="search" name="search" size="50" /></td>
  16. <td colspan="2">&nbsp;</td>
  17. </tr>
  18. <tr>
  19. <td><label for="email">Email search:</label></td>
  20. <td><input type="text" id="email" name="email" size="50" /></td>
  21. <td><input type="button" id="btn_search" value="Search"/></td>
  22. <td><div id="output"></div></td>
  23. </tr>
  24. </table>
  25. <br/>
  26. <table id="result_table">
  27. <thead>
  28. <tr>
  29. <th>ID</th>
  30. <th>Name</th>
  31. <th>Email</th>
  32. <th>City</th>
  33. <th>State</th>
  34. <th>Country</th>
  35. <th>Rank</th>
  36. <th>Description</th>
  37. </tr>
  38. </thead>
  39. <tbody id="result_tbody"></tbody>
  40. </table>
  41. <script type="text/javascript" src="../tidx.js"></script>
  42. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  43. <script type="text/javascript">
  44. var idx = new Tidx();
  45. var json = null;
  46. $(function(){
  47. var output = $('#output');
  48. // Load test JSON
  49. output.html('Loading .. ');
  50. $.getJSON('test.json', function(data){
  51. json = data;
  52. var s = new Date().getTime();
  53. for(var i = 0; i < json.length; i++){
  54. for(var f in json[i]){
  55. switch(f){
  56. case 'id': break; // Don't index the ID
  57. case 'email': idx.index(false, i, f, json[i][f]); break;
  58. default: idx.index(true, i, f, json[i][f]); break;
  59. }
  60. }
  61. }
  62. output.html('test.json indexed in ' + (new Date().getTime() - s) + "ms\n");
  63. });
  64. // Setup the search button
  65. $('#btn_search').click(function(){
  66. var s = new Date().getTime();
  67. var raw_results = {};
  68. // Conduct the multiterm search
  69. var term_count = idx.raw_search($('#search').val(), raw_results);
  70. // Add in the field specific scan of 'email'
  71. term_count += idx.field_scan('email', $('#email').val(), raw_results);
  72. // Process the results
  73. var results = idx.order(raw_results, term_count);
  74. var rtbl = $('#result_tbody').empty();
  75. for(var i in results){
  76. var row = json[results[i]];
  77. rtbl.append(
  78. '<tr>' +
  79. '<td>' + row['id'] + '</td>' +
  80. '<td>' + row['name'] + '</td>' +
  81. '<td>' + row['email'] + '</td>' +
  82. '<td>' + row['city'] + '</td>' +
  83. '<td>' + row['state'] + '</td>' +
  84. '<td>' + row['country'] + '</td>' +
  85. '<td>' + row['rank'] + '</td>' +
  86. '<td>' + row['desc'] + '</td>' +
  87. '</tr>'
  88. );
  89. }
  90. });
  91. // Enter should work to search, too
  92. $('#search').keydown(function(e){
  93. if(e.which == 13){ $('#btn_search').click(); }
  94. }).focus();
  95. });
  96. </script>
  97. </body>
  98. </html>