pimbongaerts / mesophotic

Ruby on Rails application to host scientific information on mesophotic coral ecosystems
http://www.mesophotic.org
MIT License
1 stars 1 forks source link

Export word search query as new column in csv #146

Open veronicar239 opened 8 months ago

veronicar239 commented 8 months ago

@ryanbooker Our goal is to export the number of occurrences for each publication that come up in a search query on the Publications page. At the moment, the search query displays the number of occurrences of the search word for each publication on the left side. We're hoping to get this number into a new column (something like "word count" or "query word count") in the exported csv file. @pimbongaerts to chime in to help explain if there's a better explanation. Thanks!

Screen Shot 2024-01-10 at 9 04 13 AM
pimbongaerts commented 5 months ago

These counts relate to relevance:

    <td>
      <span><%= publication.relevance %> <span>occurrence(s)</span></span>
    </td>

as defined in this scope:

  scope :relevance, -> (search_term, search_params = Publication.default_search_params) {
    if search_term.present?
      st = ActiveRecord::Base.sanitize_sql_for_conditions ["?", search_term]

      filter = (search_params["search_fields"] || PUBLICATION_SEARCH_FIELDS).map { |field|
        "(IFNULL(LENGTH(#{field}), 0) - IFNULL(LENGTH(REPLACE(LOWER(#{field}), LOWER(#{st}), '')), 0))"
      }.join(" + ")

      records = select("*, (#{filter}) / LENGTH(#{st}) AS relevance")
      limited = records.where("relevance > 0")
      records
      .where("publications.id IN (SELECT id FROM (#{limited.to_sql}))")
      .base_search(search_params)
      .order("relevance DESC, publication_year DESC, filename ASC")
    end
  }

Is there a way to include these in the CSV scope, if should_show_relevance?