Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add search functionality of static pages to Ruby on Rails app without a database?
    text
    copied!<p>Anyone know of any good gems or documentation on how to index static pages within a rails app to add search functionality? So far my search has led me to <a href="http://sunspot.github.io/" rel="nofollow">Sunspot</a> and <a href="https://www.ruby-toolbox.com/projects/cobweb" rel="nofollow">Cobweb</a>, but both seem to be a little more complicated than what I'm trying to achieve.</p> <p>Here's an example of how my views directory looks:</p> <pre><code>views | |__Folder_1 | |__ View-1 |__ View-2 | Folder_2 | |__ View-3 |__ View-4 </code></pre> <p>Each folder is a controller with the views as defined actions if that makes any difference when considering how to set this up. The end goal is to return a list of links for pages that include the searched terms.</p> <p><strong>Edit:</strong></p> <p>Each search query is intended to crawl through the HTML contents of all static pages and return a list of links for pages that match any non stop word term searched. I also plan on adding relevancy to the search based on the frequency of the searched terms within a static page and word placement. </p> <p>Example:</p> <p>Search Query: "Recipe for scrambled eggs" - Would return a link for any page with the words "recipe", "scrambled", and "eggs" with the most relevant links placed at the top of the returned list:</p> <pre><code>Search Results: Page 1 (Most relevant because includes all 3 terms) Page 2 (Includes 2 terms) Page 3 (Includes 1 terms) </code></pre> <p>Preferably, the search functionality would only attempt to match searched terms to the text of each view so that if a user entered 'div' as a search term it would not return every single page because div elements exist within the HTML content.</p> <p><strong>Answer:</strong></p> <p>After a few weeks of studying Ruby this is what I've come up with - Basically I'm filtering through each sub directory within my /app/views/ directory, reading each file within the sub directory's contents, processing the text to remove HTML tags and common stop words, and storing it within a search index hash.</p> <p>search_controller.rb</p> <pre><code>#include sanitize helper to enable use of strip_tags method in controller include ActionView::Helpers::SanitizeHelper class SearchController &lt; ApplicationController prepend_before_filter :search def search if params[:q] stopwords = ["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "aren't", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "can't", "cannot", "could", "couldn't", "did", "didn't", "do", "does", "doesn't", "doing", "don't", "down", "during", "each", "few", "for", "from", "further", "had", "hadn't", "has", "hasn't", "have", "haven't", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "isn't", "it", "it's", "its", "itself", "let's", "me", "more", "most", "mustn't", "my", "myself", "no", "nor", "not", "of", "off", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "shan't", "she", "she'd", "she'll", "she's", "should", "shouldn't", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "wasn't", "we", "we'd", "we'll", "we're", "we've", "were", "weren't", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "won't", "would", "wouldn't", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself"] #cleanse all stop words from search query @search_terms = strip_tags(params[:q]).downcase.split.delete_if{|x| stopwords.include?(x)} #declare empty index hash @search_index = {} #filter through each view and add view text to search entry Rails.root.join('app', "views").entries.each do |view_dir| unless %w(. .. search shared layouts).include?(view_dir.to_s) Rails.root.join('app', "views", view_dir.to_s).entries.each do |view| unless %w(. ..).include?(view.to_s) #add relative path for view and processed contents to search index hash as key, value pair @search_index["/" + view_dir.to_s + "/" + view.to_s.gsub('.html.erb', '')] = strip_tags(IO.read(Rails.root.join('app', "views", view_dir.to_s, view.to_s))).downcase.squish.split.delete_if{|x| stopwords.include?(x)}.join(" ") end end end end end end end </code></pre> <p>If anyone has any improvements or suggestions I would love to hear them!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload