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.

81 lines
2.8 KiB

  1. const bunyan = require('bunyan')
  2. const level = require('levelup')
  3. const down = require('memdown')
  4. const SearchIndexAdder = require('search-index-adder')
  5. const SearchIndexSearcher = require('search-index-searcher')
  6. module.exports = function (givenOptions, moduleReady) {
  7. const optionsLoaded = function (err, SearchIndex) {
  8. const siUtil = require('./siUtil.js')(SearchIndex.options)
  9. if (err) return moduleReady(err)
  10. SearchIndex.close = siUtil.close
  11. SearchIndex.countDocs = siUtil.countDocs
  12. getAdder(SearchIndex, adderLoaded)
  13. }
  14. const adderLoaded = function (err, SearchIndex) {
  15. if (err) return moduleReady(err)
  16. getSearcher(SearchIndex, searcherLoaded)
  17. }
  18. const searcherLoaded = function (err, SearchIndex) {
  19. if (err) return moduleReady(err)
  20. return moduleReady(err, SearchIndex)
  21. }
  22. getOptions(givenOptions, optionsLoaded)
  23. }
  24. const getAdder = function (SearchIndex, done) {
  25. SearchIndexAdder(SearchIndex.options, function (err, searchIndexAdder) {
  26. SearchIndex.add = searchIndexAdder.add
  27. SearchIndex.callbackyAdd = searchIndexAdder.concurrentAdd // deprecated
  28. SearchIndex.concurrentAdd = searchIndexAdder.concurrentAdd
  29. SearchIndex.createWriteStream = searchIndexAdder.createWriteStream
  30. SearchIndex.dbWriteStream = searchIndexAdder.dbWriteStream
  31. SearchIndex.defaultPipeline = searchIndexAdder.defaultPipeline
  32. SearchIndex.del = searchIndexAdder.deleter
  33. SearchIndex.deleteStream = searchIndexAdder.deleteStream
  34. SearchIndex.flush = searchIndexAdder.flush
  35. done(err, SearchIndex)
  36. })
  37. }
  38. const getSearcher = function (SearchIndex, done) {
  39. SearchIndexSearcher(SearchIndex.options, function (err, searchIndexSearcher) {
  40. SearchIndex.availableFields = searchIndexSearcher.availableFields
  41. SearchIndex.buckets = searchIndexSearcher.bucketStream
  42. SearchIndex.categorize = searchIndexSearcher.categoryStream
  43. SearchIndex.dbReadStream = searchIndexSearcher.dbReadStream
  44. SearchIndex.get = searchIndexSearcher.get
  45. SearchIndex.match = searchIndexSearcher.match
  46. SearchIndex.scan = searchIndexSearcher.scan
  47. SearchIndex.search = searchIndexSearcher.search
  48. SearchIndex.totalHits = searchIndexSearcher.totalHits
  49. done(err, SearchIndex)
  50. })
  51. }
  52. const getOptions = function (options, done) {
  53. var SearchIndex = {}
  54. SearchIndex.options = Object.assign({}, {
  55. indexPath: 'si',
  56. keySeparator: '○',
  57. logLevel: 'error'
  58. }, options)
  59. options.log = bunyan.createLogger({
  60. name: 'search-index',
  61. level: options.logLevel
  62. })
  63. if (!options.indexes) {
  64. level(SearchIndex.options.indexPath || 'si', {
  65. valueEncoding: 'json',
  66. db: down
  67. }, function (err, db) {
  68. SearchIndex.options.indexes = db
  69. return done(err, SearchIndex)
  70. })
  71. } else {
  72. return done(null, SearchIndex)
  73. }
  74. }