tire 0.5.4 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +3 -0
- data/README.markdown +49 -29
- data/examples/rails-application-template.rb +15 -15
- data/examples/tire-dsl.rb +24 -24
- data/lib/tire.rb +2 -1
- data/lib/tire/alias.rb +3 -3
- data/lib/tire/dsl.rb +17 -19
- data/lib/tire/index.rb +67 -10
- data/lib/tire/model/callbacks.rb +1 -1
- data/lib/tire/model/indexing.rb +2 -2
- data/lib/tire/model/naming.rb +1 -1
- data/lib/tire/model/percolate.rb +3 -3
- data/lib/tire/model/persistence.rb +1 -1
- data/lib/tire/model/persistence/attributes.rb +2 -2
- data/lib/tire/model/persistence/storage.rb +2 -2
- data/lib/tire/model/search.rb +8 -8
- data/lib/tire/multi_search.rb +4 -4
- data/lib/tire/rubyext/ruby_1_8.rb +1 -1
- data/lib/tire/search.rb +1 -1
- data/lib/tire/search/scan.rb +1 -1
- data/lib/tire/tasks.rb +1 -1
- data/lib/tire/version.rb +7 -11
- data/test/integration/active_record_searchable_test.rb +42 -0
- data/test/integration/dis_max_queries_test.rb +1 -1
- data/test/integration/dsl_search_test.rb +9 -0
- data/test/integration/index_mapping_test.rb +82 -7
- data/test/integration/persistent_model_test.rb +17 -0
- data/test/integration/sort_test.rb +16 -0
- data/test/models/active_record_models.rb +9 -0
- data/test/models/persistent_article.rb +1 -1
- data/test/models/persistent_article_in_index.rb +1 -1
- data/test/models/persistent_article_in_namespace.rb +1 -1
- data/test/models/persistent_article_with_percolation.rb +5 -0
- data/test/models/persistent_articles_with_custom_index_name.rb +1 -1
- data/test/test_helper.rb +1 -1
- data/test/unit/index_alias_test.rb +1 -1
- data/test/unit/index_test.rb +79 -1
- data/test/unit/model_persistence_test.rb +11 -1
- data/test/unit/model_search_test.rb +2 -2
- data/test/unit/tire_test.rb +11 -7
- data/tire.gemspec +4 -3
- metadata +61 -51
data/.travis.yml
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
language: ruby
|
6
6
|
|
7
7
|
rvm:
|
8
|
+
- 2.0.0
|
8
9
|
- 1.9.3
|
9
10
|
- 1.8.7
|
10
11
|
- ree
|
@@ -27,6 +28,8 @@ matrix:
|
|
27
28
|
env: TEST_COMMAND="rake test:integration"
|
28
29
|
- rvm: ree
|
29
30
|
env: TEST_COMMAND="rake test:integration"
|
31
|
+
allow_failures:
|
32
|
+
- rvm: ree
|
30
33
|
|
31
34
|
notifications:
|
32
35
|
disable: true
|
data/README.markdown
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Tire
|
2
2
|
=========
|
3
3
|
|
4
|
-
_Tire_ is a Ruby (1.8 or 1.9) client for the [
|
4
|
+
_Tire_ is a Ruby (1.8 or 1.9) client for the [Elasticsearch](http://www.elasticsearch.org/)
|
5
5
|
search engine/database.
|
6
6
|
|
7
|
-
|
7
|
+
_Elasticsearch_ is a scalable, distributed, cloud-ready, highly-available,
|
8
8
|
full-text search engine and database with
|
9
|
-
[
|
9
|
+
[powerful aggregation features](http://www.elasticsearch.org/guide/reference/api/search/facets/),
|
10
10
|
communicating by JSON over RESTful HTTP, based on [Lucene](http://lucene.apache.org/), written in Java.
|
11
11
|
|
12
12
|
This Readme provides a brief overview of _Tire's_ features. The more detailed documentation is at <http://karmi.github.com/tire/>.
|
@@ -17,11 +17,11 @@ and [issues](https://github.com/karmi/tire/issues).
|
|
17
17
|
Installation
|
18
18
|
------------
|
19
19
|
|
20
|
-
OK. First, you need a running
|
20
|
+
OK. First, you need a running _Elasticsearch_ server. Thankfully, it's easy. Let's define easy:
|
21
21
|
|
22
|
-
$ curl -k -L -o elasticsearch-0.
|
23
|
-
$ tar -zxvf elasticsearch-0.
|
24
|
-
$ ./elasticsearch-0.
|
22
|
+
$ curl -k -L -o elasticsearch-0.20.2.tar.gz http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.20.2.tar.gz
|
23
|
+
$ tar -zxvf elasticsearch-0.20.2.tar.gz
|
24
|
+
$ ./elasticsearch-0.20.2/bin/elasticsearch -f
|
25
25
|
|
26
26
|
See, easy. On a Mac, you can also use _Homebrew_:
|
27
27
|
|
@@ -41,11 +41,11 @@ Of course, you can install it from the source as well:
|
|
41
41
|
Usage
|
42
42
|
-----
|
43
43
|
|
44
|
-
_Tire_ exposes easy-to-use domain specific language for fluent communication with
|
44
|
+
_Tire_ exposes easy-to-use domain specific language for fluent communication with _Elasticsearch_.
|
45
45
|
|
46
46
|
It easily blends with your _ActiveModel_/_ActiveRecord_ classes for convenient usage in _Rails_ applications.
|
47
47
|
|
48
|
-
To test-drive the core
|
48
|
+
To test-drive the core _Elasticsearch_ functionality, let's require the gem:
|
49
49
|
|
50
50
|
```ruby
|
51
51
|
require 'rubygems'
|
@@ -102,7 +102,7 @@ for a specific document type:
|
|
102
102
|
```
|
103
103
|
|
104
104
|
Of course, we may have large amounts of data, and it may be impossible or impractical to add them to the index
|
105
|
-
one by one. We can use
|
105
|
+
one by one. We can use _Elasticsearch's_
|
106
106
|
[bulk storage](http://www.elasticsearch.org/guide/reference/api/bulk.html).
|
107
107
|
Notice, that collection items must have an `id` property or method,
|
108
108
|
and should have a `type` property, if you've set any specific mapping for the index.
|
@@ -344,7 +344,7 @@ When you set the log level to _debug_:
|
|
344
344
|
the JSON responses are logged as well. This is not a great idea for production environment,
|
345
345
|
but it's priceless when you want to paste a complicated transaction to the mailing list or IRC channel.
|
346
346
|
|
347
|
-
The _Tire_ DSL tries hard to provide a strong Ruby-like API for the main
|
347
|
+
The _Tire_ DSL tries hard to provide a strong Ruby-like API for the main _Elasticsearch_ features.
|
348
348
|
|
349
349
|
By default, _Tire_ wraps the results collection in a enumerable `Results::Collection` class,
|
350
350
|
and result items in a `Results::Item` class, which looks like a child of `Hash` and `Openstruct`,
|
@@ -357,7 +357,7 @@ If that seems like a great idea to you, there's a big chance you already have su
|
|
357
357
|
|
358
358
|
One would bet it's an `ActiveRecord` or `ActiveModel` class, containing model of your Rails application.
|
359
359
|
|
360
|
-
Fortunately, _Tire_ makes blending
|
360
|
+
Fortunately, _Tire_ makes blending _Elasticsearch_ features into your models trivially possible.
|
361
361
|
|
362
362
|
|
363
363
|
ActiveModel Integration
|
@@ -365,7 +365,7 @@ ActiveModel Integration
|
|
365
365
|
|
366
366
|
If you're the type with no time for lengthy introductions, you can generate a fully working
|
367
367
|
example Rails application, with an `ActiveRecord` model and a search form, to play with
|
368
|
-
(it even downloads
|
368
|
+
(it even downloads _Elasticsearch_ itself, generates the application skeleton and leaves you with
|
369
369
|
a _Git_ repository to explore the steps and the code):
|
370
370
|
|
371
371
|
$ rails new searchapp -m https://raw.github.com/karmi/tire/master/examples/rails-application-template.rb
|
@@ -384,7 +384,7 @@ To make it searchable with _Tire_, just `include` it:
|
|
384
384
|
When you now save a record:
|
385
385
|
|
386
386
|
```ruby
|
387
|
-
Article.create :title => "I Love
|
387
|
+
Article.create :title => "I Love Elasticsearch",
|
388
388
|
:content => "...",
|
389
389
|
:author => "Captain Nemo",
|
390
390
|
:published_on => Time.now
|
@@ -529,7 +529,7 @@ The easiest way is to customize the `to_json` serialization support of your mode
|
|
529
529
|
class Article < ActiveRecord::Base
|
530
530
|
# ...
|
531
531
|
|
532
|
-
include_root_in_json = false
|
532
|
+
self.include_root_in_json = false
|
533
533
|
def to_indexed_json
|
534
534
|
to_json :except => ['updated_at'], :methods => ['length']
|
535
535
|
end
|
@@ -574,9 +574,12 @@ control on how the documents are added to or removed from the index:
|
|
574
574
|
end
|
575
575
|
```
|
576
576
|
|
577
|
+
When you're integrating _Tire_ with ActiveRecord models, you should use the `after_commit`
|
578
|
+
and `after_rollback` hooks to keep the index in sync with your database.
|
579
|
+
|
577
580
|
The results returned by `Article.search` are wrapped in the aforementioned `Item` class, by default.
|
578
|
-
This way, we have a fast and flexible access to the properties returned from
|
579
|
-
`_source` or `fields` JSON properties). This way, we can index whatever JSON we like in
|
581
|
+
This way, we have a fast and flexible access to the properties returned from _Elasticsearch_ (via the
|
582
|
+
`_source` or `fields` JSON properties). This way, we can index whatever JSON we like in _Elasticsearch_,
|
580
583
|
and retrieve it, simply, via the dot notation:
|
581
584
|
|
582
585
|
```ruby
|
@@ -588,18 +591,18 @@ and retrieve it, simply, via the dot notation:
|
|
588
591
|
```
|
589
592
|
|
590
593
|
The `Item` instances masquerade themselves as instances of your model within a _Rails_ application
|
591
|
-
(based on the `_type` property retrieved from
|
594
|
+
(based on the `_type` property retrieved from _Elasticsearch_), so you can use them carefree;
|
592
595
|
all the `url_for` or `dom_id` helpers work as expected.
|
593
596
|
|
594
|
-
If you need to access the “real” model (eg. to access its
|
595
|
-
stored in
|
597
|
+
If you need to access the “real” model (eg. to access its associations or methods not
|
598
|
+
stored in _Elasticsearch_), just load it from the database:
|
596
599
|
|
597
600
|
```ruby
|
598
601
|
puts article.load(:include => 'comments').comments.size
|
599
602
|
```
|
600
603
|
|
601
604
|
You can see that _Tire_ stays as far from the database as possible. That's because it believes
|
602
|
-
you have most of the data you want to display stored in
|
605
|
+
you have most of the data you want to display stored in _Elasticsearch_. When you need
|
603
606
|
to eagerly load the records from the database itself, for whatever reason,
|
604
607
|
you can do it with the `:load` option when searching:
|
605
608
|
|
@@ -636,14 +639,14 @@ so you can pass all the usual parameters to the `search` method in the controlle
|
|
636
639
|
@articles = Article.search params[:q], :page => (params[:page] || 1)
|
637
640
|
```
|
638
641
|
|
639
|
-
OK. Chances are, you have lots of records stored in your database. How will you get them to
|
642
|
+
OK. Chances are, you have lots of records stored in your database. How will you get them to _Elasticsearch_? Easy:
|
640
643
|
|
641
644
|
```ruby
|
642
645
|
Article.index.import Article.all
|
643
646
|
```
|
644
647
|
|
645
648
|
This way, however, all your records are loaded into memory, serialized into JSON,
|
646
|
-
and sent down the wire to
|
649
|
+
and sent down the wire to _Elasticsearch_. Not practical, you say? You're right.
|
647
650
|
|
648
651
|
Provided your model implements some sort of _pagination_ — and it probably does —, you can just run:
|
649
652
|
|
@@ -661,7 +664,7 @@ in chunks of 1000. If that number doesn't suit you, just provide a better one:
|
|
661
664
|
Any other parameters you provide to the `import` method are passed down to the `paginate` method.
|
662
665
|
|
663
666
|
Are we saying you have to fiddle with this thing in a `rails console` or silly Ruby scripts? No.
|
664
|
-
Just call the included _Rake_ task on the
|
667
|
+
Just call the included _Rake_ task on the command line:
|
665
668
|
|
666
669
|
```bash
|
667
670
|
$ rake environment tire:import CLASS='Article'
|
@@ -674,7 +677,14 @@ provided by the `mapping` block in your model):
|
|
674
677
|
$ rake environment tire:import CLASS='Article' FORCE=true
|
675
678
|
```
|
676
679
|
|
677
|
-
When
|
680
|
+
When importing records from multiple tables, you can fight the n+1 queries problem by passing
|
681
|
+
`include` parameters to the `paginate` method:
|
682
|
+
|
683
|
+
```bash
|
684
|
+
rake environment tire:import PARAMS='{:include => ["comments"]}' CLASS=Article FORCE=true
|
685
|
+
```
|
686
|
+
|
687
|
+
When you'll spend more time with _Elasticsearch_, you'll notice how
|
678
688
|
[index aliases](http://www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html)
|
679
689
|
are the best idea since the invention of inverted index.
|
680
690
|
You can index your data into a fresh index (and possibly update an alias once everything's fine):
|
@@ -683,6 +693,16 @@ You can index your data into a fresh index (and possibly update an alias once ev
|
|
683
693
|
$ rake environment tire:import CLASS='Article' INDEX='articles-2011-05'
|
684
694
|
```
|
685
695
|
|
696
|
+
Finally, consider the Rake importing task just a convenient starting point. If you're loading
|
697
|
+
substantial amounts of data, want better control on which data will be indexed, etc., use the
|
698
|
+
lower-level Tire API with eg. `ActiveRecordBase#find_in_batches`:
|
699
|
+
|
700
|
+
```ruby
|
701
|
+
Article.where("published_on > ?", Time.parse("2012-10-01")).find_in_batches do |group|
|
702
|
+
Tire.index("articles").import group
|
703
|
+
end
|
704
|
+
```
|
705
|
+
|
686
706
|
OK. All this time we have been talking about `ActiveRecord` models, since
|
687
707
|
it is a reasonable _Rails_' default for the storage layer.
|
688
708
|
|
@@ -708,7 +728,7 @@ Well, things stay mostly the same:
|
|
708
728
|
|
709
729
|
end
|
710
730
|
|
711
|
-
Article.create :title => 'I Love
|
731
|
+
Article.create :title => 'I Love Elasticsearch'
|
712
732
|
|
713
733
|
Article.tire.search 'love'
|
714
734
|
```
|
@@ -723,9 +743,9 @@ database to store stuff like `{ :name => 'Tire', :tags => [ 'ruby', 'search' ] }
|
|
723
743
|
Because all you need, really, is to just dump a JSON-representation of your data into a database and load it back again.
|
724
744
|
Because you've noticed that _searching_ your data is a much more effective way of retrieval
|
725
745
|
then constructing elaborate database query conditions.
|
726
|
-
Because you have _lots_ of data and want to use
|
746
|
+
Because you have _lots_ of data and want to use _Elasticsearch's_ advanced distributed features.
|
727
747
|
|
728
|
-
All good reasons to use
|
748
|
+
All good reasons to use _Elasticsearch_ as a schema-free and highly-scalable storage and retrieval/aggregation engine for your data.
|
729
749
|
|
730
750
|
To use the persistence mode, we'll include the `Tire::Persistence` module in our class and define its properties;
|
731
751
|
we can add the standard mapping declarations, set default values, or define casting for the property to create
|
@@ -759,7 +779,7 @@ and extensions to the core _Tire_ functionality — be sure to check them out.
|
|
759
779
|
Other Clients
|
760
780
|
-------------
|
761
781
|
|
762
|
-
Check out [other
|
782
|
+
Check out [other _Elasticsearch_ clients](http://www.elasticsearch.org/guide/appendix/clients.html).
|
763
783
|
|
764
784
|
|
765
785
|
Feedback
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# ===================================================================================================================
|
2
|
-
# Template for generating a no-frills Rails application with support for
|
2
|
+
# Template for generating a no-frills Rails application with support for Elasticsearch full-text search via Tire
|
3
3
|
# ===================================================================================================================
|
4
4
|
#
|
5
|
-
# This file creates a basic, fully working Rails application with support for
|
5
|
+
# This file creates a basic, fully working Rails application with support for Elasticsearch full-text search
|
6
6
|
# via the Tire gem [http://github.com/karmi/tire].
|
7
7
|
#
|
8
8
|
# You DON'T NEED ELASTICSEARCH INSTALLED, it is installed and launched automatically by this script.
|
@@ -14,7 +14,7 @@
|
|
14
14
|
# * Ruby >= 1.8.7
|
15
15
|
# * Rubygems
|
16
16
|
# * Rails >= 3.0.7
|
17
|
-
# * Sun Java 6 (for
|
17
|
+
# * Sun Java 6 (for Elasticsearch)
|
18
18
|
#
|
19
19
|
#
|
20
20
|
# Usage
|
@@ -46,7 +46,7 @@ end
|
|
46
46
|
at_exit do
|
47
47
|
pid = File.read("#{destination_root}/tmp/pids/elasticsearch.pid") rescue nil
|
48
48
|
if pid
|
49
|
-
say_status "Stop", "
|
49
|
+
say_status "Stop", "Elasticsearch", :yellow
|
50
50
|
run "kill #{pid}"
|
51
51
|
end
|
52
52
|
end
|
@@ -62,7 +62,7 @@ file ".gitignore", <<-END.gsub(/ /, '')
|
|
62
62
|
tmp/**/*
|
63
63
|
config/database.yml
|
64
64
|
db/*.sqlite3
|
65
|
-
vendor/elasticsearch-0.
|
65
|
+
vendor/elasticsearch-0.20.2/
|
66
66
|
END
|
67
67
|
|
68
68
|
git :init
|
@@ -71,32 +71,32 @@ git :commit => "-m 'Initial commit: Clean application'"
|
|
71
71
|
|
72
72
|
unless (RestClient.get('http://localhost:9200') rescue false)
|
73
73
|
COMMAND = <<-COMMAND.gsub(/^ /, '')
|
74
|
-
curl -k -L -# -o elasticsearch-0.
|
75
|
-
"http://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.
|
76
|
-
tar -zxf elasticsearch-0.
|
77
|
-
rm -f elasticsearch-0.
|
78
|
-
./elasticsearch-0.
|
74
|
+
curl -k -L -# -o elasticsearch-0.20.2.tar.gz \
|
75
|
+
"http://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.20.2.tar.gz"
|
76
|
+
tar -zxf elasticsearch-0.20.2.tar.gz
|
77
|
+
rm -f elasticsearch-0.20.2.tar.gz
|
78
|
+
./elasticsearch-0.20.2/bin/elasticsearch -p #{destination_root}/tmp/pids/elasticsearch.pid
|
79
79
|
COMMAND
|
80
80
|
|
81
81
|
puts "\n"
|
82
|
-
say_status "ERROR", "
|
82
|
+
say_status "ERROR", "Elasticsearch not running!\n", :red
|
83
83
|
puts '-'*80
|
84
|
-
say_status '', "It appears that
|
84
|
+
say_status '', "It appears that Elasticsearch is not running on this machine."
|
85
85
|
say_status '', "Is it installed? Do you want me to install it for you with this command?\n\n"
|
86
86
|
COMMAND.each_line { |l| say_status '', "$ #{l}" }
|
87
87
|
puts
|
88
88
|
say_status '', "(To uninstall, just remove the generated application directory.)"
|
89
89
|
puts '-'*80, ''
|
90
90
|
|
91
|
-
if yes?("Install
|
91
|
+
if yes?("Install Elasticsearch?", :bold)
|
92
92
|
puts
|
93
|
-
say_status "Install", "
|
93
|
+
say_status "Install", "Elasticsearch", :yellow
|
94
94
|
|
95
95
|
commands = COMMAND.split("\n")
|
96
96
|
exec = commands.pop
|
97
97
|
inside("vendor") do
|
98
98
|
commands.each { |command| run command }
|
99
|
-
run "(#{exec})" # Launch
|
99
|
+
run "(#{exec})" # Launch Elasticsearch in subshell
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
data/examples/tire-dsl.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
#
|
3
3
|
# **Tire** provides rich and comfortable Ruby API for the
|
4
|
-
# [
|
4
|
+
# [_Elasticsearch_](http://www.elasticsearch.org/) search engine/database.
|
5
5
|
#
|
6
|
-
#
|
6
|
+
# _Elasticsearch_ is a scalable, distributed, cloud-ready, highly-available
|
7
7
|
# full-text search engine and database, communicating by JSON over RESTful HTTP,
|
8
8
|
# based on [Lucene](http://lucene.apache.org/), written in Java.
|
9
9
|
#
|
@@ -40,14 +40,14 @@ require 'tire'
|
|
40
40
|
|
41
41
|
#### Prerequisites
|
42
42
|
|
43
|
-
# We'll need a working and running
|
43
|
+
# We'll need a working and running _Elasticsearch_ server, of course. Thankfully, that's easy.
|
44
44
|
( puts <<-"INSTALL" ; exit(1) ) unless (RestClient.get('http://localhost:9200') rescue false)
|
45
45
|
|
46
|
-
[ERROR] You don’t appear to have
|
46
|
+
[ERROR] You don’t appear to have Elasticsearch installed. Please install and launch it with the following commands:
|
47
47
|
|
48
|
-
curl -k -L -o elasticsearch-0.
|
49
|
-
tar -zxvf elasticsearch-0.
|
50
|
-
./elasticsearch-0.
|
48
|
+
curl -k -L -o elasticsearch-0.20.2.tar.gz http://github.com/downloads/elasticsearch/elasticsearch/elasticsearch-0.20.2.tar.gz
|
49
|
+
tar -zxvf elasticsearch-0.20.2.tar.gz
|
50
|
+
./elasticsearch-0.20.2/bin/elasticsearch -f
|
51
51
|
INSTALL
|
52
52
|
|
53
53
|
### Storing and indexing documents
|
@@ -68,7 +68,7 @@ Tire.index 'articles' do
|
|
68
68
|
store :title => 'One', :tags => ['ruby'], :published_on => '2011-01-01'
|
69
69
|
store :title => 'Two', :tags => ['ruby', 'python'], :published_on => '2011-01-02'
|
70
70
|
|
71
|
-
# We usually want to set a specific _type_ for the document in
|
71
|
+
# We usually want to set a specific _type_ for the document in _Elasticsearch_.
|
72
72
|
# Simply setting a `type` property is OK.
|
73
73
|
#
|
74
74
|
store :type => 'article',
|
@@ -137,7 +137,7 @@ Tire.index 'articles' do
|
|
137
137
|
#
|
138
138
|
:id => { :type => 'string', :index => 'not_analyzed', :include_in_all => false },
|
139
139
|
|
140
|
-
# ... set the boost or analyzer settings for the field, etc. The
|
140
|
+
# ... set the boost or analyzer settings for the field, etc. The _Elasticsearch_ guide
|
141
141
|
# has [more information](http://elasticsearch.org/guide/reference/mapping/index.html).
|
142
142
|
# Don't forget, that proper mapping is key to efficient and effective search.
|
143
143
|
# But don't fret about getting the mapping right the first time, you won't.
|
@@ -154,7 +154,7 @@ end
|
|
154
154
|
#### Bulk Indexing
|
155
155
|
|
156
156
|
# Of course, we may have large amounts of data, and adding them to the index one by one really isn't the best idea.
|
157
|
-
# We can use
|
157
|
+
# We can use _Elasticsearch's_ [bulk API](http://www.elasticsearch.org/guide/reference/api/bulk.html)
|
158
158
|
# for importing the data.
|
159
159
|
|
160
160
|
# So, for demonstration purposes, let's suppose we have a simple collection of hashes to store.
|
@@ -197,7 +197,7 @@ end
|
|
197
197
|
|
198
198
|
### Searching
|
199
199
|
|
200
|
-
# With the documents indexed and stored in the
|
200
|
+
# With the documents indexed and stored in the _Elasticsearch_ database, we can search them, finally.
|
201
201
|
#
|
202
202
|
# _Tire_ exposes the search interface via simple domain-specific language.
|
203
203
|
|
@@ -289,7 +289,7 @@ end
|
|
289
289
|
# We may use any valid [Lucene query syntax](http://lucene.apache.org/java/3_0_3/queryparsersyntax.html)
|
290
290
|
# for the `query_string` queries.
|
291
291
|
|
292
|
-
# For debugging our queries, we can display the JSON which is being sent to
|
292
|
+
# For debugging our queries, we can display the JSON which is being sent to _Elasticsearch_.
|
293
293
|
#
|
294
294
|
# {"query":{"query_string":{"query":"title:T*"}}}
|
295
295
|
#
|
@@ -342,7 +342,7 @@ end
|
|
342
342
|
#
|
343
343
|
Tire.configure do
|
344
344
|
|
345
|
-
# First of all, we can configure the URL for
|
345
|
+
# First of all, we can configure the URL for _Elasticsearch_.
|
346
346
|
#
|
347
347
|
url "http://search.example.com"
|
348
348
|
|
@@ -362,7 +362,7 @@ end
|
|
362
362
|
### Complex Searching
|
363
363
|
|
364
364
|
# Query strings are convenient for simple searches, but we may want to define our queries more expressively,
|
365
|
-
# using the
|
365
|
+
# using the _Elasticsearch_ [Query DSL](http://www.elasticsearch.org/guide/reference/query-dsl/index.html).
|
366
366
|
#
|
367
367
|
s = Tire.search('articles') do
|
368
368
|
|
@@ -471,7 +471,7 @@ Tire.search('articles') do
|
|
471
471
|
end
|
472
472
|
end
|
473
473
|
|
474
|
-
#
|
474
|
+
# _Elasticsearch_ supports many types of [queries](http://www.elasticsearch.org/guide/reference/query-dsl/).
|
475
475
|
#
|
476
476
|
# Eventually, _Tire_ will support all of them. So far, only these are supported:
|
477
477
|
#
|
@@ -487,7 +487,7 @@ end
|
|
487
487
|
|
488
488
|
#### Faceted Search
|
489
489
|
|
490
|
-
#
|
490
|
+
# _Elasticsearch_ makes it trivial to retrieve complex aggregated data from our index/database,
|
491
491
|
# so called [_facets_](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html).
|
492
492
|
|
493
493
|
# Let's say we want to display article counts for every tag in the database.
|
@@ -579,7 +579,7 @@ s.results.facets['global-tags']['terms'].each do |f|
|
|
579
579
|
puts "#{f['term'].ljust(10)} #{f['count']}"
|
580
580
|
end
|
581
581
|
|
582
|
-
#
|
582
|
+
# _Elasticsearch_ supports many advanced types of facets, such as those for computing statistics or geographical distance.
|
583
583
|
#
|
584
584
|
# Eventually, _Tire_ will support all of them. So far, only these are supported:
|
585
585
|
#
|
@@ -591,7 +591,7 @@ end
|
|
591
591
|
# * [terms_stats](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html)
|
592
592
|
# * [query](http://www.elasticsearch.org/guide/reference/api/search/facets/query-facet.html)
|
593
593
|
|
594
|
-
# We have seen that
|
594
|
+
# We have seen that _Elasticsearch_ facets enable us to fetch complex aggregations from our data.
|
595
595
|
#
|
596
596
|
# They are frequently used for another feature, „faceted navigation“.
|
597
597
|
# We can be combine query and facets with
|
@@ -777,7 +777,7 @@ end
|
|
777
777
|
#### Highlighting
|
778
778
|
|
779
779
|
# Often, we want to highlight the snippets matching our query in the displayed results.
|
780
|
-
#
|
780
|
+
# _Elasticsearch_ provides rich
|
781
781
|
# [highlighting](http://www.elasticsearch.org/guide/reference/api/search/highlighting.html)
|
782
782
|
# features, and _Tire_ makes them trivial to use.
|
783
783
|
#
|
@@ -786,7 +786,7 @@ s = Tire.search 'articles' do
|
|
786
786
|
# Let's search for documents containing word “Two” in their titles,
|
787
787
|
query { string 'title:Two' }
|
788
788
|
|
789
|
-
# and instruct
|
789
|
+
# and instruct _Elasticsearch_ to highlight relevant snippets.
|
790
790
|
#
|
791
791
|
highlight :title
|
792
792
|
end
|
@@ -818,7 +818,7 @@ end
|
|
818
818
|
|
819
819
|
#### Percolation
|
820
820
|
|
821
|
-
#
|
821
|
+
# _Elasticsearch_ comes with one very interesting, and rather unique feature:
|
822
822
|
# [_percolation_](http://www.elasticsearch.org/guide/reference/api/percolate.html).
|
823
823
|
|
824
824
|
# It works in a „reverse search“ manner to regular search workflow of adding
|
@@ -848,7 +848,7 @@ index = Tire.index('weather') do
|
|
848
848
|
end
|
849
849
|
|
850
850
|
# Notice, that we have added a _tags_ field to the query document, because it behaves
|
851
|
-
# just like any other document in
|
851
|
+
# just like any other document in _Elasticsearch_.
|
852
852
|
|
853
853
|
# We will refresh the `_percolator` index for immediate access.
|
854
854
|
#
|
@@ -864,7 +864,7 @@ matches = index.percolate(:message => '[Warning] Extreme flooding expected after
|
|
864
864
|
#
|
865
865
|
puts "Matching queries: " + matches.inspect
|
866
866
|
|
867
|
-
# We can filter the executed queries with a regular
|
867
|
+
# We can filter the executed queries with a regular _Elasticsearch_ query passed as a block to
|
868
868
|
# the `percolate` method.
|
869
869
|
#
|
870
870
|
matches = index.percolate(:message => '[Warning] Extreme flooding expected after tsunami wave.') do
|
@@ -918,7 +918,7 @@ puts "Matching queries: " + response['matches'].inspect
|
|
918
918
|
### ActiveModel Integration
|
919
919
|
|
920
920
|
# As you can see, [_Tire_](https://github.com/karmi/tire) supports the
|
921
|
-
# main features of
|
921
|
+
# main features of _Elasticsearch_ in Ruby.
|
922
922
|
#
|
923
923
|
# It allows you to create and delete indices, add documents, search them, retrieve the facets, highlight the results,
|
924
924
|
# and comes with a usable logging facility.
|