Railsアプリ内の静的ページにインデックスを付けて検索機能を追加する方法に関する優れたgemまたはドキュメントを知っている人はいますか? これまでのところ、検索の結果、SunspotとCobwebにたどり着きましたが、どちらも私が達成しようとしているものよりも少し複雑なようです。
私のビューディレクトリがどのように見えるかの例を次に示します:
views
|
|__Folder_1
|
|__ View-1
|__ View-2
|
Folder_2
|
|__ View-3
|__ View-4
これを設定する方法を検討するときに違いが生じる場合、各フォルダーは定義されたアクションとしてのビューを持つコントローラーです。最終的な目標は、検索された用語を含むページへのリンクのリストを返すことです。
編集:
各検索クエリは、すべての静的ページの HTML コンテンツをクロールし、検索されたストップ ワード以外の用語に一致するページのリンクのリストを返すことを目的としています。また、静的なページ内で検索された用語の頻度と単語の配置に基づいて、検索に関連性を追加する予定です。
例:
検索クエリ: 「スクランブルエッグのレシピ」 - 「レシピ」、「スクランブルエッグ」、「卵」という単語を含むページへのリンクを返し、最も関連性の高いリンクが返されたリストの一番上に配置されます。
Search Results:
Page 1 (Most relevant because includes all 3 terms)
Page 2 (Includes 2 terms)
Page 3 (Includes 1 terms)
できれば、検索機能は、検索語を各ビューのテキストに一致させようとするだけで、ユーザーが検索語として「div」を入力した場合、HTML コンテンツ内に div 要素が存在するため、すべてのページが返されるわけではありません。
答え:
Ruby を数週間勉強した後、これが私が思いついたものです - 基本的に、/app/views/ ディレクトリ内の各サブディレクトリをフィルタリングし、サブディレクトリのコンテンツ内の各ファイルを読み取り、テキストを処理して HTML を削除していますタグと一般的なストップ ワードを検索し、検索インデックス ハッシュ内に格納します。
search_controller.rb
#include sanitize helper to enable use of strip_tags method in controller
include ActionView::Helpers::SanitizeHelper
class SearchController < 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
改善点や提案があれば、ぜひお聞かせください。