search_knowledge_base_backend.rb 8.3 KB

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