Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 145 lines (118 sloc) 4.146 kb
c9cd52d @floere + moved everything into a single server search app
authored
1 # encoding: utf-8
2 #
3 require 'sinatra/base'
4 require 'picky'
a9fb8fd @floere Update to newer Picky version and new data.
authored
5 require 'picky-client'
c9cd52d @floere + moved everything into a single server search app
authored
6 require File.expand_path '../gem', __FILE__
7 require File.expand_path '../logging', __FILE__
8
9 # This app shows how to integrate the Picky server directly
10 # inside a web app. However, if you really need performance
11 # and easy caching, this is not recommended.
12 #
13 class GemSearch < Sinatra::Application
14
15 # We do this so we don't have to type
16 # Picky:: in front of everything.
17 #
18 include Picky
19
20 # Server.
21 #
a9fb8fd @floere Update to newer Picky version and new data.
authored
22
23 # The gem data.
24 #
25 csv = CSV.open('./data/gems.csv', headers: false)
26 .to_a
27 .map { |row| AGem.new(*row) }
c9cd52d @floere + moved everything into a single server search app
authored
28
29 # Define an index.
30 #
31 gems_index = Index.new :gems do
a9fb8fd @floere Update to newer Picky version and new data.
authored
32 key_format :to_i
33
34 source { csv }
c9cd52d @floere + moved everything into a single server search app
authored
35
36 indexing removes_characters: /[^a-zA-Z0-9\s\/\_\-\"\&\|\.]/, # whitelist
37 stopwords: /\b(and|the|of|a|on|at|it|in|for|to)\b/,
38 splits_text_on: /[\s\/\_\-\"\&\|]/,
39 substitutes_characters_with: CharacterSubstituters::WestEuropean.new # Normalizes special user input, Ä -> Ae, ñ -> n etc.
40
41 category :name,
42 similarity: Similarity::DoubleMetaphone.new(2),
43 partial: Partial::Substring.new(from: 1),
44 qualifiers: [:name, :gem]
45
46 category :version,
47 partial: Partial::Substring.new(from: 1),
48 qualifiers: [:version],
49 from: :versions
50
51 category :author,
52 similarity: Similarity::DoubleMetaphone.new(2),
53 partial: Partial::Substring.new(from: 1),
a9fb8fd @floere Update to newer Picky version and new data.
authored
54 qualifiers: [:author, :authors, :written, :writer, :by],
55 :from => :authors
c9cd52d @floere + moved everything into a single server search app
authored
56
57 category :dependencies,
58 similarity: Similarity::DoubleMetaphone.new(2),
59 partial: Partial::Substring.new(from: 1),
60 qualifiers: [:dependency, :dependencies, :depends, :using, :uses, :use, :needs]
61
62 category :summary, partial: Partial::None.new
63 end
64
65 # Define a search over the books index.
66 #
67 gems = Search.new gems_index do
68 searching removes_characters: /[^äöüáéíóúàèßa-zA-Z0-9\s\/\-\_\,\&\.\"\~\*\:]/, # Picky needs control chars *"~: to pass through.
69 stopwords: /\b(and|the|of|a|on|at|it|in|for)\b/,
70 splits_text_on: /[\s\/\-\_\,\&]+/,
a9fb8fd @floere Update to newer Picky version and new data.
authored
71 max_words: 5,
c9cd52d @floere + moved everything into a single server search app
authored
72 substitutes_characters_with: CharacterSubstituters::WestEuropean.new # Normalizes special user input, Ä -> Ae, ñ -> n etc.
73
74 boost [:summary, :name] => +4,
75 [:name] => +1,
76 [:summary] => -2
77 end
78
79
80 # Client.
81 #
82
83 set :static, true
84 set :public, File.dirname(__FILE__)
85 set :views, File.expand_path('../views', __FILE__)
86 set :haml, :format => :html5
87
88 # Root, the search page.
89 #
90 get '/' do
91 @query = params[:q]
92
93 response['Cache-Control'] = 'public, max-age=36000'
94 haml :'/search'
95 end
96
97 # Configure. The configuration info page.
98 #
99 get '/configure' do
100 response['Cache-Control'] = 'public, max-age=36000'
101 haml :'/configure'
102 end
103
104 # Renders the results into the json.
105 #
106 # You get the results from the (local) picky server and then
107 # populate the result hash with rendered models.
108 #
109 get '/search/full' do
110 results = gems.search params[:query], params[:ids] || 20, params[:offset] || 0
111 AppLogger.info results
112 results = results.to_hash
113 results.extend Picky::Convenience
114 results.populate_with AGem do |a_gem|
8a17d13 @floere Update indexes.
authored
115 a_gem.to_html
c9cd52d @floere + moved everything into a single server search app
authored
116 end
117
118 #
119 # Or, to populate with the model instances, use:
120 # results.populate_with Book
121 #
122 # Then to render:
123 # rendered_entries = results.entries.map do |book| (render each book here) end
124 #
125 response['Cache-Control'] = 'public, max-age=36000'
126 ActiveSupport::JSON.encode results
127 end
128
129 # Updates the search count while the user is typing.
130 #
131 get '/search/live' do
eb76fb2 @floere ! books -> gems
authored
132 results = gems.search params[:query], params[:ids] || 20, params[:offset] || 0
c9cd52d @floere + moved everything into a single server search app
authored
133 response['Cache-Control'] = 'public, max-age=36000'
134 results.to_json
135 end
136
137 helpers do
138
139 def js path
140 "<script src='javascripts/#{path}.js' type='text/javascript'></script>"
141 end
142
143 end
144
145 end
Something went wrong with that request. Please try again.