-
Notifications
You must be signed in to change notification settings - Fork 61
/
ch07_text_analysis.txt
1024 lines (923 loc) · 19.3 KB
/
ch07_text_analysis.txt
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
##
## A handy scripts file for hands-on exercises on Kibana
## Copy the whole contents of this file to Kibana's DevTools
## If you wish to follow the running commentary for these scripts,
## follow the Ch2 Getting Started wiki page here: https://github.com/madhusudhankonda/elasticsearch-in-action/wiki/Ch-7.-Text Analysis
##
## Please let me know if you find any issues with this script
# A standard (implicit) analyzer
GET _analyze
{
"text": "James Bond 007"
}
# A simple analyzer
GET _analyze
{
"text": "James Bond 007",
"analyzer": "simple"
}
# Configuring an analyzer on the fly
GET _analyze
{
"tokenizer": "path_hierarchy",
"filter": ["uppercase"],
"text": "/Volumes/FILES/Dev"
}
# Default "standard" analyser
GET _analyze
{
"text": "Hot cup of ☕ and a 🍿is a Weird Combo :(!!"
}
# Specifying the standard analyser
GET _analyze
{
"analyzer": "standard",
"text": "Hot cup of ☕ and a 🍿is a Weird Combo :(!!"
}
# Specifying the standard analyser with stop fitler
GET _analyze
{
"tokenizer": "standard",
"text": "Hot cup of ☕ and a 🍿is a Weird Combo :(!!",
"filter": ["lowercase"]
}
#Configuring analyzers
PUT my_index_with_stopwords
{
"settings": {
"analysis": {
"analyzer": {
"standard_with_stopwords":{
"type":"standard",
"stopwords":"_english_"
}
}
}
}
}
POST my_index_with_stopwords/_analyze
{
"text": ["Hot cup of ☕ and a 🍿is a Weird Combo :(!!"],
"analyzer": "standard_with_stopwords"
}
DELETE my_index_with_stopwords_hindi
PUT my_index_with_stopwords_hindi
{
"settings": {
"analysis": {
"analyzer": {
"standard_with_stopwords_hindi":{
"type":"standard",
"stopwords":"_hindi_"
}
}
}
}
}
POST my_index_with_stopwords_hindi/_analyze
{
"text": ["आप कैसे हैं?"],
"analyzer": "standard_with_stopwords_hindi"
}
POST my_index_with_stopwords_hindi/_analyze
{
"text": ["आप क्या कर रहे हो?"],
"analyzer": "standard_with_stopwords_hindi"
}
# Index with custom swear words
PUT index_with_swear_stopwords
{
"settings": {
"analysis": {
"analyzer": {
"swearwords_analyzer":{
"type":"standard",
"stopwords_path":"swearwords.txt"
}
}
}
}
}
#Test it
POST index_with_swear_stopwords/_analyze
{
"text": ["Damn, that sucks!"],
"analyzer": "swearwords_analyzer"
}
## Setting token length
DELETE my_index_with_max_token_length
PUT my_index_with_max_token_length
{
"settings": {
"analysis": {
"analyzer": {
"standard_max_token_length":{
"type":"standard",
"max_token_length":7
}
}
}
}
}
POST my_index_with_max_token_length/_analyze
{
"text": ["elasticsearch"],
"analyzer": "standard_max_token_length"
}
# Simple analyzer
POST _analyze
{
"text": ["Lukša's K8s in Action"],
"analyzer": "simple"
}
# Whitespace Analyzer
POST _analyze
{
"text":"Peter_Piper picked a peck of PICKLED-peppers!!",
"analyzer": "whitespace"
}
# Keyword Analyzer
POST _analyze
{
"text":"Elasticsearch in Action",
"analyzer": "keyword"
}
# Fingerprint analyzer
POST _analyze
{
"text": "A dosa is a thin pancake or crepe originating from South India. It is made from a fermented batter consisting of lentils and rice.",
"analyzer": "fingerprint"
}
# Pattern
POST _analyze
{
"text":"Is is true?",
"pattern": "[tT]rue|[yY]es",
"analyzer": "pattern"
}
# Additional example - a pattern analyzer
DELETE index_with_pattern_analyzer
PUT index_with_pattern_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"pattern_analyzer": {
"type": "pattern",
"pattern": "[a-zA-Z]{3}",
"lowercase": true
}
}
}
}
}
# Dash pattern
PUT index_with_dash_pattern_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"pattern_analyzer": {
"type": "pattern",
"pattern": "[-]",
"lowercase": true
}
}
}
}
}
POST index_with_dash_pattern_analyzer/_analyze
{
"text": "1234-5678-9000-0000",
"analyzer": "pattern_analyzer"
}
# English Language Analyzer
POST _analyze
{
"text": "She sells sea shells",
"analyzer": "english"
}
# German Language Analyzer
POST _analyze
{
"text": "Guten Morgen",
"analyzer": "german"
}
# English Language Analyzer
POST _analyze
{
"text": "नमस्ते कैसी हो तुम",
"analyzer": "hindi"
}
DELETE index_with_custom_english_analyzer
# Custom Language Analyser with stopwords
PUT index_with_custom_english_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"index_with_custom_english_analyzer":{
"type":"english",
"stopwords":["a","an","is","and","for"]
}
}
}
}
}
POST index_with_custom_english_analyzer/_analyze
{
"text":"A dog is for a life",
"analyzer":"index_with_custom_english_analyzer"
}
# Stemming
POST index_with_custom_english_analyzer/_analyze
{
"text":"Author authors authoring authored authorization authority",
"analyzer":"english"
}
DELETE index_with_stem_exclusion_english_analyzer
# Custom Language Analyser with stem_exception
PUT index_with_stem_exclusion_english_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"stem_exclusion_english_analyzer":{
"type":"english",
"stem_exclusion":["authority","authorization"]
}
}
}
}
}
POST index_with_stem_exclusion_english_analyzer/_analyze
{
"text": "No one can challenge my authority without my authorization",
"analyzer": "stem_exclusion_english_analyzer"
}
DELETE index_with_custom_analyzer
## Custom Analyzer
PUT index_with_custom_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer":{
"type":"custom",
"char_filter":["html_strip"],
"tokenizer":"standard",
"filter":["uppercase"]
}
}
}
}
}
POST index_with_custom_analyzer/_analyze
{
"text": "<H1>HELLO, WoRLD</H1>",
"analyzer": "custom_analyzer"
}
# Advanced Custom Analyser
PUT index_with_custom_greek_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer":{
"type":"custom",
"char_filter":["greek_symbol_mapper"],
"tokenizer":"standard",
"filter":["uppercase"]
}
},
"char_filter": {
"greek_symbol_mapper":{
"type":"mapping",
"mappings":[
"α => alpha",
"β => Beta",
"γ => Gamma"
]
}
}
}
}
}
POST index_with_custom_greek_analyzer/_analyze
{
"text": "α and β are roots of a quadratic equation",
"analyzer": "custom_analyzer"
}
DELETE index_with_parse_greek_letters_custom_analyzer
PUT index_with_parse_greek_letters_custom_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"greek_letter_custom_analyzer":{
"type":"custom",
"char_filter":["greek_symbol_mapper"],
"tokenizer":"standard",
"filter":["lowercase", "greek_keep_words"]
}
},
"char_filter": {
"greek_symbol_mapper":{
"type":"mapping",
"mappings":[
"α => alpha",
"β => Beta",
"γ => Gamma"
]
}
},
"filter": {
"greek_keep_words":{
"type":"keep",
"keep_words":["alpha", "beta", "gamma"]
}
}
}
}
}
POST index_with_parse_greek_letters_custom_analyzer/_analyze
{
"text": "α and β are roots of a quadratic equation. γ isn't",
"analyzer": "greek_letter_custom_analyzer"
}
## Specifying the analysers
# Setting analyzers at field level during index creation
PUT authors_with_field_level_analyzers
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"about":{
"type": "text",
"analyzer": "english"
},
"description":{
"type": "text",
"fields": {
"my":{
"type": "text",
"analyzer": "fingerprint"
}
}
}
}
}
}
# Creating an index with a default analyzer
PUT authors_with_default_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"default":{
"type":"keyword"
}
}
}
}
}
# Test the analyzer
PUT authors_with_default_analyzer/_analyze
{
"name":"John Doe"
}
# Setting the analyzer alongside a search query
GET authors_index_for_search_analyzer/_search
{
"query": {
"match": {
"author_name": {
"query": "M Konda",
"analyzer": "simple"
}
}
}
}
# Setting a search analyzer at a field level
PUT authors_index_with_both_analyzers_field_level
{
"mappings": {
"properties": {
"author_name":{
"type": "text",
"analyzer": "stop",
"search_analyzer": "simple"
}
}
}
}
# Setting a search analyzer at a field level
PUT authors_index_with_both_analyzers_field_level
{
"mappings": {
"properties": {
"author_name":{
"type": "text",
"analyzer": "stop",
"search_analyzer": "simple"
}
}
}
}
# Specifying index and search analyzers during the index creation
PUT authors_index_with_both_analyzers_field_level
{
"mappings": {
"properties": {
"author_name":{
"type": "text",
"analyzer": "standard",
"search_analyzer": "simple"
}
}
}
}
## Character Filters
# HTML Strip Character Filter
POST _analyze
{
"text":"<h1>Where is my cheese?</h1>",
"tokenizer": "standard",
"char_filter": ["html_strip"]
}
DELETE index_with_html_strip_filter
PUT index_with_html_strip_filter
{
"settings": {
"analysis": {
"analyzer": {
"custom_html_strip_filter_analyzer":{
"tokenizer":"keyword",
"char_filter":["my_html_strip_filter"]
}
},
"char_filter": {
"my_html_strip_filter":{
"type":"html_strip",
"escaped_tags":["h1"]
}
}
}
}
}
POST index_with_html_strip_filter/_analyze
{
"text": "<h1>Hello,</h1> <h2>World!</h2>",
"analyzer": "custom_html_strip_filter_analyzer"
}
# Mapping Char Filter
POST _analyze
{
"text": "I am from UK",
"char_filter": [
{
"type": "mapping",
"mappings": [
"UK => United Kingdom"
]
}
]
}
DELETE index_with_mapping_char_filter
# Custom analyzer wiht a mapping characte filter
PUT index_with_mapping_char_filter
{
"settings": {
"analysis": {
"analyzer": {
"my_social_abbreviations_analyzer": {
"tokenizer": "keyword",
"char_filter": [
"my_social_abbreviations"
]
}
},
"char_filter": {
"my_social_abbreviations": {
"type": "mapping",
"mappings": [
"LOL => laughing out loud",
"BRB => be right back",
"OMG => oh my god"
]
}
}
}
}
}
POST index_with_mapping_char_filter/_analyze
{
"text": "LOL",
"analyzer": "my_social_abbreviations_analyzer"
}
#Character filter using mappings from a file
POST _analyze
{
"text": "FBI and CIA are USA's security organizations",
"char_filter": [
{
"type": "mapping",
"mappings_path": "secret_organizations.txt"
}
]
}
# Pattern replce charcter filter
DELETE index_with_pattern_replace_filter
PUT index_with_pattern_replace_filter
{
"settings": {
"analysis": {
"analyzer": {
"my_pattern_replace_analyzer":{
"tokenizer":"keyword",
"char_filter":["pattern_replace_filter"]
}
},
"char_filter": {
"pattern_replace_filter":{
"type":"pattern_replace",
"pattern":"_",
"replacement":"-"
}
}
}
}
}
POST index_with_pattern_replace_filter/_analyze
{
"text": "Apple_Boy_Cat",
"analyzer": "my_pattern_replace_analyzer"
}
PUT my-index-00001
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": [
"my_char_filter"
]
}
},
"char_filter": {
"my_char_filter": {
"type": "pattern_replace",
"pattern": "(\\d+)-(?=\\d)",
"replacement": "$1_"
}
}
}
}
}
POST my-index-00001/_analyze
{
"analyzer": "my_analyzer",
"text": "123-456-789"
}
## Tokenizers
# Standard tokenizer
POST _analyze
{
"text": "Hello,cruel world!",
"tokenizer": "standard"
}
# Cutom stadard tokenizer
PUT index_with_custom_standard_tokenizer
{
"settings": {
"analysis": {
"analyzer": {
"custom_token_length_analyzer": {
"tokenizer": "custom_token_length_tokenizer"
}
},
"tokenizer": {
"custom_token_length_tokenizer": {
"type": "standard",
"max_token_length": 2
}
}
}
}
}
POST index_with_custom_standard_tokenizer/_analyze
{
"text": "Bond",
"analyzer": "custom_token_length_analyzer"
}
# URL Tokenizer
POST _analyze
{
"text": "My website is https://localhost:9090",
"tokenizer": "uax_url_email"
}
# Email
POST _analyze
{
"text": "my email is dimpy@dimpy.com",
"tokenizer": "uax_url_email"
}
# N-Gram Tokenizer
POST _analyze
{
"text": "Bond",
"tokenizer": "ngram"
}
# Custom n-gram tokenizer
DELETE index_with_ngram_tokenizer
PUT index_with_ngram_tokenizer
{
"settings": {
"analysis": {
"analyzer": {
"ngram_analyzer":{
"tokenizer":"ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer":{
"type":"ngram",
"min_gram":2,
"max_gram":3,
"token_chars":[
"letter"
]
}
}
}
}
}
POST index_with_ngram_tokenizer/_analyze
{
"text": "bond",
"analyzer": "ngram_analyzer"
}
#Edge-ngram
POST _analyze
{
"text":"hello",
"tokenizer": "edge_ngram"
}
DELETE index_with_edge_ngram
PUT index_with_edge_ngram
{
"settings": {
"analysis": {
"analyzer": {
"edge_ngram_analyzer":{
"tokenizer":"my_edge_ngram_tokenizer"
}
},
"tokenizer": {
"my_edge_ngram_tokenizer":{
"type":"edge_ngram",
"min_gram":2,
"max_gram":6,
"token_chars":["letter","digit"]
}
}
}
}
}
POST index_with_edge_ngram/_analyze
{
"text": "bond",
"analyzer": "edge_ngram_analyzer"
}
## Simple Pattern Analyzer
POST _analyze
{
"text": "hello",
"tokenizer": "simple_pattern"
}
DELETE index_simple_pattern_analyzer
PUT index_simple_pattern_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"my_simple_pattern_analyzer":{
"tokenizer":"simple_pattern_tokenizer"
}
},
"tokenizer": {
"simple_pattern_tokenizer":{
"type": "simple_pattern",
"pattern":"[a-zA-Z]{2}"
}
}
}
}
}
POST index_simple_pattern_analyzer/_analyze
{
"text": "sugarfree",
"analyzer": "my_simple_pattern_analyzer"
}
DELETE index_with_simple_pattern_split_analyzer
#Simple pattern split tokenizer
PUT index_with_simple_pattern_split_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"simple_pattern_split_analyzer":{
"tokenizer":"simple_pattern_split_tokenizer"
}
}
,"tokenizer": {
"simple_pattern_split_tokenizer":{
"type":"simple_pattern_split",
"pattern":">"
}
}
}
}
}
POST index_with_simple_pattern_split_analyzer/_analyze
{
"text": "c>file",
"analyzer": "simple_pattern_split_analyzer"
}
# Path Hierarchy
POST _analyze
{
"text": "/Volumes/doe/documents",
"tokenizer": "path_hierarchy"
}
## Token Fitlers
GET _analyze
{
"tokenizer" : "standard",
"filter" : ["uppercase","reverse"],
"text" : "bond"
}
PUT index_with_token_filters
{
"settings": {
"analysis": {
"analyzer": {
"token_filter_analyser": {
"tokenizer": "standard",
"filter": [
"uppercase",
"reverse"
]
}
}
}
}
}
POST index_with_token_filters/_analyze
{
"analyzer": "token_filter_analyser",
"text": "bond"
}
#Stemmer
POST _analyze
{
"tokenizer": "standard",
"filter": ["stemmer"],
"text": "barking is my life"
}
#Shingles
POST _analyze
{
"tokenizer": "standard",
"filter": ["shingle"],
"text": "java python go"
}
DELETE index_with_shingle
PUT index_with_shingle
{
"settings": {
"analysis": {
"analyzer": {
"shingles_analyzer":{
"tokenizer":"standard",
"filter":["shingles_filter"]
}
},
"filter": {
"shingles_filter":{
"type":"shingle",
"min_shingle_size":2,
"max_shingle_size":3,
"output_unigrams":false
}
}
}
}
}
POST index_with_shingle/_analyze
{
"text": "java python go",
"analyzer": "shingles_analyzer"
}
DELETE index_with_synonyms
# Index with a synonyms filter
PUT index_with_synonyms
{
"settings": {
"analysis": {
"filter": {
"synonyms_filter":{
"type":"synonym",
"synonyms":[ "soccer => football"]
}
}
}
}
}
# Test the index - the output should be "What" "Football"
POST index_with_synonyms/_analyze
{
"text": "What's soccer?",
"tokenizer": "standard",
"filter": ["synonyms_filter"]
}
# Index with synonyms filter - reading the synonyms from the code itself
# Thanks to Thomas Hill - he mentioned a mistake in my earlier verion - where the analyzer was set to standard.
# if you set the type to "standard", then Elasticsearch only looks at the parameters for Standard Analyzer,
# such as "stopwords_path". It does NOT look at parameters for a custom analyzer, such as "filter".
# Thank you @Thomas Hill!
PUT index_with_synonyms_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"synonyms_analyzer":{
"type":"custom",
"tokenizer":"standard",
"filter":["synonyms_filter"]
}
}
,"filter": {
"synonyms_filter":{
"type":"synonym",
"synonyms":["lol => Laughing out loud"]
}
}
}
}
}
POST index_with_synonyms_analyzer/_analyze
{
"text":"lol",
"analyzer": "synonyms_analyzer"
}
GET index_with_synonyms/_search
{
"query": {
"match": {
"sport": {
"query": "football",
"analyzer": "synonyms_analyzer"
}
}
}
}
# Synonyms from a file.
# Note I've set the type to be standard and tested with 8.5 version - it seems working.
# Should you have any issues, do change it to "custom" as per previous
# comments (see line 946) and try it out.
PUT index_with_synonyms_from_file_analyzer
{
"settings": {
"analysis": {
"analyzer": {
"synonyms_analyzer":{
"type":"standard",
"filter":["synonyms_from_file_filter"]
}
},
"filter": {