Skip to content

Commit dfe3f12

Browse files
committed
[MODEL] Added a "trigram" custom analyzer to the example
Related: elastic#697
1 parent 1630cf6 commit dfe3f12

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

elasticsearch-model/examples/activerecord_custom_analyzer.rb

+36-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ class Article < ActiveRecord::Base
3535
type: 'pattern',
3636
pattern: "_|-|\\.",
3737
lowercase: true
38+
},
39+
trigram: {
40+
tokenizer: 'trigram'
41+
}
42+
},
43+
tokenizer: {
44+
trigram: {
45+
type: 'ngram',
46+
min_gram: 3,
47+
max_gram: 3,
48+
token_chars: ['letter', 'digit']
3849
}
3950
}
4051
} } do
4152
mapping do
4253
indexes :title, type: 'text' do
4354
indexes :keyword, analyzer: 'keyword'
4455
indexes :pattern, analyzer: 'pattern'
56+
indexes :trigram, analyzer: 'trigram'
4557
end
4658
end
4759
end
@@ -52,7 +64,7 @@ class Article < ActiveRecord::Base
5264
Article.delete_all
5365
Article.create title: 'Foo'
5466
Article.create title: 'Foo-Bar'
55-
Article.create title: 'Foo_Bar_Baz'
67+
Article.create title: 'Foo_Bar_Bazooka'
5668
Article.create title: 'Foo.Bar'
5769

5870
# Index records
@@ -62,24 +74,31 @@ class Article < ActiveRecord::Base
6274

6375
puts '', '-'*80
6476

65-
puts "Fulltext analyzer [Foo_Bar_1]".ansi(:bold),
77+
puts "Fulltext analyzer [Foo_Bar_1_Bazooka]".ansi(:bold),
6678
"Tokens: " +
6779
Article.__elasticsearch__.client.indices
68-
.analyze(index: Article.index_name, body: { field: 'title', text: 'Foo_Bar_1' })['tokens']
80+
.analyze(index: Article.index_name, body: { field: 'title', text: 'Foo_Bar_1_Bazooka' })['tokens']
6981
.map { |d| "[#{d['token']}]" }.join(' '),
7082
"\n"
7183

72-
puts "Keyword analyzer [Foo_Bar_1]".ansi(:bold),
84+
puts "Keyword analyzer [Foo_Bar_1_Bazooka]".ansi(:bold),
7385
"Tokens: " +
7486
Article.__elasticsearch__.client.indices
75-
.analyze(index: Article.index_name, body: { field: 'title.keyword', text: 'Foo_Bar_1' })['tokens']
87+
.analyze(index: Article.index_name, body: { field: 'title.keyword', text: 'Foo_Bar_1_Bazooka' })['tokens']
7688
.map { |d| "[#{d['token']}]" }.join(' '),
7789
"\n"
7890

79-
puts "Pattern analyzer [Foo_Bar_1]".ansi(:bold),
91+
puts "Pattern analyzer [Foo_Bar_1_Bazooka]".ansi(:bold),
8092
"Tokens: " +
8193
Article.__elasticsearch__.client.indices
82-
.analyze(index: Article.index_name, body: { field: 'title.pattern', text: 'Foo_Bar_1' })['tokens']
94+
.analyze(index: Article.index_name, body: { field: 'title.pattern', text: 'Foo_Bar_1_Bazooka' })['tokens']
95+
.map { |d| "[#{d['token']}]" }.join(' '),
96+
"\n"
97+
98+
puts "Trigram analyzer [Foo_Bar_1_Bazooka]".ansi(:bold),
99+
"Tokens: " +
100+
Article.__elasticsearch__.client.indices
101+
.analyze(index: Article.index_name, body: { field: 'title.trigram', text: 'Foo_Bar_1_Bazooka' })['tokens']
83102
.map { |d| "[#{d['token']}]" }.join(' '),
84103
"\n"
85104

@@ -103,4 +122,14 @@ class Article < ActiveRecord::Base
103122

104123
puts '', '-'*80
105124

125+
response = Article.search query: { match: { 'title.trigram' => 'zoo' } } ;
126+
127+
puts "Trigram search for 'zoo'".ansi(:bold),
128+
"#{response.response.hits.total} matches: " +
129+
response.records.map { |d| d.title }.join(', '),
130+
"\n"
131+
132+
puts '', '-'*80
133+
134+
106135
require 'pry'; binding.pry;

0 commit comments

Comments
 (0)