search_knowledge_base_backend.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. class SearchKnowledgeBaseBackend
  2. attr_reader :knowledge_base
  3. # @param [Hash] params the paramsused to initialize search instance
  4. # @option params [KnowledgeBase, <KnowledgeBase>] :knowledge_base (nil) knowledge base instance
  5. # @option params [KnowledgeBase::Locale, <KnowledgeBase::Locale>, String] :locale (nil) KB Locale or string identifier
  6. # @option params [KnowledgeBase::Category] :scope (nil) optional search scope
  7. # @option params [Symbol] :flavor (:public) agent or public to indicate source and narrow down to internal or public answers accordingly
  8. # @option params [String, Array<String>] :index (nil) indexes to limit search to, searches all indexes if nil
  9. # @option params [Boolean] :highlight_enabled (true) highlight matching text
  10. def initialize(params)
  11. @params = params.compact
  12. prepare_scope_ids
  13. end
  14. def search(query, user: nil)
  15. raw_results = if SearchIndexBackend.enabled?
  16. SearchIndexBackend.search(query, indexes, options)
  17. else
  18. search_fallback(query, indexes, { user: user })
  19. end
  20. if (limit = @params.fetch(:limit, nil))
  21. raw_results = raw_results[0, limit]
  22. end
  23. #raw_results
  24. filter_results raw_results, user
  25. end
  26. def search_fallback(query, indexes, options)
  27. indexes
  28. .map { |index| search_fallback_for_index(query, index, options) }
  29. .flatten
  30. end
  31. def search_fallback_for_index(query, index, options)
  32. index
  33. .constantize
  34. .search_fallback("%#{query}%", @cached_scope_ids, options: options)
  35. .where(kb_locale: kb_locales)
  36. .pluck(:id)
  37. .map { |id| { id: id, type: index } }
  38. end
  39. def filter_results(raw_results, user)
  40. raw_results
  41. .group_by { |result| result[:type] }
  42. .map { |group_name, grouped_results| filter_type(group_name, grouped_results, user) }
  43. .flatten
  44. end
  45. def filter_type(type, grouped_results, user)
  46. translation_ids = translation_ids_for_type(type, user)
  47. if !translation_ids
  48. return []
  49. end
  50. grouped_results.select { |result| translation_ids&.include? result[:id].to_i }
  51. end
  52. def translation_ids_for_type(type, user)
  53. case type
  54. when KnowledgeBase::Answer::Translation.name
  55. translation_ids_for_answers(user)
  56. when KnowledgeBase::Category::Translation.name
  57. translation_ids_for_categories(user)
  58. when KnowledgeBase::Translation.name
  59. translation_ids_for_kbs(user)
  60. end
  61. end
  62. def translation_ids_for_answers(user)
  63. scope = KnowledgeBase::Answer
  64. .joins(:category)
  65. .where(knowledge_base_categories: { knowledge_base_id: knowledge_bases })
  66. scope = if user&.permissions?('knowledge_base.editor')
  67. scope
  68. elsif user&.permissions?('knowledge_base.reader') && flavor == :agent
  69. scope.internal
  70. else
  71. scope.published
  72. end
  73. flatten_translation_ids(scope)
  74. end
  75. def translation_ids_for_categories(user)
  76. scope = KnowledgeBase::Category.where knowledge_base_id: knowledge_bases
  77. if user&.permissions?('knowledge_base.editor')
  78. flatten_translation_ids scope
  79. elsif user&.permissions?('knowledge_base.reader') && flavor == :agent
  80. flatten_answer_translation_ids(scope, :internal)
  81. else
  82. flatten_answer_translation_ids(scope, :public)
  83. end
  84. end
  85. def translation_ids_for_kbs(_user)
  86. flatten_translation_ids KnowledgeBase.active.where(id: knowledge_bases)
  87. end
  88. def indexes
  89. return Array(@params.fetch(:index)) if @params.key?(:index)
  90. %w[
  91. KnowledgeBase::Answer::Translation
  92. KnowledgeBase::Category::Translation
  93. KnowledgeBase::Translation
  94. ]
  95. end
  96. def kb_locales
  97. @kb_locales ||= begin
  98. case @params.fetch(:locale, nil)
  99. when KnowledgeBase::Locale
  100. Array(@params.fetch(:locale))
  101. when String
  102. KnowledgeBase::Locale
  103. .joins(:system_locale)
  104. .where(knowledge_base_id: knowledge_bases, locales: { locale: @params.fetch(:locale) })
  105. else
  106. KnowledgeBase::Locale
  107. .where(knowledge_base_id: knowledge_bases)
  108. end
  109. end
  110. end
  111. def kb_locales_in(knowledge_base_id)
  112. @kb_locales_in ||= {}
  113. @kb_locales_in[knowledge_base_id] ||= @kb_locales.select { |locale| locale.knowledge_base_id == knowledge_base_id }
  114. end
  115. def kb_locale_ids
  116. @kb_locale_ids ||= kb_locales.pluck(:id)
  117. end
  118. def knowledge_bases
  119. @knowledge_bases ||= begin
  120. if @params.key? :knowledge_base
  121. Array(@params.fetch(:knowledge_base))
  122. else
  123. KnowledgeBase.active
  124. end
  125. end
  126. end
  127. def flavor
  128. @params.fetch(:flavor, :public).to_sym
  129. end
  130. def options
  131. output = {
  132. query_extension: {
  133. bool: {
  134. must: [ { terms: { kb_locale_id: kb_locale_ids } } ]
  135. }
  136. }
  137. }
  138. if @params.fetch(:highlight_enabled, true)
  139. output[:highlight_fields_by_indexes] = {
  140. 'KnowledgeBase::Answer::Translation': %w[title content.body],
  141. 'KnowledgeBase::Category::Translation': %w[title],
  142. 'KnowledgeBase::Translation': %w[title]
  143. }
  144. end
  145. if @params.fetch(:scope, nil)
  146. scope = { terms: { scope_id: @cached_scope_ids } }
  147. output[:query_extension][:bool][:must].push scope
  148. end
  149. output
  150. end
  151. def flatten_translation_ids(collection)
  152. collection
  153. .eager_load(:translations)
  154. .map { |elem| elem.translations.pluck(:id) }
  155. .flatten
  156. end
  157. def flatten_answer_translation_ids(collection, visibility)
  158. collection
  159. .eager_load(:translations)
  160. .map { |elem| visible_category_translation_ids(elem, visibility) }
  161. .flatten
  162. end
  163. def visible_category_translation_ids(category, visibility)
  164. category
  165. .translations
  166. .to_a
  167. .select { |elem| visible_translation?(elem, visibility) }
  168. .pluck(:id)
  169. end
  170. def visible_translation?(translation, visibility)
  171. if !kb_locales_in(translation.category.knowledge_base_id).include? translation.kb_locale
  172. return false
  173. end
  174. translation.category.send("#{visibility}_content?", translation.kb_locale)
  175. end
  176. def prepare_scope_ids
  177. return if !@params.key? :scope
  178. @cached_scope_ids = @params.fetch(:scope).self_with_children_ids
  179. end
  180. end