Sha256: b6b15c729f91b2d768bed8c7a0e3800a0534307b57981c009674737d3d93bdba

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'sinatra/base'
require 'picky'

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.
  #
  search = 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.
  #
  get '/books' do
    results = search.search_with_text params[:query], params[:ids] || 20, params[:offset] || 0
    results.to_json
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
picky-generators-3.0.0.pre1 prototypes/server/sinatra/app.rb