Sha256: aa443d5c66af6d5638e56d478037573d4f75365805e4d9c20e4aec4fb246b187

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require 'sinatra/base'
require 'picky'
require File.expand_path '../logging', __FILE__

class BookSearch < Sinatra::Application

  # We do this so we don't have to type
  # Picky:: in front of everything.
  #
  include Picky

  # Define an index.
  #
  books_index = Indexes::Memory.new :books do
    source   Sources::CSV.new(:title, :author, :year, file: "data/#{PICKY_ENVIRONMENT}/library.csv")
    indexing removes_characters: /[^a-zA-Z0-9\s\/\-\_\:\"\&\.]/i,
             stopwords:          /\b(and|the|of|it|in|for)\b/i,
             splits_text_on:     /[\s\/\-\_\:\"\&\/]/
    category :title,
             similarity: Similarity::DoubleMetaphone.new(3),
             partial: Partial::Substring.new(from: 1) # Default is from: -3.
    category :author, partial: Partial::Substring.new(from: 1)
    category :year, partial: Partial::None.new
  end

  # Index and load on USR1 signal.
  #
  Signal.trap('USR1') do
    books_index.reindex # kill -USR1 <pid>
  end

  # Define a search over the books index.
  #
  books = Search.new books_index do
    searching removes_characters: /[^a-zA-Z0-9\s\/\-\_\&\.\"\~\*\:\,]/i, # Picky needs control chars *"~:, to pass through.
              stopwords:          /\b(and|the|of|it|in|for)\b/i,
              splits_text_on:     /[\s\/\-\&]+/,
              substitutes_characters_with: CharacterSubstituters::WestEuropean.new # Normalizes special user input, Ä -> Ae, ñ -> n etc.
    boost [:title, :author] => +3, [:title] => +1
  end

  # Route /books to the books search and log when searching.
  #
  get '/books' do
    results = books.search params[:query], params[:ids] || 20, params[:offset] || 0
    AppLogger.info results.to_log(params[:query])
    results.to_json
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
picky-generators-3.0.0.pre5 prototypes/server/sinatra/app.rb
picky-generators-3.0.0.pre4 prototypes/server/sinatra/app.rb
picky-generators-3.0.0.pre3 prototypes/server/sinatra/app.rb
picky-generators-3.0.0.pre2 prototypes/server/sinatra/app.rb