fulltext-search.js 980 B

123456789101112131415161718192021222324252627282930313233343536
  1. window.Searcher = (function() {
  2. function Searcher() {
  3. this._index = lunr(function () {
  4. this.field('title', {boost: 10})
  5. this.field('body')
  6. this.ref('id')
  7. }) ;
  8. this._indexContent = undefined;
  9. }
  10. Searcher.prototype.init = function() {
  11. var self = this;
  12. $("script[type='text/x-docstrap-searchdb']").each(function(idx, item) {
  13. self._indexContent = JSON.parse(item.innerHTML);
  14. for (var entryId in self._indexContent) {
  15. self._index.add(self._indexContent[entryId]);
  16. }
  17. });
  18. };
  19. Searcher.prototype.search = function(searchTerm) {
  20. var results = [],
  21. searchResults = this._index.search(searchTerm);
  22. for (var idx = 0; idx < searchResults.length; idx++) {
  23. results.push(this._indexContent[searchResults[idx].ref])
  24. }
  25. return results;
  26. };
  27. return new Searcher();
  28. })();