search_knowledge_base_backend.rb 6.5 KB

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