search_knowledge_base_backend.rb 6.3 KB

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