engine.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const _ = require('lodash')
  2. const tsquery = require('pg-tsquery')()
  3. module.exports = {
  4. async activate() {
  5. // not used
  6. },
  7. async deactivate() {
  8. // not used
  9. },
  10. /**
  11. * INIT
  12. */
  13. async init() {
  14. // -> Create Index
  15. const indexExists = await WIKI.models.knex.schema.hasTable('pagesVector')
  16. if (!indexExists) {
  17. await WIKI.models.knex.schema.createTable('pagesVector', table => {
  18. table.increments()
  19. table.string('path')
  20. table.string('locale')
  21. table.string('title')
  22. table.string('description')
  23. table.specificType('titleTk', 'TSVECTOR')
  24. table.specificType('descriptionTk', 'TSVECTOR')
  25. table.specificType('contentTk', 'TSVECTOR')
  26. })
  27. }
  28. },
  29. /**
  30. * QUERY
  31. *
  32. * @param {String} q Query
  33. * @param {Object} opts Additional options
  34. */
  35. async query(q, opts) {
  36. try {
  37. const results = await WIKI.models.knex.raw(`
  38. SELECT id, path, locale, title, description
  39. FROM "pagesVector", to_tsquery(?) query
  40. WHERE (query @@ "titleTk") OR (query @@ "descriptionTk") OR (query @@ "contentTk")
  41. `, [tsquery(q)])
  42. return {
  43. results: results.rows,
  44. suggestions: [],
  45. totalHits: results.rows.length
  46. }
  47. } catch (err) {
  48. WIKI.logger.warn('Search Engine Error:')
  49. WIKI.logger.warn(err)
  50. }
  51. },
  52. /**
  53. * CREATE
  54. *
  55. * @param {Object} page Page to create
  56. */
  57. async created(page) {
  58. await WIKI.models.knex.raw(`
  59. INSERT INTO "pagesVector" (path, locale, title, description, "titleTk", "descriptionTk", "contentTk") VALUES (
  60. '?', '?', '?', '?', to_tsvector('?'), to_tsvector('?'), to_tsvector('?')
  61. )
  62. `, [page.path, page.locale, page.title, page.description, page.title, page.description, page.content])
  63. },
  64. /**
  65. * UPDATE
  66. *
  67. * @param {Object} page Page to update
  68. */
  69. async updated(page) {
  70. await WIKI.models.knex.raw(`
  71. UPDATE "pagesVector" SET
  72. title = '?',
  73. description = '?',
  74. "titleTk" = to_tsvector('?'),
  75. "descriptionTk" = to_tsvector('?'),
  76. "contentTk" = to_tsvector('?')
  77. WHERE path = '?' AND locale = '?' LIMIT 1
  78. `, [page.title, page.description, page.title, page.description, page.content, page.path, page.locale])
  79. },
  80. /**
  81. * DELETE
  82. *
  83. * @param {Object} page Page to delete
  84. */
  85. async deleted(page) {
  86. await WIKI.models.knex('pagesVector').where({
  87. locale: page.locale,
  88. path: page.path
  89. }).del().limit(1)
  90. },
  91. /**
  92. * RENAME
  93. *
  94. * @param {Object} page Page to rename
  95. */
  96. async renamed(page) {
  97. await WIKI.models.knex('pagesVector').where({
  98. locale: page.locale,
  99. path: page.sourcePath
  100. }).update({
  101. locale: page.locale,
  102. path: page.destinationPath
  103. }).limit(1)
  104. },
  105. /**
  106. * REBUILD INDEX
  107. */
  108. async rebuild() {
  109. await WIKI.models.knex('pagesVector').truncate()
  110. await WIKI.models.knex.raw(`
  111. INSERT INTO "pagesVector" (path, locale, title, description, "titleTk", "descriptionTk", "contentTk")
  112. SELECT path, "localeCode" AS locale, title, description, to_tsvector(title) AS "titleTk", to_tsvector(description) AS "descriptionTk", to_tsvector(content) AS "contentTk"
  113. FROM "pages"
  114. WHERE pages."isPublished" AND NOT pages."isPrivate"`)
  115. }
  116. }