durfdoen-2.0/lib/helpers/search.rb

41 lines
1 KiB
Ruby
Raw Normal View History

2020-09-09 21:54:22 +02:00
require 'json'
require 'nokogiri'
module SearchHelper
class CreateFullTextIndex
COMMON_WORDS = %w{ a about above across ... } unless defined?(COMMON_WORDS)
def initialize(articles)
@articles = articles
end
def call
@articles.map do |item|
words = item.raw_content().downcase.split(/\W+/)
keywords = words.uniq - COMMON_WORDS
{
url: item.path,
id: item[:id],
title: item[:titel],
verkort: item[:verkorte_naam],
konvent: item[:konvent],
body: keywords.join(" ")
}
end.to_a
end
end
2020-09-15 21:44:23 +02:00
def to_partials_search(items)
my_hash = {}
2020-09-09 21:54:22 +02:00
2020-09-15 21:44:23 +02:00
items.each do |item|
2020-09-16 01:40:48 +02:00
my_hash[(item.path)] = {
html: (render '/partials/pretty_link.*', :item => item).gsub("\n", ''),
titel: item[:titel] || item[:naam]
}
2020-09-09 21:54:22 +02:00
end
2020-09-15 21:44:23 +02:00
my_hash.to_json
2020-09-09 21:54:22 +02:00
end
end