search_knowledge_base_backend.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class SearchKnowledgeBaseBackend
  3. attr_reader :knowledge_base
  4. # @param [Hash] params the paramsused to initialize search instance
  5. # @option params [KnowledgeBase, <KnowledgeBase>] :knowledge_base (nil) knowledge base instance
  6. # @option params [KnowledgeBase::Locale, <KnowledgeBase::Locale>, String] :locale (nil) KB Locale or string identifier
  7. # @option params [KnowledgeBase::Category] :scope (nil) optional search scope
  8. # @option params [Symbol] :flavor (:public) agent or public to indicate source and narrow down to internal or public answers accordingly
  9. # @option params [String, Array<String>] :index (nil) indexes to limit search to, searches all indexes if nil
  10. # @option params [Integer] :limit per page param for paginatin
  11. # @option params [Boolean] :highlight_enabled (true) highlight matching text
  12. # @option params [Hash<String=>String>, Hash<Symbol=>Symbol>] :order_by hash with column => asc/desc
  13. def initialize(params)
  14. @params = params.compact
  15. prepare_scope_ids
  16. end
  17. def use_internal_assets?
  18. flavor == :agent && KnowledgeBase.granular_permissions?
  19. end
  20. def search(query, user: nil, pagination: nil)
  21. if use_internal_assets? # cache for later use
  22. @granular_permissions_handler = KnowledgeBase::InternalAssets.new(user)
  23. end
  24. raw_results = raw_results(query, user, pagination: pagination)
  25. filtered = filter_results raw_results, user
  26. if pagination
  27. filtered = filtered.slice pagination.offset, pagination.limit
  28. elsif @params[:limit]
  29. filtered = filtered.slice 0, @params[:limit]
  30. end
  31. filtered
  32. end
  33. def search_fallback(query, indexes, options)
  34. indexes
  35. .map { |index| search_fallback_for_index(query, index, options) }
  36. .flatten
  37. end
  38. def search_fallback_for_index(query, index, options)
  39. index
  40. .constantize
  41. .search_fallback("%#{query}%", @cached_scope_ids, options: options)
  42. .where(kb_locale: kb_locales)
  43. .reorder(**search_fallback_order)
  44. .pluck(:id)
  45. .map { |id| { id: id, type: index } }
  46. end
  47. def search_fallback_order
  48. @params[:order_by].presence || { updated_at: :desc }
  49. end
  50. def raw_results(query, user, pagination: nil)
  51. return search_fallback(query, indexes, { user: user }) if !SearchIndexBackend.enabled?
  52. SearchIndexBackend
  53. .search(query, indexes, options(pagination: pagination))
  54. .map do |hash|
  55. hash[:id] = hash[:id].to_i
  56. hash
  57. end
  58. end
  59. def filter_results(raw_results, user)
  60. raw_results
  61. .group_by { |result| result[:type] }
  62. .map { |group_name, grouped_results| filter_type(group_name, grouped_results, user) }
  63. .flatten
  64. end
  65. def filter_type(type, grouped_results, user)
  66. translation_ids = translation_ids_for_type(type, user)
  67. if !translation_ids
  68. return []
  69. end
  70. grouped_results.select { |result| translation_ids&.include? result[:id].to_i }
  71. end
  72. def translation_ids_for_type(type, user)
  73. case type
  74. when KnowledgeBase::Answer::Translation.name
  75. translation_ids_for_answers(user)
  76. when KnowledgeBase::Category::Translation.name
  77. translation_ids_for_categories(user)
  78. when KnowledgeBase::Translation.name
  79. translation_ids_for_kbs(user)
  80. end
  81. end
  82. def translation_ids_for_answers(user)
  83. scope = KnowledgeBase::Answer
  84. .joins(:category)
  85. .where(knowledge_base_categories: { knowledge_base_id: knowledge_bases })
  86. .then do |relation|
  87. if use_internal_assets? # cache for later use
  88. relation.where(id: @granular_permissions_handler.all_answer_ids)
  89. elsif user&.permissions?('knowledge_base.editor')
  90. relation
  91. elsif user&.permissions?('knowledge_base.reader') && flavor == :agent
  92. relation.internal
  93. else
  94. relation.published
  95. end
  96. end
  97. flatten_translation_ids(scope)
  98. end
  99. def translation_ids_for_categories(user)
  100. scope = KnowledgeBase::Category.where(knowledge_base_id: knowledge_bases)
  101. if use_internal_assets?
  102. flatten_translation_ids scope.where(id: @granular_permissions_handler.all_category_ids)
  103. elsif user&.permissions?('knowledge_base.editor')
  104. flatten_translation_ids scope
  105. elsif user&.permissions?('knowledge_base.reader') && flavor == :agent
  106. flatten_answer_translation_ids(scope, :internal)
  107. else
  108. flatten_answer_translation_ids(scope, :public)
  109. end
  110. end
  111. def translation_ids_for_kbs(_user)
  112. flatten_translation_ids KnowledgeBase.active.where(id: knowledge_bases)
  113. end
  114. def indexes
  115. return Array(@params.fetch(:index)) if @params.key?(:index)
  116. %w[
  117. KnowledgeBase::Answer::Translation
  118. KnowledgeBase::Category::Translation
  119. KnowledgeBase::Translation
  120. ]
  121. end
  122. def kb_locales
  123. @kb_locales ||= begin
  124. case @params.fetch(:locale, nil)
  125. when KnowledgeBase::Locale
  126. Array(@params.fetch(:locale))
  127. when String
  128. KnowledgeBase::Locale
  129. .joins(:system_locale)
  130. .where(knowledge_base_id: knowledge_bases, locales: { locale: @params.fetch(:locale) })
  131. else
  132. KnowledgeBase::Locale
  133. .where(knowledge_base_id: knowledge_bases)
  134. end
  135. end
  136. end
  137. def kb_locales_in(knowledge_base_id)
  138. @kb_locales_in ||= {}
  139. @kb_locales_in[knowledge_base_id] ||= @kb_locales.select { |locale| locale.knowledge_base_id == knowledge_base_id }
  140. end
  141. def kb_locale_ids
  142. @kb_locale_ids ||= kb_locales.pluck(:id)
  143. end
  144. def knowledge_bases
  145. @knowledge_bases ||= begin
  146. if @params.key? :knowledge_base
  147. Array(@params.fetch(:knowledge_base))
  148. else
  149. KnowledgeBase.active
  150. end
  151. end
  152. end
  153. def flavor
  154. @params.fetch(:flavor, :public).to_sym
  155. end
  156. def base_options
  157. {
  158. query_extension: {
  159. bool: {
  160. must: [ { terms: { kb_locale_id: kb_locale_ids } } ]
  161. }
  162. }
  163. }
  164. end
  165. def options_apply_query_fields(hash)
  166. return if flavor == :agent
  167. hash[:query_fields_by_indexes] = {
  168. 'KnowledgeBase::Answer::Translation': %w[title content.body attachment.content tags],
  169. 'KnowledgeBase::Category::Translation': %w[title],
  170. 'KnowledgeBase::Translation': %w[title]
  171. }
  172. end
  173. def options_apply_highlight(hash)
  174. return if !@params.fetch(:highlight_enabled, true)
  175. hash[:highlight_fields_by_indexes] = {
  176. 'KnowledgeBase::Answer::Translation': %w[title content.body tags],
  177. 'KnowledgeBase::Category::Translation': %w[title],
  178. 'KnowledgeBase::Translation': %w[title]
  179. }
  180. end
  181. def options_apply_scope(hash)
  182. return if !@params.fetch(:scope, nil)
  183. hash[:query_extension][:bool][:must].push({ terms: { scope_id: @cached_scope_ids } })
  184. end
  185. def options_apply_pagination(hash, pagination)
  186. if @params[:from] && @params[:limit]
  187. hash[:from] = @params[:from]
  188. hash[:limit] = @params[:limit]
  189. elsif pagination
  190. hash[:from] = 0
  191. hash[:limit] = pagination.limit * 99
  192. end
  193. end
  194. def options_apply_order(hash)
  195. return if @params[:order_by].blank?
  196. hash[:sort_by] = @params[:order_by].keys
  197. hash[:order_by] = @params[:order_by].values
  198. end
  199. def options_apply_fulltext(hash)
  200. hash[:fulltext] = true
  201. end
  202. def options(pagination: nil)
  203. output = base_options
  204. options_apply_query_fields(output)
  205. options_apply_highlight(output)
  206. options_apply_scope(output)
  207. options_apply_pagination(output, pagination)
  208. options_apply_order(output)
  209. options_apply_fulltext(output)
  210. output
  211. end
  212. def flatten_translation_ids(collection)
  213. collection
  214. .eager_load(:translations)
  215. .map { |elem| elem.translations.pluck(:id) }
  216. .flatten
  217. end
  218. def flatten_answer_translation_ids(collection, visibility)
  219. collection
  220. .eager_load(:translations)
  221. .map { |elem| visible_category_translation_ids(elem, visibility) }
  222. .flatten
  223. end
  224. def visible_category_translation_ids(category, visibility)
  225. category
  226. .translations
  227. .to_a
  228. .select { |elem| visible_translation?(elem, visibility) }
  229. .pluck(:id)
  230. end
  231. def visible_translation?(translation, visibility)
  232. if kb_locales_in(translation.category.knowledge_base_id).exclude?(translation.kb_locale)
  233. return false
  234. end
  235. translation.category.send("#{visibility}_content?", translation.kb_locale)
  236. end
  237. def prepare_scope_ids
  238. return if !@params.key? :scope
  239. @cached_scope_ids = @params.fetch(:scope).self_with_children_ids
  240. end
  241. end