-
Notifications
You must be signed in to change notification settings - Fork 1
/
atom.xml
1063 lines (954 loc) · 75.6 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Category: full-text | valve's]]></title>
<link href="http://valve.github.io/blog/categories/full-text/atom.xml" rel="self"/>
<link href="http://valve.github.io/"/>
<updated>2014-02-24T11:33:19+04:00</updated>
<id>http://valve.github.io/</id>
<author>
<name><![CDATA[valve]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Introduction to full text search for Rails developers]]></title>
<link href="http://valve.github.io/blog/2014/02/22/rails-developer-guide-to-full-text-search-with-solr/"/>
<updated>2014-02-22T15:05:00+04:00</updated>
<id>http://valve.github.io/blog/2014/02/22/rails-developer-guide-to-full-text-search-with-solr</id>
<content type="html"><![CDATA[<p>Every developer has heard of full-text search. However, most developers search with SQL and relational databases.</p>
<p>Almost every developer knows deep inside that full-text search is better suited for searching text, but continues to use old <code>LIKE '%?%'</code> queries.</p>
<p>I was one of those developers who never used full-text search,
but I have changed and I invite others to join me and discover
the other side of search with Solr.</p>
<!--more-->
<p></p>
<p>This article assumes you’re comfortable with Ruby, Rails and PostgreSQL. I’ll build a simple <em>people near me</em> application using Solr in small incremental steps and hopefully help readers to
overcome the feeling of uncomfortable uneasiness when thinking about full text search technology.</p>
<p><strong>Disclaimer:</strong> <em>My goal here is to familiarize the reader with full-text search,
not create an ideal rails application structure.
I’ll be using long views and JavaScript inside ERB templates.
The point is to make a small but complete application in a single article
and it is possible to do so only by keeping it really simple.</em></p>
<p>OK, enough talk, let’s build the app!</p>
<p>Let’s call this app <code>Neibo</code>:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>➜ personal ruby -v
</span><span class='line'>ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin13.0.0]
</span><span class='line'>➜ personal rails -v
</span><span class='line'>Rails 4.0.2
</span><span class='line'>➜ personal rails new neibo</p>
</span><span class='line'>
</span><span class='line'><pre><code> create
</span><span class='line'> create README.rdoc
</span><span class='line'> create Rakefile
</span><span class='line'> create config.ru
</span><span class='line'> create .gitignore
</span><span class='line'> create Gemfile
</span><span class='line'> create app
</span><span class='line'> ...
</span><span class='line'></code></pre>
</span><span class='line'>
</span><span class='line'><p></span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Let’s remove the <code>sqlite3</code>, <code>turbolinks</code>, <code>coffee-rails</code>, <code>jbuilder</code> and <code>jquery-rails</code> gems
as we will not need them. We should also add the <code>pg</code> gem to talk to Postgres DB.</p>
<p>My Gemfile is now:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>source ‘<a href="https://rubygems.org">https://rubygems.org</a>’</p>
</span><span class='line'>
</span><span class='line'><p>gem ‘rails’, ‘4.0.2’
</span><span class='line'>gem ‘pg’
</span><span class='line'>gem ‘sass-rails’, ‘~> 4.0.0’
</span><span class='line'>gem ‘uglifier’, ‘>= 1.3.0’</p>
</span><span class='line'>
</span><span class='line'><p></span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Now you need to set the pg connection in <code>config/database.yml</code> file:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain"></p></span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain"><h1>config/database.yml</h1></span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain"><p>common</span><span class="p-Indicator">:</span> <span class="nl">&amp</span><span class="l-Scalar-Plain">;common</span>
</span><span class='line'> <span class="l-Scalar-Plain">adapter</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">postgresql</span>
</span><span class='line'> <span class="l-Scalar-Plain">encoding</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">unicode</span>
</span><span class='line'> <span class="l-Scalar-Plain">pool</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">5</span>
</span><span class='line'> <span class="l-Scalar-Plain">timeout</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">5000</span>
</span><span class='line'> <span class="l-Scalar-Plain">min_messages</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">warning</p></span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain"><p>development</span><span class="p-Indicator">:</span>
</span><span class='line'> <span class="nl">&lt</span><span class="l-Scalar-Plain">;&lt;</span><span class="p-Indicator">:</span> <span class="nv">*common</span>
</span><span class='line'> <span class="l-Scalar-Plain">database</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">neibo</p></span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain"><p>test</span><span class="p-Indicator">:</span>
</span><span class='line'> <span class="nl">&lt</span><span class="l-Scalar-Plain">;&lt;</span><span class="p-Indicator">:</span> <span class="nv">*common</span>
</span><span class='line'> <span class="l-Scalar-Plain">database</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">neibo_test</p></span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain"><p>production</span><span class="p-Indicator">:</span>
</span><span class='line'> <span class="nl">&lt</span><span class="l-Scalar-Plain">;&lt;</span><span class="p-Indicator">:</span> <span class="nv">*common</span>
</span><span class='line'> <span class="l-Scalar-Plain">database</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">neibo_production</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Then let’s create a <code>Person</code> model and generate a migration for it:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/mo</span><span class="n">dels</span><span class="o">/</span><span class="n">person</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>class Person &lt; ActiveRecord::Base</span>
</span><span class='line'><span class="sr">end</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>➜ neibo rails g migration create_people</p>
</span><span class='line'>
</span><span class='line'><pre><code> invoke active_record
</span><span class='line'> create db/migrate/20140222113048_create_people.rb
</span><span class='line'></code></pre>
</span><span class='line'>
</span><span class='line'><p></span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>db/mi</span><span class="n">grate</span><span class="o">/</span><span class="mi">20140222113048</span><span class="n">_create_people</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>class CreatePeople &lt; ActiveRecord::Migration</span>
</span><span class='line'><span class="sr"> def change</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="n">create_table</span> <span class="ss">:people</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">null</span><span class="p">:</span> <span class="kp">false</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">text</span> <span class="ss">:about</span><span class="p">,</span> <span class="ss">null</span><span class="p">:</span> <span class="kp">false</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:likes</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">string</span> <span class="ss">:dislikes</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">float</span> <span class="ss">:lat</span><span class="p">,</span> <span class="ss">null</span><span class="p">:</span> <span class="kp">true</span>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">float</span> <span class="ss">:lon</span><span class="p">,</span> <span class="ss">null</span><span class="p">:</span> <span class="kp">true</span>
</span><span class='line'>
</span><span class='line'> <span class="n">t</span><span class="o">.</span><span class="n">timestamps</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>For every person we store a <code>name</code>,
an <code>about</code> – this is where a person can tell the world about himself,
<code>likes</code> – things person likes and <code>dislikes</code>.
We also want to store a person’s location, so that other people can locate him within certain radius.</p>
<p>We store a location using two <a href="http://www.postgresql.org/docs/9.3/static/datatype-numeric.html#DATATYPE-FLOAT">floating point</a> numbers, <code>lat</code> – for latitude, and <code>lon</code> – for longitude.
It’s possible to use a
specialized <a href="http://www.postgresql.org/docs/current/static/datatype-geometric.html#AEN6547">Point</a> data type, but I want to keep it simple here.</p>
<p>I make <code>lat</code> & <code>lon</code> attributes nullable in case a user
denies the browser geolocation permission.</p>
<p>Let’s create the databases and run the migration.</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>➜ neibo rake db:create
</span><span class='line'>➜ neibo rake db:migrate
</span><span class='line'>== CreatePeople: migrating ===================================================
</span><span class='line'>— create_table(:people)
</span><span class='line'> –> 0.0080s
</span><span class='line'>== CreatePeople: migrated (0.0080s) ==========================================</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>We now need to create a controller, a route and a view:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">people_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>class PeopleController &lt; ApplicationController</span>
</span><span class='line'><span class="sr"> def index</span>
</span><span class='line'><span class="sr"> end</span>
</span><span class='line'><span class="sr">end</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>config/</span><span class="n">routes</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>Neibo::Application.routes.draw do</span>
</span><span class='line'><span class="sr"> root to: &lsquo;people#index&rsquo;</span>
</span><span class='line'><span class="sr"> resources :people</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p><code>app/views/people/index.html.erb</code>
<div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='erb'><span class='line'><span class="x">&lt;% if flash[:alert].present? </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x"> <h1 style="color:red">&lt;%= flash[:alert] </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% end </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% if current_user </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x"> <h2>Hello, &lt;%= current_user.name </span><span class="err">%></span><span class="x"></h2></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><p> <h3>Search people near you: </h3></span>
</span><span class='line'><span class="x"> &lt;%= form_tag people_path, method: :get do </span><span class="err">%></span><span class="x"></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><pre><code>&lt;div&gt;</span>
</span><span class='line'><span class="x"> &lt;%= text_field_tag :search, params[:search], </span>
</span><span class='line'><span class="x"> placeholder: 'Search nearby people', type: :search, style: 'width: 400px' %&gt;</span>
</span><span class='line'><span class="x"> &lt;label&gt;Search within mile radius:</span>
</span><span class='line'><span class="x"> &lt;%= select_tag :radius, options_for_select([10, 50, 100]) %&gt;</span>
</span><span class='line'><span class="x"> &lt;/label&gt;</span>
</span><span class='line'><span class="x">&lt;/div&gt;</span>
</span><span class='line'><span class="x">&lt;div&gt;&lt;%= submit_tag 'Search' %&gt;&lt;/div&gt;</span>
</span><span class='line'><span class="x"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="x"><p> &lt;% end </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% else </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x"> <h2>Hello, guest</h2></span>
</span><span class='line'><span class="x"> <h3></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><pre><code>Fill your profile so that people could find you. </span>
</span><span class='line'><span class="x">Allow browser to access your location if you want to be found by people near you.</span>
</span><span class='line'><span class="x"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="x"><p> </h3></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><p> &lt;%= form_for @new_person do |f| </span><span class="err">%></span><span class="x"></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><pre><code>&lt;div&gt; &lt;%= f.text_field :name, placeholder: 'Enter your name' %&gt; &lt;/div&gt;</span>
</span><span class='line'><span class="x">&lt;div&gt; &lt;%= f.text_area :about, placeholder: 'Tell about yourself' %&gt; &lt;/div&gt;</span>
</span><span class='line'><span class="x">&lt;div&gt; &lt;%= f.text_field :likes, placeholder: 'Stuff you like' %&gt; &lt;/div&gt;</span>
</span><span class='line'><span class="x">&lt;div&gt; &lt;%= f.text_field :dislikes, placeholder: 'Stuff you don\'t ' %&gt; &lt;/div&gt;</span>
</span><span class='line'><span class="x">&lt;div&gt; &lt;%= f.submit 'Save' %&gt; &lt;/div&gt;</span>
</span><span class='line'><span class="x"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="x"><p> &lt;% end </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% end </span><span class="err">%></span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>This UI has two parts: if a user has already filled his details,
he can use the search form and search for people nearby.
If this is a new user, he fills his details, optionally allows a browser to get his location and
saves his profile in the database.</p>
<p>We now need to modify the <code>app/assets/javascripts/application.js</code>
and remove the files we’re not using.
I remove them all and leave the <code>application.js</code> empty.</p>
<p>The view code checks the <code>current_user</code> method to see if the current user profile has been filled.
Let’s create this method:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">application_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>class ApplicationController &lt; ActionController::Base</span>
</span><span class='line'><span class="sr"> protect_from_forgery with: :exception</span>
</span><span class='line'><span class="sr"> helper_method :current_user</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="kp">private</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p> def current_user</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="vi">@current_user</span> <span class="o">||=</span> <span class="no">Person</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">session</span><span class="o">[</span><span class="ss">:current_user_id</span><span class="o">]</span><span class="p">)</span> <span class="k">if</span> <span class="n">session</span><span class="o">[</span><span class="ss">:current_user_id</span><span class="o">]</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>I’ll be storing current user’s id in session and get the user object from the database.</p>
<p>Let’s concentrate on the <code>new user</code> scenario.
In order for the application to learn the user’s location, we need to grab it from the browser and save.</p>
<p>Adding the code to the view:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'> <span class="nt"><h2></span>Hello, guest<span class="nt"></h2></span>
</span><span class='line'> <span class="nt"><h3></p></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><pre><code></span>Fill your profile so that people could find you.
</span><span class='line'>Allow browser to access your location if you want to be found by people near you.
</span><span class='line'><span class="nt"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><p></span> <span class="nt"></h3></p></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><p></span> <span class="ni">&lt;</span>%= form_for @new_person do |f| %><span class="nt"></p></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><pre><code></span><span class="ni">&lt;</span>div<span class="ni">&gt;</span> <span class="ni">&lt;</span>%= f.text_field :name, placeholder: 'Enter your name' %<span class="ni">&gt;</span> <span class="ni">&lt;</span>/div<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>div<span class="ni">&gt;</span> <span class="ni">&lt;</span>%= f.text_area :about, placeholder: 'Tell about yourself' %<span class="ni">&gt;</span> <span class="ni">&lt;</span>/div<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>div<span class="ni">&gt;</span> <span class="ni">&lt;</span>%= f.text_field :likes, placeholder: 'Stuff you like' %<span class="ni">&gt;</span> <span class="ni">&lt;</span>/div<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>div<span class="ni">&gt;</span> <span class="ni">&lt;</span>%= f.text_field :dislikes, placeholder: 'Stuff you don\'t ' %<span class="ni">&gt;</span> <span class="ni">&lt;</span>/div<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>%= f.hidden_field :lat %<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>%= f.hidden_field :lon %<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>div<span class="ni">&gt;</span> <span class="ni">&lt;</span>%= f.submit 'Save' %<span class="ni">&gt;</span> <span class="ni">&lt;</span>/div<span class="ni">&gt;</span>
</span><span class='line'><span class="nt"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><p></span> <span class="ni">&lt;</span>% end %>
</span><span class='line'> <span class="nt"><script></span><span class="o"><</span><span class="err">/p></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nx">pre</span><span class="o">><</span><span class="nx">code</span><span class="o">></span><span class="nx">navigator</span><span class="p">.</span><span class="nx">geolocation</span><span class="p">.</span><span class="nx">getCurrentPosition</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">position</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">'#person_lat'</span><span class="p">).</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">position</span><span class="p">.</span><span class="nx">coords</span><span class="p">.</span><span class="nx">latitude</span><span class="p">;</span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">'#person_lon'</span><span class="p">).</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">position</span><span class="p">.</span><span class="nx">coords</span><span class="p">.</span><span class="nx">longitude</span><span class="p">;</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'><span class="o"><</span><span class="err">/code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nx">p</span><span class="o">></span> <span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>The lines we’re interested in are:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"></p></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><pre><code></span><span class="ni">&lt;</span>%= f.hidden_field :lat %<span class="ni">&gt;</span>
</span><span class='line'><span class="ni">&lt;</span>%= f.hidden_field :lon %<span class="ni">&gt;</span>
</span><span class='line'><span class="nt"></code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><p></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>And the JavaScript:
<div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'> <span class="o"><</span><span class="nx">script</span><span class="o">><</span><span class="err">/p></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nx">pre</span><span class="o">><</span><span class="nx">code</span><span class="o">></span><span class="nx">navigator</span><span class="p">.</span><span class="nx">geolocation</span><span class="p">.</span><span class="nx">getCurrentPosition</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">position</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">'#person_lat'</span><span class="p">).</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">position</span><span class="p">.</span><span class="nx">coords</span><span class="p">.</span><span class="nx">latitude</span><span class="p">;</span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelector</span><span class="p">(</span><span class="s1">'#person_lon'</span><span class="p">).</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">position</span><span class="p">.</span><span class="nx">coords</span><span class="p">.</span><span class="nx">longitude</span><span class="p">;</span>
</span><span class='line'><span class="p">});</span>
</span><span class='line'><span class="o"><</span><span class="err">/code></pre></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nx">p</span><span class="o">></span> <span class="o"><</span><span class="err">/script></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>When a view loads, JavaScripts asks a user for permission to get his location.
If the user agrees, the callback is invoked and the location is saved in the hidden fields
so that the form can
submit them back to the server.</p>
<p>Now the controller part:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">people_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>def index</span>
</span><span class='line'><span class="sr"> @new_person = Person.new</span>
</span><span class='line'><span class="sr">end</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span><span class="k">def</span> <span class="nf">create</span>
</span><span class='line'> <span class="vi">@new_person</span> <span class="o">=</span> <span class="no">Person</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">person_params</span><span class="p">)</span>
</span><span class='line'> <span class="k">if</span> <span class="vi">@new_person</span><span class="o">.</span><span class="n">save</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><pre><code>session[:current_user_id] = @new_person.id</span>
</span><span class='line'><span class="sr">redirect_to people_path</span>
</span><span class='line'><span class="sr"></</span><span class="n">code</span><span class="o">><</span><span class="sr">/pre></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p> else</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="n">flash</span><span class="o">.</span><span class="n">now</span><span class="o">.</span><span class="n">alert</span> <span class="o">=</span> <span class="s1">'Please fill your profile'</span>
</span><span class='line'><span class="n">render</span> <span class="ss">:index</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">end</span>
</span><span class='line'><span class="k">end</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>private</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span><span class="k">def</span> <span class="nf">person_params</span>
</span><span class='line'> <span class="n">params</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="ss">:person</span><span class="p">)</span><span class="o">.</span><span class="n">permit</span><span class="p">(</span><span class="ss">:name</span><span class="p">,</span> <span class="ss">:about</span><span class="p">,</span> <span class="ss">:likes</span><span class="p">,</span> <span class="ss">:dislikes</span><span class="p">,</span> <span class="ss">:lat</span><span class="p">,</span> <span class="ss">:lon</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Here we have boilerplate ruby code, we’re using strong parameters to only allow a known
set of attributes.
We then try to create a user and save the new user ID in the session.
This way <code>current_user</code> helper method will retrieve the current user from the
database.
If the validation fails, we just display the message about it and render the view again.</p>
<p>Let’s add those validations:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/mo</span><span class="n">dels</span><span class="o">/</span><span class="n">person</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>class Person &lt; ActiveRecord::Base</span>
</span><span class='line'><span class="sr"> validates :name, :about, :likes, :dislikes, presence: true</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Now when we go back to the browser and reload the page we can enter the profile data,
allow the browser to get our geolocation and click save.</p>
<p>At this point we introduced ourselves to
the system and <code>current_user.id</code> is stored in the encrypted cookie.</p>
<p>Next part is where the fun starts: we need to be able to search for other users nearby.
We should allow limiting the search radius, specify the search term and see the results.</p>
<p>We must remove people who have our search term in their <code>dislikes</code> attribute.
For example if a person dislikes Chinese couisine, and we’re searching for people
who like it,… you get the idea.</p>
<p>Let’s take a little detour and speak about Solr and the gems that enable it in Rails.
We’ll be using <a href="https://github.com/sunspot/sunspot">sunspot</a> – an excellent gem that
adds a nice DSL (really, it’s nice) on top of <a href="https://github.com/rsolr/rsolr">rsolr</a>.</p>
<p>At this point you might be asking: <em>“Wait! What’s RSolr? I’m now totally confused between Solr, RSolr and Sunspot and how they relate to each other”</em>.
I totally understand your confusion. Let’s break this mess into pieces:</p>
<ol>
<li>Solr – a Java server that runs as a separate service and communicates
via XML over HTTP API. It is generally considered a robust and full-featured
— yet hard to learn full-text search solution.
The only way you can communicate directly with Solr from a Rails application
is to send rather cryptic XML requests.</li>
<li>Nobody wants to mess with raw XML over HTTP, so here enters RSolr – a wrapper around the
Solr HTTP API that allows interacting with Solr from Ruby.</li>
<li>However RSolr is still rather low-level and does not provide any DSL or convenience methods
to define which Rails models should be searchable and how the indexes will be updated.
The need for a new library was apparent, so the Sunspot was born. A really nice DSL that
integrates directly into ActiveRecord models and allows specifying which attributes we need
to index, as well as how to transform and query the data.</li>
</ol>
<p>Now you’re saying: “<em>I still don’t understand, if Solr is a Java service it means
I need to install and configure it on my system?
That’s a horrible perspective, get me out of this!</em>”.
Absolutely not. The Sunspot gem is bundled with a development version of Solr and has a nice set of rake tasks to manage it. You can start, stop, reindex the data, all using rake tasks. There is no need to install Solr manually, all you need is to add two gems:</p>
<p><code>sunspot_solr</code> and <code>sunspot_rails</code>.</p>
<p><code>sunspot_solr</code> is the pre-packaged development version of Solr and <code>sunspot_rails</code>
is the Sunspot gem itself. So you need to make sure you place the <code>sunspot_solr</code> into <code>:development</code> group in your Gemfile.</p>
<p>Now you can start bundled Solr with <code>rake sunspot:solr:start</code>, stop it with <code>rake sunspot:solr:stop</code> and
reindex all data with <code>rake sunspot:reindex</code>.</p>
<p>OK, now that confusion is hopefully out of the way, let’s continue with our person search
scenario.</p>
<p>Let us define the searchable attributes on our <code>Person</code> model:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Person</span> <span class="o">&</span><span class="n">lt</span><span class="p">;</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'> <span class="n">validates</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">:about</span><span class="p">,</span> <span class="ss">:likes</span><span class="p">,</span> <span class="ss">:dislikes</span><span class="p">,</span> <span class="ss">presence</span><span class="p">:</span> <span class="kp">true</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p> searchable do</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="n">text</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">boost</span><span class="p">:</span> <span class="mi">5</span><span class="o">.</span><span class="mi">0</span>
</span><span class='line'><span class="n">text</span> <span class="ss">:about</span><span class="p">,</span> <span class="ss">:likes</span>
</span><span class='line'><span class="n">latlon</span><span class="p">(</span><span class="ss">:location</span><span class="p">)</span> <span class="p">{</span> <span class="ss">Sunspot</span><span class="p">:</span><span class="ss">:Util</span><span class="o">::</span><span class="no">Coordinates</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">lat</span><span class="p">,</span> <span class="n">lon</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>Let’s break it down piece by piece:</p>
<ol>
<li><code>searchable</code> block is a place where you define the full-text indexing behavior.
Inside this block you can specify various rules describing which attributes should
be indexed, their pre-index transformations, facets, filters and so on.</li>
<li><code>text :name</code> – A person should be searchable by his name.</li>
<li><code>boost: 5.0</code> – boost option tells Solr to prioritize the results found by this particular attribute.
If you’re searching for <code>John Doe</code>,
all the people with this name will come first,
and only after those who dislike John Does.</li>
<li><code>text :about, :likes</code> – <code>Person</code> should be searchable by these attributes.</li>
<li><code>latlon(:location) { Sunspot::Util::Coordinates.new(lat, lon) }</code> – create a geo-spatial
index on person’s location using <code>lat</code> and <code>lon</code> attributes.
This will allow searching for people within a certain mile radius.</li>
</ol>
<p>Great, wasn’t that simple? We’ve defined a set of searchable attributes on a <code>Person</code> model.
Now we’re ready to actually search for people.</p>
<p>Let us add a <code>_person</code> partial where search result item will be displayed:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='erb'><span class='line'><span class="x">&lt;%# app/views/people/_person.html.erb </span><span class="err">%></span><span class="x"></p></span>
</span><span class='line'>
</span><span class='line'><span class="x"><div class="person"></span>
</span><span class='line'><span class="x"> <h4></span><span class="cp"><%=</span> <span class="n">person</span><span class="o">.</span><span class="n">name</span> <span class="cp">%></span><span class="x"> </h4></span>
</span><span class='line'><span class="x"> <h5>About:</h5></span>
</span><span class='line'><span class="x"> <italic></span><span class="cp"><%=</span> <span class="n">person</span><span class="o">.</span><span class="n">about</span> <span class="cp">%></span><span class="x"></italic></span>
</span><span class='line'><span class="x"> <h5>Likes:</h5></span>
</span><span class='line'><span class="x"> <italic></span><span class="cp"><%=</span> <span class="n">person</span><span class="o">.</span><span class="n">likes</span> <span class="cp">%></span><span class="x"></italic></span>
</span><span class='line'><span class="x"> <h5>Dislikes:</h5></span>
</span><span class='line'><span class="x"> <italic></span><span class="cp"><%=</span> <span class="n">person</span><span class="o">.</span><span class="n">dislikes</span> <span class="cp">%></span><span class="x"></italic></span>
</span><span class='line'><span class="x"></div></span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="x"><hr/></span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="x"><p></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>We also need to add the iteration to the <code>index</code> view:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='erb'><span class='line'><span class="x">&lt;%# app/views/people/index.html.erb </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% @people.each do |person| </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x"> &lt;%= render partial: &lsquo;person&rsquo;, locals: {person: person} </span><span class="err">%></span><span class="x"></span>
</span><span class='line'><span class="x">&lt;% end </span><span class="err">%></span><span class="x"></span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>So the view is ready, let’s modify the controller code:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">people_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>def index</span>
</span><span class='line'><span class="sr"> if current_user</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="k">if</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">].</span><span class="n">present?</span> <span class="o">||</span> <span class="n">params</span><span class="o">[</span><span class="ss">:radius</span><span class="o">].</span><span class="n">present?</span>
</span><span class='line'> <span class="n">search</span> <span class="o">=</span> <span class="no">Person</span><span class="o">.</span><span class="n">search</span> <span class="k">do</span>
</span><span class='line'> <span class="n">fulltext</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">]</span>
</span><span class='line'> <span class="k">if</span> <span class="n">current_user</span><span class="o">.</span><span class="n">has_location?</span>
</span><span class='line'> <span class="n">with</span><span class="p">(</span><span class="ss">:location</span><span class="p">)</span><span class="o">.</span><span class="n">in_radius</span><span class="p">(</span><span class="n">current_user</span><span class="o">.</span><span class="n">lat</span><span class="p">,</span> <span class="n">current_user</span><span class="o">.</span><span class="n">lon</span><span class="p">,</span> <span class="n">params</span><span class="o">[</span><span class="ss">:radius</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="vi">@people</span> <span class="o">=</span> <span class="n">search</span><span class="o">.</span><span class="n">results</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'> <span class="vi">@people</span> <span class="o">=</span> <span class="o">[]</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">else</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><pre><code>@new_person = Person.new</span>
</span><span class='line'><span class="sr"></</span><span class="n">code</span><span class="o">><</span><span class="sr">/pre></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p> end</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>On line <code>3</code> we check if current user is saved, on line <code>4</code> we verify we have something to
search by, either a search term or a radius. Then on lines <code>5 - 10</code> is where the actual
full-text search happens. We use a <code>Model.search</code> method and pass it a block.
Inside this block we need to specify the logic of the search.
In our case we call the <code>fulltext</code> method and pass it our search term.</p>
<p>Let me be clear, we have two phases: <strong>indexing</strong> and <strong>searching</strong>. Indexing is defined
inside a model in a <code>searchable</code> block. You use <code>text</code> method to specify which attributes
should be full-text searchable.</p>
<p>Search by calling <code>Model.search</code> method and passing it a block too. But this time
we call <code>fulltext</code> method to actually do full-text search on indexed attributes.</p>
<p>OK, we now understand how to do full-text search on text attributes, we’re already doing it on
<code>name</code>, <code>about</code> and <code>likes</code> attributes. What we also need is a way to restrict the results
to a certain radius on a map. This is what lines <code>7 - 9</code> are for.</p>
<p>In our application it’s possible for a user to deny geolocation permissions and his
profile to be saved without coordinates. So, we need a convenience method to see
if the current user has a location:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/mo</span><span class="n">dels</span><span class="o">/</span><span class="n">person</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>def has_location?</span>
</span><span class='line'><span class="sr"> lat &amp;&amp; lon</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>This method is useful in <code>Person.search</code> block where we specify the search radius:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">people_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>if current_user.has_location?</span>
</span><span class='line'><span class="sr"> with(:location).in_radius(current_user.lat, current_user.lon, params[:radius])</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>We’re using the current user’s <code>lat</code> & <code>lon</code> attributes and the radius from params to perform the
filtering. You should remember to convert miles to kilometers, because Sunspot operates on
kilometers.</p>
<p>OK, first version of the people search is ready to try, let’s run it.</p>
<p>Works fine, but when I search for someone within 10 mile radius, I find myself too.
There should be a way to search for <em>other</em> people, excluding myself. Let’s fix it.</p>
<p>Sunspot allows using attributes as filters. For this we should call methods like <code>integer</code>,
<code>string</code>, <code>datetime</code> etc. In this case we need to search for all people except those
with <code>:id</code> equal to the <code>:id</code> of current user. We also need to filter out the people with
<code>dislikes</code> equal to the search term:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/mo</span><span class="n">dels</span><span class="o">/</span><span class="n">person</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>searchable do</span>
</span><span class='line'><span class="sr"> text :name, boost: 5.0</span>
</span><span class='line'><span class="sr"> text :about, :likes</span>
</span><span class='line'><span class="sr"> integer (:id)</span>
</span><span class='line'><span class="sr"> string(:dislikes)</span>
</span><span class='line'><span class="sr"> latlon(:location) { Sunspot::Util::Coordinates.new(lat, lon) }</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>
<p>On line <code>5</code> we’re creating an indexed filter on <code>:id</code> column, and on the next line a filter on
<code>:dislikes</code> column.</p>
<p>Now the filtering itself:</p>
<p><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><h1>app/</span><span class="n">controllers</span><span class="o">/</span><span class="n">people_controller</span><span class="o">.</span><span class="n">rb</span><span class="o"><</span><span class="sr">/h1></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p>def index</span>
</span><span class='line'><span class="sr"> if current_user</</span><span class="nb">p</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="n">pre</span><span class="o">><</span><span class="n">code</span><span class="o">></span><span class="k">if</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">].</span><span class="n">present?</span> <span class="o">||</span> <span class="n">params</span><span class="o">[</span><span class="ss">:radius</span><span class="o">].</span><span class="n">present?</span>
</span><span class='line'> <span class="n">search</span> <span class="o">=</span> <span class="no">Person</span><span class="o">.</span><span class="n">search</span> <span class="k">do</span>
</span><span class='line'> <span class="n">without</span><span class="p">(</span><span class="ss">:id</span><span class="p">,</span> <span class="n">current_user</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
</span><span class='line'> <span class="n">without</span><span class="p">(</span><span class="ss">:dislikes</span><span class="p">,</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">]</span><span class="p">)</span> <span class="k">if</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">].</span><span class="n">present?</span>
</span><span class='line'> <span class="n">fulltext</span> <span class="n">params</span><span class="o">[</span><span class="ss">:search</span><span class="o">]</span>
</span><span class='line'> <span class="k">if</span> <span class="n">current_user</span><span class="o">.</span><span class="n">has_location?</span>
</span><span class='line'> <span class="n">with</span><span class="p">(</span><span class="ss">:location</span><span class="p">)</span><span class="o">.</span><span class="n">in_radius</span><span class="p">(</span><span class="n">current_user</span><span class="o">.</span><span class="n">lat</span><span class="p">,</span> <span class="n">current_user</span><span class="o">.</span><span class="n">lon</span><span class="p">,</span> <span class="n">params</span><span class="o">[</span><span class="ss">:radius</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="vi">@people</span> <span class="o">=</span> <span class="n">search</span><span class="o">.</span><span class="n">results</span>
</span><span class='line'><span class="k">else</span>
</span><span class='line'> <span class="vi">@people</span> <span class="o">=</span> <span class="o">[]</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'><span class="o"><</span><span class="sr">/code></</span><span class="n">pre</span><span class="o">></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nb">p</span><span class="o">></span> <span class="k">else</span><span class="o"><</span><span class="sr">/p></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><pre><code>@new_person = Person.new</span>
</span><span class='line'><span class="sr"></</span><span class="n">code</span><span class="o">><</span><span class="sr">/pre></span>
</span><span class='line'>
</span><span class='line'><span class="sr"><p> end</span>
</span><span class='line'><span class="sr">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div></p>