@@ -35,13 +35,25 @@ class Article < ActiveRecord::Base
35
35
type : 'pattern' ,
36
36
pattern : "_|-|\\ ." ,
37
37
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' ]
38
49
}
39
50
}
40
51
} } do
41
52
mapping do
42
53
indexes :title , type : 'text' do
43
54
indexes :keyword , analyzer : 'keyword'
44
55
indexes :pattern , analyzer : 'pattern'
56
+ indexes :trigram , analyzer : 'trigram'
45
57
end
46
58
end
47
59
end
@@ -52,7 +64,7 @@ class Article < ActiveRecord::Base
52
64
Article . delete_all
53
65
Article . create title : 'Foo'
54
66
Article . create title : 'Foo-Bar'
55
- Article . create title : 'Foo_Bar_Baz '
67
+ Article . create title : 'Foo_Bar_Bazooka '
56
68
Article . create title : 'Foo.Bar'
57
69
58
70
# Index records
@@ -62,24 +74,31 @@ class Article < ActiveRecord::Base
62
74
63
75
puts '' , '-' *80
64
76
65
- puts "Fulltext analyzer [Foo_Bar_1 ]" . ansi ( :bold ) ,
77
+ puts "Fulltext analyzer [Foo_Bar_1_Bazooka ]" . ansi ( :bold ) ,
66
78
"Tokens: " +
67
79
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' ]
69
81
. map { |d | "[#{ d [ 'token' ] } ]" } . join ( ' ' ) ,
70
82
"\n "
71
83
72
- puts "Keyword analyzer [Foo_Bar_1 ]" . ansi ( :bold ) ,
84
+ puts "Keyword analyzer [Foo_Bar_1_Bazooka ]" . ansi ( :bold ) ,
73
85
"Tokens: " +
74
86
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' ]
76
88
. map { |d | "[#{ d [ 'token' ] } ]" } . join ( ' ' ) ,
77
89
"\n "
78
90
79
- puts "Pattern analyzer [Foo_Bar_1 ]" . ansi ( :bold ) ,
91
+ puts "Pattern analyzer [Foo_Bar_1_Bazooka ]" . ansi ( :bold ) ,
80
92
"Tokens: " +
81
93
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' ]
83
102
. map { |d | "[#{ d [ 'token' ] } ]" } . join ( ' ' ) ,
84
103
"\n "
85
104
@@ -103,4 +122,14 @@ class Article < ActiveRecord::Base
103
122
104
123
puts '' , '-' *80
105
124
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
+
106
135
require 'pry' ; binding . pry ;
0 commit comments