├── .gitignore ├── README.md ├── book_01.txt ├── book_02.txt ├── book_03.txt ├── book_04.txt ├── book_05.txt ├── book_06.txt ├── book_06_data.txt ├── book_07.txt ├── book_08_suggestion.txt ├── book_09_ngram.txt ├── book_mapping.yaml ├── boost_by_popularity.txt ├── boost_index.txt ├── boost_query.txt ├── boost_workshop.txt ├── car.txt ├── category_problem.txt ├── cluster.txt ├── dynamic_template.txt ├── geo.txt ├── homepro.txt ├── hunspell.txt ├── imc_ais ├── main_slide │ ├── SPRINT3R-ELASTICSEARCH-AIS.pdf │ ├── book_data.txt │ └── demo_01.txt └── workshop │ ├── SPRINT3R-ELASTICSEARCH-AGGREGATION-WORKSHOP.pdf │ └── car.txt ├── kaidee ├── 02_cluster.txt ├── backup_recovery.txt ├── master_election.txt ├── replica │ ├── node01.yml │ └── node02.yml ├── s3_backup.txt ├── scripting.txt ├── single_responsibility │ ├── node01.yml │ ├── node02.yml │ ├── node03.yml │ └── node04.yml ├── slide │ ├── SPRINT3R-01-NEW-ELASTICSEARCH-KIBANA.pdf │ ├── SPRINT3R-02-CONFIG-CLUSTER-FARM.pdf │ ├── SPRINT3R-03-SCRIPTING.pdf │ ├── SPRINT3R-04-DYNAMIC-MAPPING-TEMPLATE.pdf │ └── SPRINT3R-05-BACKUP-RECOVERY.pdf ├── stop_node.txt ├── template.txt ├── template_kaidee.json └── unicast.txt ├── logstash ├── elasticsearch.conf ├── filter.conf └── simple.config ├── logstash_example └── logstash-apache.conf ├── modeling_data.txt ├── note_01.txt ├── preserve_special_character.txt ├── problem └── sort_alphabet.txt ├── sentiment.py ├── sentiment02.py ├── slide01 ├── ELASTICSEARCH-STARTER-GUIDE.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-01.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-02-CRUD.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-03-BULK-API.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-04-ANALYZER.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-05-MAPPING.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-06-SEARCH.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-07-AGGREGATION-TEST-DRIVE.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-07-AGGREGATION.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-AUTO-SUGGESTION-NGRAM.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-AUTO-SUGGESTION.pdf ├── SPRINT3R-ELASTICSEARCH-BOOST.pdf ├── SPRINT3R-ELASTICSEARCH-CLUSTER.pdf ├── SPRINT3R-ELASTICSEARCH-GEO-LOCATION.pdf ├── SPRINT3R-ELASTICSEARCH-INSTALLATION.pdf ├── SPRINT3R-ELASTICSEARCH-INTRO.pdf ├── SPRINT3R-ELASTICSEARCH-KIBANA.pdf ├── SPRINT3R-ELASTICSEARCH-LOGSTASH.pdf ├── SPRINT3R-ELASTICSEARCH-MODELING-DATA.pdf ├── SPRINT3R-ELASTICSEARCH-PRESERVE-SPECIAL-CHARACTER.pdf ├── SPRINT3R-ELASTICSEARCH-TIPS-PRODUCTION.pdf ├── SPRINT3R-ELASTICSEARCH-TIPS.pdf └── SPRINT3R-ELASTICSEARCH-WORKSHOP_LOGS.pdf ├── slide02 ├── SPRINT3R-ELASTICSEARCH-BOOK-01.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-02-CRUD.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-03-BULK-API.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-04-ANALYZER.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-05-MAPPING.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-06-SEARCH.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-07-AGGREGATION-TEST-DRIVE.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-07-AGGREGATION.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-AUTO-SUGGESTION-NGRAM.pdf ├── SPRINT3R-ELASTICSEARCH-BOOK-AUTO-SUGGESTION.pdf ├── SPRINT3R-ELASTICSEARCH-BOOST.pdf ├── SPRINT3R-ELASTICSEARCH-CLUSTER.pdf ├── SPRINT3R-ELASTICSEARCH-GEO-LOCATION.pdf ├── SPRINT3R-ELASTICSEARCH-INSTALLATION-FULL.pdf ├── SPRINT3R-ELASTICSEARCH-INSTALLATION.pdf ├── SPRINT3R-ELASTICSEARCH-INTRO.pdf ├── SPRINT3R-ELASTICSEARCH-KIBANA.pdf ├── SPRINT3R-ELASTICSEARCH-LOGSTASH.pdf ├── SPRINT3R-ELASTICSEARCH-MODELING-DATA.pdf ├── SPRINT3R-ELASTICSEARCH-TIPS-PRODUCTION.pdf ├── SPRINT3R-ELASTICSEARCH-TIPS.pdf └── SPRINT3R-ELASTICSEARCH-WORKSHOP_LOGS.pdf ├── slide_fastlane ├── SPRINT3R-ELASTICSEARCH-01-INSTALLATION.pdf └── SPRINT3R-ELASTICSEARCH-02-INTRODUCTION-FAST-LANE.pdf ├── sport.txt ├── synonym.txt ├── thailand_city_geo.txt ├── tip_config.txt ├── tips.txt └── twitter.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # elasticsearch_workshop 2 | Example for Elasticsearch workshop 3 | -------------------------------------------------------------------------------- /book_01.txt: -------------------------------------------------------------------------------- 1 | PUT /store/book/1 2 | { 3 | "title" : "Elasticsearch: The Definitive Guide", 4 | "author_name" : ["Clinton Gormley", "Zachary Tong"], 5 | "tag" : ["search", "computer"], 6 | "isbn-13" : "978-1449358549", 7 | "isbn-10" : "1449358543", 8 | "price" : 44.30, 9 | "page" : 724, 10 | "description" : "A Distributed Real-Time Search and Analytics Engine", 11 | "detail" : "Learn how to use Elasticsearch, an open source, distributed, RESTful search engine built on top of Apache Lucene. Each chapter in this book tackles a particular facet of Elasticsearch with separate sections for beginners more advanced programmers. If you’re a beginner, advanced techniques are not required reading, but you can revisit them once you have a solid understanding of the basics." 12 | } 13 | 14 | 15 | PUT /store/book/2 16 | { 17 | "title" : "Clean Code: A Handbook of Agile Software Craftsmanship", 18 | "author_name" : [" Robert C. Martin"], 19 | "tag" : ["development", "computer"], 20 | "isbn-13" : "000-0132350882", 21 | "isbn-10" : "0132350882", 22 | "price" : 34.16, 23 | "page" : 464, 24 | "description" : "Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees.", 25 | "detail" : "Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn’t have to be that way." 26 | } 27 | 28 | 29 | GET /store/book/1 30 | 31 | GET /store/book/2 32 | 33 | GET /store/book/_search 34 | 35 | GET /store/book/_search?q=title:Elasticsearch 36 | 37 | GET /store/book/_search 38 | { 39 | "query" : { 40 | "match" : { 41 | "title" : "Elasticsearch" 42 | } 43 | } 44 | } 45 | 46 | GET /store/book/_search 47 | { 48 | "query" : { 49 | "match" : { 50 | "_all" : "Elasticsearch" 51 | } 52 | } 53 | } 54 | 55 | 56 | 57 | GET /store/book/_search 58 | { 59 | "query" : { 60 | "filtered" : { 61 | "filter" : { 62 | "range" : { 63 | "page":{"gt":500} 64 | 65 | } 66 | }, 67 | "query" : { 68 | "match" : { 69 | "title" : "Elasticsearch" 70 | } 71 | } 72 | } 73 | } 74 | } 75 | 76 | GET store/_search 77 | { 78 | "query": { 79 | "bool": { 80 | "must": [ 81 | { 82 | "match": { 83 | "title": "elasticsearch" 84 | } 85 | } 86 | ], 87 | "filter": { 88 | "range": { 89 | "page": { 90 | "gte": 700 91 | } 92 | } 93 | } 94 | } 95 | } 96 | } 97 | 98 | 99 | GET /store/book/_search 100 | { 101 | "query" : { 102 | "match_phrase" : { 103 | "_all" : "open source" 104 | } 105 | } } 106 | 107 | 108 | GET /store/book/_search 109 | { 110 | "query" : { 111 | "match_phrase" : { 112 | "_all" : "clean" 113 | }}, 114 | "highlight": { 115 | "fields" : { 116 | "detail" : {}, 117 | "title" : {} 118 | } 119 | } 120 | } 121 | 122 | 123 | GET /store/book/_search 124 | { 125 | "aggs": { 126 | "all_tags": { 127 | "terms": { 128 | "field": "tag" 129 | } 130 | } 131 | } 132 | } 133 | 134 | 135 | GET /store/book/_search 136 | { 137 | "query": { 138 | "match_phrase": { 139 | "_all": "clean" 140 | } 141 | }, 142 | "aggs": { 143 | "all_tags": { 144 | "terms": { 145 | "field": "tag" 146 | } 147 | } 148 | } 149 | } 150 | 151 | -------------------------------------------------------------------------------- /book_02.txt: -------------------------------------------------------------------------------- 1 | //Automatic generate ID of document 2 | POST /store/book/ 3 | { 4 | "title" : "Scrum: a Breathtakingly Brief and Agile Introduction", 5 | "author_name" : ["Chris Sims", "Hillary Louise Johnson"], 6 | "tag" : ["scrum", "agile","computer"], 7 | "asin" : "B007P5N8D4", 8 | "price" : 2.99, 9 | "page" : 54, 10 | "description" : "A pocket-sized overview of roles, artifacts and the sprint cycle, adapted from the bestsellerThe Elements of Scrum by Chris Sims & Hillary Louise Johnson ", 11 | "detail" : "A pocket-sized overview of roles, artifacts and the sprint cycle, adapted from the bestsellerThe Elements of Scrum by Chris Sims & Hillary Louise Johnson" 12 | } 13 | 14 | 15 | //Retrieving part of a document 16 | GET /store/book/1?_source=title,description 17 | 18 | 19 | //Check existing document 20 | curl -i -XHEAD http://localhost:9200/store/book/1 21 | 22 | curl -i -XHEAD http://localhost:9200/store/book/100 23 | 24 | //Update whole document 25 | PUT /store/book/123 26 | { 27 | "title" : "Update", 28 | "author_name" : ["user1", "user2"], 29 | "tag" : ["update", "book"] 30 | } 31 | 32 | //Partial update document 33 | POST /store/book/1/_update 34 | { 35 | "doc" : { 36 | "title" : "partial update", 37 | "tag" : [ "test", "computer" ], 38 | "views": 0 39 | } } 40 | 41 | //Partial update with script 42 | POST /store/book/1/_update 43 | { 44 | "script" : "ctx._source.views+=1" 45 | } 46 | 47 | 48 | POST /store/book/1/_update 49 | { 50 | "script" : "ctx._source.tag+=new_tag", 51 | "params" : { 52 | "new_tag" : "search" 53 | } 54 | } 55 | 56 | //Partial update with not-existing field 57 | POST /store/book/2/_update => BUG !!!! 58 | { 59 | "script": "ctx._source.views+=1", 60 | "upsert": { 61 | "views": 1 62 | } 63 | } 64 | 65 | //Resolve https://gist.github.com/dakrone/432f2800eb83939114e5 66 | 67 | POST /store/book/2/_update 68 | { 69 | "script": "if (!ctx._source.views) {ctx._source.views = 1};ctx._source.views += views", 70 | "params": { 71 | "views": 5 72 | } 73 | } 74 | 75 | 76 | 77 | //Problem duplicate ID 78 | PUT /store/book/123?op_type=create 79 | { 80 | "title" : "New", 81 | "author_name" : ["user1", "user2"], 82 | "tag" : ["new", "book"] 83 | } 84 | 85 | PUT /store/book/123/_create 86 | { 87 | "title" : "New", 88 | "author_name" : ["user1", "user2"], 89 | "tag" : ["new", "book"] 90 | } 91 | 92 | 93 | //Delete by query 94 | DELETE /store/book/_query 95 | { 96 | "query": { 97 | "term": { 98 | "tag": "elasticsearch" 99 | } 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /book_03.txt: -------------------------------------------------------------------------------- 1 | //Retrieve multiple documents 2 | GET /_mget 3 | { 4 | "docs": [ 5 | { 6 | "_index": "store", 7 | "_type": "book", 8 | "_id": 1, 9 | "_source": [ 10 | "title", 11 | "views" 12 | ] 13 | }, 14 | { 15 | "_index": "store", 16 | "_type": "book", 17 | "_id": 2, 18 | "_source": "title" 19 | } 20 | ] 21 | } 22 | 23 | 24 | GET /store/book/_mget 25 | { 26 | "docs": [ 27 | { 28 | "_id": 1, 29 | "_source": [ "title","views"] 30 | }, 31 | { 32 | "_id": 2, 33 | "_source": "title" 34 | } 35 | ] 36 | } 37 | 38 | 39 | GET /store/book/_mget 40 | { 41 | "ids": [1,2] 42 | } 43 | 44 | 45 | //Bulk API 46 | 47 | 48 | //Create new document 49 | POST /_bulk 50 | {"create":{"_index":"store","_type":"book","_id":"1001"}} 51 | {"title":"new book 1000", "description":"my new book"} 52 | 53 | //Index document + auto ID 54 | POST /_bulk 55 | {"index":{"_index":"store","_type":"book"}} 56 | {"title":"new book xxx", "description":"my new book with auto id"} 57 | 58 | 59 | 60 | POST /_bulk 61 | { "delete": { "_index": "store", "_type": "book", "_id": "1001" }} 62 | { "create": { "_index": "store", "_type": "book", "_id": "1001" }} 63 | { "title": "My new book" } 64 | { "index": { "_index": "store", "_type": "book" }} 65 | { "title": "My second book" } 66 | { "update": { "_index": "store", "_type": "book", "_id": "1001", "_retry_on_conflict" : 3} } 67 | { "doc" : {"title" : "My updated book"} } 68 | 69 | 70 | POST /store/book/_bulk 71 | { "delete": { "_id": "1001" }} 72 | { "create": { "_id": "1001" }} 73 | { "title": "My new book" } 74 | { "index": { }} 75 | { "title": "My second book" } 76 | { "update": { "_id": "1001", "_retry_on_conflict" : 3} } 77 | { "doc" : {"title" : "My updated book"} } 78 | 79 | 80 | //Add log with Bulk api 81 | POST /store/log/_bulk 82 | { "create": { "_id": "1" }} 83 | { "code":"200", "message":"success" } 84 | { "update": { "_id": "2", "_retry_on_conflict" : 3}} 85 | { "doc" : {"code" : "404", "message" : "not found"}} 86 | { "create": { "_id": "3" }} 87 | { "code":"200", "message":"success" } 88 | { "update": { "_id": "4", "_retry_on_conflict" : 3}} 89 | { "doc" : {"code" : "404", "message" : "not found"}} 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /book_04.txt: -------------------------------------------------------------------------------- 1 | //Index new documents 2 | DELETE store 3 | 4 | PUT /store/book/1 5 | { 6 | "title" : "Elasticsearch: The Definitive Guide", 7 | "published_date" : "2015-01-15" 8 | } 9 | 10 | PUT /store/book/2 11 | { 12 | "title" : "Elasticsearch: The Definitive Guide", 13 | "published_date" : "2015-01-16" 14 | } 15 | 16 | PUT /store/book/3 17 | { 18 | "title" : "Elasticsearch: The Definitive Guide", 19 | "published_date" : "2015-01-17" 20 | } 21 | 22 | //Search data 23 | GET /store/book/_search?q=2015 24 | GET /store/book/_search?q=2015-01-15 25 | GET /store/book/_search?q=published_date:2015-01-15 26 | GET /store/book/_search?q=published_date:2015 27 | 28 | //Mapping of book 29 | GET /store/_mapping/book 30 | 31 | 32 | 33 | //Analyze 34 | 35 | //Standard analyzer 36 | GET /_analyze?analyzer=standard&text=Set the shape to semi-transparent by calling set_trans(5) 37 | 38 | //Simple analyzer 39 | GET /_analyze?analyzer=simple&text=Set the shape to semi-transparent by calling set_trans(5) 40 | 41 | //Whitespace analyzer 42 | GET /_analyze?analyzer=whitespace&text=Set the shape to semi-transparent by calling set_trans(5) 43 | 44 | 45 | //Language analyzer 46 | GET /_analyze?analyzer=english&text=Set the shape to semi-transparent by calling set_trans(5) 47 | 48 | 49 | //Thai analyzer 50 | http://localhost:9200/store/_analyze?analyzer=thai&text=สวัสดีประเทศไทย 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /book_05.txt: -------------------------------------------------------------------------------- 1 | //Mapping 2 | 3 | //Create Index 4 | PUT /store 5 | 6 | //Get mapping of book 7 | GET /store/_mapping/book/ 8 | 9 | 10 | //Update mapping 11 | 12 | //Index and Analyzer 13 | 14 | DELETE /store 15 | 16 | PUT /store 17 | { 18 | "mappings": { 19 | "book": { 20 | "properties": { 21 | "title": { 22 | "type": "string", 23 | "analyzer": "english" 24 | }, 25 | "published_date": { 26 | "type": "date" 27 | } 28 | } 29 | } 30 | } 31 | } 32 | 33 | 34 | //Update mapping 35 | PUT /store/_mapping/book 36 | { 37 | "properties": { 38 | "tag": { 39 | "type": "string", 40 | "index": "not_analyzed" 41 | } 42 | } 43 | } 44 | 45 | 46 | //Testing mapping 47 | GET store/_analyze?field=title&text=i love you 48 | GET store/_analyze?field=tag&text=i love you 49 | 50 | 51 | //Create mapping with Thai analyzer 52 | PUT /store 53 | { 54 | "mappings": { 55 | "book": { 56 | "properties": { 57 | "title": { 58 | "type": "string", 59 | "analyzer": "thai" 60 | }, 61 | "published_date": { 62 | "type": "date" 63 | } 64 | } 65 | } 66 | } 67 | } 68 | 69 | //Test thai 70 | http://localhost:9200/store/_analyze?field=title&text=สวัสดีประเทศไทย&pretty 71 | 72 | //Add default of search and index analyzer 73 | PUT /store 74 | { 75 | "mappings": { 76 | "book": { 77 | "index_analyzer": "standard", 78 | "search_analyzer": "standard", 79 | "properties": { 80 | "title": { 81 | "type": "string", 82 | "analyzer": "thai" 83 | }, 84 | "published_date": { 85 | "type": "date" 86 | } 87 | } 88 | } 89 | } 90 | }s 91 | 92 | 93 | //Testing thai analyzer in browser 94 | http://localhost:9200/store/_analyze?field=title&text=สวัสดีประเทศไทย&pretty 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /book_06.txt: -------------------------------------------------------------------------------- 1 | //Preparing index 2 | DELETE /store 3 | PUT /store 4 | 5 | //Preparing data example 6 | 7 | 8 | //Empty search 9 | GET /store/book/_search 10 | { 11 | } 12 | 13 | GET /store/_search 14 | { 15 | "query": { 16 | "match_all": {} 17 | } 18 | } 19 | 20 | //Not show _source 21 | GET /store/_search?_source=false 22 | { 23 | "query": { 24 | "match_all": {} 25 | } 26 | } 27 | 28 | //Not show _source and show some field[s] 29 | GET /store/_search?_source=false&fields=title,price 30 | { 31 | "query": { 32 | "match_all": {} 33 | } 34 | } 35 | 36 | //Match 37 | GET /store/_search 38 | { 39 | "query": { 40 | "match": { 41 | "title": "elasticsearch" 42 | } 43 | } 44 | } 45 | 46 | //Multiple match 47 | GET /store/book/_search 48 | { 49 | "query": { 50 | "multi_match": { 51 | "query": "nosql", 52 | "fields": [ 53 | "title", 54 | "long_description" 55 | ] 56 | } 57 | } 58 | } 59 | 60 | //Search with a single term 61 | GET /store/book/_search 62 | { 63 | "query": { 64 | "term": { 65 | "title": { 66 | "value": "elasticsearch" 67 | } 68 | } 69 | } 70 | } 71 | 72 | 73 | //Search with multiple term 74 | GET /store/book/_search 75 | { 76 | "query": { 77 | "terms": { 78 | "title": [ 79 | "search", 80 | "the" 81 | ] 82 | } 83 | } 84 | } 85 | 86 | //Multiple clause with boolean 87 | GET /store/_search?_source=false&fields=title,price 88 | { 89 | "query": { 90 | "bool": { 91 | "must": [ 92 | { 93 | "match": { 94 | "title": "elasticsearch" 95 | } 96 | } 97 | ], 98 | "should": [ 99 | { 100 | "match": { 101 | "title": "book" 102 | } 103 | } 104 | ], 105 | "must_not": [ 106 | { 107 | "match": { 108 | "title": "server" 109 | } 110 | } 111 | ] 112 | } 113 | } 114 | } 115 | 116 | //Search with multiple filter 117 | GET /store/book/_search 118 | { 119 | "query": { 120 | "filtered": { 121 | "filter": { 122 | "term": { 123 | "title": "elasticsearch" 124 | } 125 | }, 126 | "query": { 127 | "term": { 128 | "published_date": "2015-01-16" 129 | } 130 | } 131 | } 132 | } 133 | } 134 | 135 | //Query with range 136 | GET /store/book/_search 137 | { 138 | "query": { 139 | "range": { 140 | "price": { 141 | "gte": 10, 142 | "lte": 21 143 | } 144 | } 145 | } 146 | } 147 | 148 | 149 | //Filter with range 150 | GET /store/book/_search 151 | { 152 | "query": { 153 | "filtered": { 154 | "query": { 155 | "match": { 156 | "category": "Online Searching" 157 | } 158 | }, 159 | "filter": { 160 | "and": [ 161 | { 162 | "term": { 163 | "title": "elasticsearch" 164 | } 165 | }, 166 | { 167 | "term": { 168 | "published_date": "2015-01-15" 169 | } 170 | }, 171 | { 172 | "range": { 173 | "price": { 174 | "gte": 10, 175 | "lte": 25 176 | } 177 | } 178 | } 179 | ] 180 | } 181 | } 182 | } 183 | } 184 | 185 | 186 | 187 | //Paging 188 | GET /store/book/_search 189 | { 190 | "from": 0, 191 | "size": 2 192 | } 193 | 194 | 195 | //Sorting 196 | GET /store/book/_search 197 | { 198 | "sort": [ 199 | { 200 | "published_date": { 201 | "order": "desc" 202 | } 203 | } 204 | ] 205 | } 206 | 207 | //Multiple sorting 208 | GET /store/book/_search 209 | { 210 | "sort": [ 211 | { 212 | "published_date": { 213 | "order": "desc" 214 | } 215 | }, 216 | { 217 | "price": { 218 | "order": "asc" 219 | } 220 | } 221 | ] 222 | } 223 | 224 | 225 | GET /store/book/_search 226 | { 227 | "from": 0, 228 | "query": { 229 | "filtered": { 230 | "filter": { 231 | "term": { 232 | "title": "elasticsearch" 233 | } 234 | } 235 | } 236 | }, 237 | "sort": [ 238 | { 239 | "published_date": { 240 | "order": "asc" 241 | } 242 | } 243 | ] 244 | } 245 | 246 | 247 | //Multiple filtered 248 | GET /store/_search 249 | { 250 | "query": { 251 | "filtered": { 252 | "query": { 253 | "term": { 254 | "title": { 255 | "value": "elasticsearch" 256 | } 257 | } 258 | }, 259 | "filter": { 260 | "bool": { 261 | "must": [ 262 | { 263 | "term": { 264 | "category": "Computer & Technology" 265 | } 266 | }, 267 | { 268 | "range": { 269 | "price": { 270 | "gte": 10, 271 | "lte": 25 272 | } 273 | } 274 | } 275 | ] 276 | } 277 | } 278 | } 279 | } 280 | } 281 | 282 | 283 | 284 | 285 | GET /store/_search 286 | { 287 | "query": { 288 | "filtered": { 289 | "query": { 290 | "term": { 291 | "title": { 292 | "value": "elasticsearch" 293 | } 294 | } 295 | }, 296 | "filter": { 297 | "bool": { 298 | "must": [ 299 | { 300 | "term": { 301 | "category": "Computer & Technology" 302 | } 303 | }, 304 | { 305 | "range": { 306 | "published_date": { 307 | "gte": "2015-01-17", 308 | "lte": "now" 309 | } 310 | } 311 | } 312 | ] 313 | } 314 | } 315 | } 316 | } 317 | } 318 | 319 | -------------------------------------------------------------------------------- /book_06_data.txt: -------------------------------------------------------------------------------- 1 | PUT /store/book/1 2 | { 3 | "title" : "Elasticsearch: The Definitive Guide", 4 | "author_name" : ["Clinton Gormley", "Zachary Tong"], 5 | "category" : ["Online Searching", "Computer & Technology"], 6 | "isbn" : "978-1449358549", 7 | "price" : 44.30, 8 | "page" : 724, 9 | "published_date" : "2015-01-15", 10 | "active" : 1, 11 | "short_description" : "A Distributed Real-Time Search and Analytics Engine", 12 | "long_description" : "Learn how to use Elasticsearch, an open source, distributed, RESTful search engine built on top of Apache Lucene. Each chapter in this book tackles a particular facet of Elasticsearch with separate sections for beginners more advanced programmers. If you’re a beginner, advanced techniques are not required reading, but you can revisit them once you have a solid understanding of the basics." 13 | } 14 | 15 | PUT /store/book/2 16 | { 17 | "title" : "Elasticsearch Server: Second Edition", 18 | "author_name" : ["Rafal Kuc", "Marek Rogoziński"], 19 | "category" : ["Online Searching", "Computer & Technology"], 20 | "isbn" : "B00JXLF7AK", 21 | "price" : 20.49, 22 | "page" : 428, 23 | "published_date" : "2015-01-15", 24 | "active" : 1, 25 | "short_description" : "Elasticsearch Server: Second Edition", 26 | "long_description" : "We all want to find the information we are looking for. This demand brings more and more tools that try to make the full-text search topic easier to understand and introduce to applications. ElasticSearch is one of those tools. It is a cluster- and cloud-aware, high-performance, open source search server. It is written in Java and runs as a standalone full-text search server. ElasticSearch uses the Lucene Java search library at its core for full-text indexing and search, and has REST-like HTTP and JSON APIs that make it easy to use from virtually any programming language. ElasticSearch is a fast, very scalable, and easy-to-use search engine that is enjoyable to use and work with." 27 | } 28 | 29 | PUT /store/book/3 30 | { 31 | "title" : "ElasticSearch Cookbook", 32 | "author_name" : ["Alberto Paro"], 33 | "category" : ["Online Searching", "Computer & Technology"], 34 | "isbn" : "B00HK3VPG8", 35 | "price" : 22.44, 36 | "page" : 424, 37 | "published_date" : "2015-01-16", 38 | "active" : 1, 39 | "short_description" : "ElasticSearch Cookbook", 40 | "long_description" : "ElasticSearch is one of the most promising NoSQL technologies available and is built to provide a scalable search solution with built-in support for near real-time search and multi-tenancy." 41 | } 42 | 43 | PUT /store/book/4 44 | { 45 | "title" : "Elasticsearch in Action", 46 | "author_name" : ["Alberto Paro"], 47 | "category" : ["Computer & Technology", "Java Programming"], 48 | "isbn" : "978-1617291623", 49 | "price" : 36.66, 50 | "page" : 400, 51 | "published_date" : "2015-01-17", 52 | "active" : 1, 53 | "short_description" : "Elasticsearch in Action", 54 | "long_description" : "Elasticsearch makes it easy to add efficient and scalable searches to enterprise applications. Busy administrators and developers love this open source real-time search and analytics engine because they can simply install it, make a few tweaks, and go on with their work. Elasticsearch is miles deep, so once it's up and running, it can be used to build nearly any custom search solution imaginable." 55 | } 56 | 57 | PUT /store/book/5 58 | { 59 | "title" : "The Logstash Book", 60 | "author_name" : ["Alberto Paro", "James Turnbull"], 61 | "category" : ["Computer & Technology", "Java Programming"], 62 | "isbn" : "B00B9JQTCO", 63 | "price" : 9.99, 64 | "page" : 262, 65 | "published_date" : "2015-01-17", 66 | "active" : 1, 67 | "short_description" : "The Logstash Book", 68 | "long_description" : "Updated for Logstash 1.4! A new book designed for SysAdmins, Operations staff, Developers and DevOps who are interested in deploying a log management solution using the open source tool Logstash. In this book we will walk you through installing, deploying, managing and extending Logstash. We are going to do that by introducing you to Example.com, where you are going to start a new job as one of its SysAdmins. The first project you will be in charge of is developing its new log management solution. " 69 | } -------------------------------------------------------------------------------- /book_07.txt: -------------------------------------------------------------------------------- 1 | //Aggregation (data + aggregation) with term aggregation 2 | GET /store/book/_search 3 | { 4 | "aggs": { 5 | "all_book_title": { 6 | "terms": { 7 | "field": "category" 8 | } 9 | } 10 | } 11 | } 12 | 13 | 14 | //Deprecated 15 | //Show only aggregation section 16 | GET /store/book/_search?search_type=count 17 | { 18 | "aggs": { 19 | "all_book_title": { 20 | "terms": { 21 | "field": "category" 22 | } 23 | } 24 | } 25 | } 26 | 27 | //New solution 28 | GET /store/book/_search 29 | { 30 | "size": 0, 31 | "aggs": { 32 | "all_book_title": { 33 | "terms": { 34 | "field": "category" 35 | } 36 | } 37 | } 38 | } 39 | 40 | 41 | //Fix problem 42 | DELETE store 43 | 44 | PUT store 45 | 46 | PUT store/_mapping/book 47 | { 48 | "properties" : { 49 | "category" : { 50 | "type" : "string", 51 | "index": "not_analyzed" 52 | } 53 | } 54 | } 55 | 56 | //Stats aggregation 57 | GET /store/book/_search 58 | { 59 | "size": 0, 60 | "aggs": { 61 | "all_book_title": { 62 | "stats": { 63 | "field": "price" 64 | } 65 | } 66 | } 67 | } 68 | 69 | 70 | //Stats aggregation :: price stat 71 | GET /store/book/_search 72 | { 73 | "size": 0, 74 | "aggs": { 75 | "price_stat": { 76 | "stats": { 77 | "field": "price" 78 | } 79 | } 80 | } 81 | } 82 | 83 | GET /store/book/_search 84 | { 85 | "size": 0, 86 | "aggs": { 87 | "price_stat": { 88 | "stats": { 89 | "script": "doc['price'].value" 90 | } 91 | } 92 | } 93 | } 94 | 95 | //Multiple aggregation type 96 | GET /store/book/_search 97 | { 98 | "size": 0, 99 | "aggs": { 100 | "all_book_title": { 101 | "terms": { 102 | "field": "category" 103 | } 104 | }, 105 | "price_stat": { 106 | "stats": { 107 | "field": "price" 108 | } 109 | } 110 | } 111 | } 112 | 113 | //Missing aggregation 114 | GET /store/book/_search 115 | { 116 | "size": 0, 117 | "aggs": { 118 | "missing_field_tags": { 119 | "missing": { 120 | "field": "tags" 121 | } 122 | } 123 | } 124 | } 125 | 126 | 127 | 128 | //Price range 129 | GET /store/book/_search 130 | { 131 | "size": 0, 132 | "aggs": { 133 | "price_range": { 134 | "range": { 135 | "field": "price", 136 | "ranges": [ 137 | { 138 | "from": 0, "to": 10 139 | }, 140 | { 141 | "from": 11, "to": 20 142 | }, 143 | { 144 | "from": 21, "to": 50 145 | } 146 | ] 147 | } 148 | } 149 | } 150 | } 151 | 152 | 153 | //Order data by category 154 | GET /store/book/_search 155 | { 156 | "size": 0, 157 | "aggs": { 158 | "all_book_title": { 159 | "terms": { 160 | "field": "category", 161 | "order": { 162 | "_count": "asc" 163 | } 164 | } 165 | } 166 | } 167 | } 168 | 169 | 170 | //Multiple aggregation (bucket + metric) 171 | GET /store/book/_search 172 | { 173 | "size": 0, 174 | "aggs": { 175 | "all_book_title": { 176 | "terms": { 177 | "field": "category", 178 | "order": { 179 | "_count": "asc" 180 | } 181 | }, 182 | "aggs": { 183 | "price_stat": { 184 | "stats": { 185 | "field" : "price" 186 | } 187 | } 188 | } 189 | } 190 | } 191 | } 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /book_08_suggestion.txt: -------------------------------------------------------------------------------- 1 | 1. Search as you type 2 | 3 | //Create index 4 | PUT /suggestion 5 | { 6 | "mappings": { 7 | "book": { 8 | "properties": { 9 | "title": { 10 | "type": "string" 11 | }, 12 | "title_suggest": { 13 | "type": "completion" 14 | } 15 | } 16 | } 17 | } 18 | } 19 | 20 | //Create documents 21 | PUT /suggestion/book/1 22 | { 23 | "title" : "Elasticsearch book one", 24 | "title_suggest" : "Elasticsearch book one" 25 | } 26 | 27 | PUT /suggestion/book/2 28 | { 29 | "title" : "Elasticsearch book two", 30 | "title_suggest" : "Elasticsearch book two" 31 | } 32 | 33 | PUT /suggestion/book/3 34 | { 35 | "title" : "Elasticsearch book three", 36 | "title_suggest" : "Elasticsearch book three" 37 | } 38 | 39 | //Query suggestion 40 | POST /suggestion/_suggest 41 | { 42 | "title" : { 43 | "text" : "elas", 44 | "completion" : { 45 | "field" : "title_suggest", 46 | "size" : 10 47 | } 48 | } 49 | } 50 | 51 | 52 | //Solution :: multiple inputs 53 | PUT /suggestion/book/1 54 | { 55 | "title": "Elasticsearch book one", 56 | "title_suggest": { 57 | "input": [ 58 | "Elasticsearch book one", 59 | "Book elasticsearch" 60 | ] 61 | } 62 | } 63 | 64 | PUT /suggestion/book/1 65 | { 66 | "title": "Elasticsearch book one", 67 | "title_suggest": { 68 | "input": [ 69 | "Elasticsearch book one", 70 | "Book elasticsearch", 71 | "buy now .... " 72 | ] 73 | } 74 | } 75 | 76 | 77 | //Solution :: single output 78 | PUT /suggestion/book/1 79 | { 80 | "title": "Elasticsearch book one", 81 | "title_suggest": { 82 | "input": [ 83 | "Elasticsearch book one", 84 | "Book elasticsearch", 85 | "buy now .... " 86 | ], 87 | "output": "Elasticsearch book one" 88 | } 89 | } 90 | 91 | 92 | //Solution :: boost score 93 | PUT /suggestion/book/2 94 | { 95 | "title": "Elasticsearch book two", 96 | "title_suggest": { 97 | "input": [ 98 | "Elasticsearch book one", 99 | "discount" 100 | ], 101 | "output": "Elasticsearch book two", 102 | "weight": 10 103 | } 104 | } 105 | 106 | 107 | //Solution :: working with payloads 108 | DELETE /suggestion 109 | 110 | PUT /suggestion 111 | { 112 | "mappings": { 113 | "book": { 114 | "properties": { 115 | "title": { 116 | "type": "string" 117 | }, 118 | "title_suggest": { 119 | "type": "completion", 120 | "payloads": true 121 | } 122 | } 123 | } 124 | } 125 | } 126 | 127 | //Create documents 128 | PUT /suggestion/book/1 129 | { 130 | "title": "Elasticsearch book one", 131 | "title_suggest": { 132 | "input": [ 133 | "Elasticsearch book one" 134 | ], 135 | "payload": { 136 | "product_id": 1 137 | } 138 | } 139 | } 140 | 141 | 142 | PUT /suggestion/book/2 143 | { 144 | "title": "Elasticsearch book two", 145 | "title_suggest": { 146 | "input": [ 147 | "Elasticsearch book two" 148 | ], 149 | "payload": { 150 | "product_id": 2 151 | } 152 | } 153 | } 154 | 155 | PUT /suggestion/book/3 156 | { 157 | "title": "Elasticsearch book three", 158 | "title_suggest": { 159 | "input": [ 160 | "Elasticsearch book three" 161 | ], 162 | "payload": { 163 | "product_id": 3 164 | } 165 | } 166 | } 167 | 168 | 169 | //Query suggestion 170 | POST /suggestion/_suggest 171 | { 172 | "title" : { 173 | "text" : "elas", 174 | "completion" : { 175 | "field" : "title_suggest", 176 | "size" : 10 177 | } 178 | } 179 | } 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /book_09_ngram.txt: -------------------------------------------------------------------------------- 1 | //Create index with N-gram 2 | PUT /demo_ngram 3 | { 4 | "settings": { 5 | "analysis": { 6 | "analyzer": { 7 | "autocomplete": { 8 | "type": "custom", 9 | "tokenizer": "standard", 10 | "filter": [ 11 | "standard", 12 | "lowercase", 13 | "stop", 14 | "kstem", 15 | "ngram" 16 | ] 17 | } 18 | }, 19 | "filter": { 20 | "ngram": { 21 | "type": "ngram", 22 | "min_gram": 2, 23 | "max_gram": 15 24 | } 25 | } 26 | } 27 | } 28 | } 29 | 30 | //Analyze N-gram with autocomplete 31 | GET /demo_ngram/_analyze?analyzer=autocomplete&text=quick 32 | 33 | //Update mapping 34 | PUT /demo_ngram/_mapping/my_type 35 | { 36 | "my_type": { 37 | "properties": { 38 | "name": { 39 | "type": "string", 40 | "analyzer": "autocomplete" 41 | } 42 | } 43 | } 44 | } 45 | 46 | //Create document 47 | POST /demo_ngram/my_type/_bulk 48 | { "index": { "_id": 1}} 49 | { "name": "Elasticsearch book one"} 50 | { "index": { "_id": 2}} 51 | { "name": "Elasticsearch book two" } 52 | { "index": { "_id": 3}} 53 | { "name": "Elasticsearch book three" } 54 | 55 | //Search 56 | GET /demo_ngram/_search 57 | { 58 | "query": { 59 | "term": { 60 | "name": { 61 | "value": "las" 62 | } 63 | } 64 | } 65 | } 66 | 67 | 68 | //Search with highlight 69 | //http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html 70 | 71 | GET /demo_ngram/_search 72 | { 73 | "query": { 74 | "term": { 75 | "name": { 76 | "value": "las" 77 | } 78 | } 79 | }, 80 | "highlight": { 81 | "fields": { 82 | "name": {} 83 | } 84 | } 85 | } 86 | 87 | ===================== Edge N-gram ================== 88 | 89 | 90 | //Create index with Edge N-gram 91 | PUT /demo_edge 92 | { 93 | "settings": { 94 | "number_of_shards": 1, 95 | "analysis": { 96 | "analyzer": { 97 | "autocomplete": { 98 | "type": "custom", 99 | "tokenizer": "standard", 100 | "filter": [ 101 | "lowercase", 102 | "autocomplete_filter" 103 | ] 104 | } 105 | }, 106 | "filter": { 107 | "autocomplete_filter": { 108 | "type": "edge_ngram", 109 | "min_gram": 1, 110 | "max_gram": 20 111 | } 112 | } 113 | } 114 | } 115 | } 116 | 117 | //Analyze Edge N-gram with autocomplete 118 | GET /demo_edge/_analyze?analyzer=autocomplete&text=quick 119 | 120 | 121 | //Update mapping 122 | PUT /demo_edge/_mapping/my_type 123 | { 124 | "my_type": { 125 | "properties": { 126 | "name": { 127 | "type": "string", 128 | "analyzer": "autocomplete" 129 | } 130 | } 131 | } 132 | } 133 | 134 | //Create document 135 | POST /demo_edge/my_type/_bulk 136 | { "index": { "_id": 1}} 137 | { "name": "Elasticsearch book one"} 138 | { "index": { "_id": 2}} 139 | { "name": "Elasticsearch book two" } 140 | { "index": { "_id": 3}} 141 | { "name": "Elasticsearch book three" } 142 | 143 | //Search 144 | GET /demo_edge/_search 145 | { 146 | "query": { 147 | "term": { 148 | "name": { 149 | "value": "boo" 150 | } 151 | } 152 | } 153 | } 154 | 155 | 156 | //Search with highlight 157 | GET /demo_edge/_search 158 | { 159 | "query": { 160 | "term": { 161 | "name": { 162 | "value": "book" 163 | } 164 | } 165 | }, 166 | "highlight": { 167 | "fields": { 168 | "name": {} 169 | } 170 | } 171 | } 172 | 173 | 174 | -------------------------------------------------------------------------------- /book_mapping.yaml: -------------------------------------------------------------------------------- 1 | mappings: 2 | book: 3 | properties: 4 | name: 5 | type: "string" 6 | analyzer: "thai" 7 | price: 8 | type: double 9 | -------------------------------------------------------------------------------- /boost_by_popularity.txt: -------------------------------------------------------------------------------- 1 | //Data 2 | PUT /blogs/post/1 3 | { 4 | "title": "my course", 5 | "content": "In this post we will talk about...", 6 | "like": 6 7 | } 8 | 9 | PUT /blogs/post/2 10 | { 11 | "title": "Good developer course", 12 | "content": "In this post we will talk about...", 13 | "like": 3 14 | } 15 | 16 | PUT /blogs/post/3 17 | { 18 | "title": "Course elasticsearch for developer", 19 | "content": "In this post we will talk about...", 20 | "like": 30 21 | } 22 | 23 | 24 | 25 | //Query by popularity 26 | GET blogs/_search 27 | { 28 | "query": { 29 | "function_score": { 30 | "query": { 31 | "multi_match": { 32 | "query": "course", 33 | "fields": ["title", "content"] 34 | } 35 | }, 36 | "field_value_factor": { 37 | "field": "like" 38 | } 39 | } 40 | } 41 | } 42 | 43 | 44 | //Custom function score 45 | GET blogs/_search 46 | { 47 | "query": { 48 | "function_score": { 49 | "query": { 50 | "multi_match": { 51 | "query": "course", 52 | "fields": ["title", "content"] 53 | } 54 | }, 55 | "field_value_factor": { 56 | "field": "like", 57 | "modifier": "log", 58 | "factor": 1.2 59 | }, 60 | "boost_mode": "sum", 61 | "max_boost": 10 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /boost_index.txt: -------------------------------------------------------------------------------- 1 | GET thai,english/_search 2 | { 3 | "indices_boost" : { 4 | "thai" : 1.5, 5 | "english" : 1.1 6 | } 7 | } 8 | 9 | 10 | GET /docs_2014_*/_search 11 | { 12 | "indices_boost": { 13 | "docs_2014_10": 3, 14 | "docs_2014_09": 2 15 | }, 16 | "query": { 17 | "match": { 18 | "text": "quick brown fox" 19 | } 20 | } 21 | } 22 | 23 | 24 | http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/query-time-boosting.html 25 | 26 | -------------------------------------------------------------------------------- /boost_query.txt: -------------------------------------------------------------------------------- 1 | DELETE store 2 | 3 | POST store/book/ 4 | { 5 | "name" : "lucene book", 6 | "price" : 400 7 | } 8 | POST store/book/ 9 | { 10 | "name" : "elasticsearch book", 11 | "price" : 200 12 | } 13 | POST store/book/ 14 | { 15 | "name" : "book", 16 | "price" : 100 17 | } 18 | 19 | GET store/_search 20 | 21 | //Search by term book 22 | GET store/_search 23 | { 24 | "query": { 25 | "term": { 26 | "name": { 27 | "value": "book" 28 | } 29 | } 30 | } 31 | } 32 | 33 | //Boosting query in boolean query 34 | GET store/_search 35 | { 36 | "query": { 37 | "bool": { 38 | "must": [ 39 | { 40 | "match": { 41 | "name": { 42 | "query": "book" 43 | } 44 | } 45 | } 46 | ], 47 | "should": [ 48 | { 49 | "match": { 50 | "name": { 51 | "query": "lucene", 52 | "boost": 5 53 | } 54 | } 55 | } 56 | ] 57 | } 58 | } 59 | } 60 | 61 | //Boosting query in boolean query 62 | GET store/_search 63 | { 64 | "query": { 65 | "bool": { 66 | "must": [ 67 | { 68 | "match": { 69 | "name" : { 70 | "query": "book" 71 | } 72 | } 73 | } 74 | ], 75 | "should": [ 76 | { 77 | "match": { 78 | "name" : { 79 | "query": "elasticsearch", 80 | "boost" : 5 81 | } 82 | } 83 | }, 84 | { 85 | "match": { 86 | "name" : { 87 | "query": "lucene", 88 | "boost" : 2 89 | } 90 | } 91 | } 92 | ] 93 | } 94 | } 95 | } 96 | 97 | //Positive and negative boosting 98 | GET store/_search 99 | { 100 | "query": { 101 | "boosting": { 102 | "positive": { 103 | "term": { 104 | "name": "book" 105 | } 106 | }, 107 | "negative": { 108 | "term": { 109 | "name": "elasticsearch" 110 | } 111 | }, 112 | "negative_boost": 0.1 113 | } 114 | } 115 | } 116 | 117 | 118 | 119 | http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html 120 | http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/not-quite-not.html 121 | http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html 122 | 123 | -------------------------------------------------------------------------------- /boost_workshop.txt: -------------------------------------------------------------------------------- 1 | //Data 2 | DELETE store 3 | 4 | POST store/book/ 5 | { 6 | "name" : "lucene", 7 | "detail" : "lucene book book book book", 8 | "price" : 400 9 | } 10 | 11 | POST store/book/ 12 | { 13 | "name" : "lucene book", 14 | "detail" : "lucene book book book book", 15 | "price" : 400 16 | } 17 | POST store/book/ 18 | { 19 | "name" : "elasticsearch in action", 20 | "detail" : "elasticsearch book book book book book book", 21 | "price" : 200 22 | } 23 | POST store/book/ 24 | { 25 | "name" : "book", 26 | "detail" : "my personal book", 27 | "price" : 100 28 | } 29 | 30 | //Search by term book 31 | GET store/_search 32 | { 33 | "query": { 34 | "term": { 35 | "_all": { 36 | "value": "book" 37 | } 38 | } 39 | } 40 | } 41 | 42 | 43 | //Workshop 44 | GET store/_search 45 | { 46 | "query": { 47 | "bool": { 48 | "must": [ 49 | { 50 | "match": { 51 | "_all": { 52 | "query": "book" 53 | } 54 | } 55 | } 56 | ], 57 | "should": [ 58 | { 59 | "match": { 60 | "name": { 61 | "query": "book", 62 | "boost": 5 63 | } 64 | } 65 | }, 66 | { 67 | "match": { 68 | "detail": { 69 | "query": "book", 70 | "boost" : 2 71 | } 72 | } 73 | } 74 | ] 75 | } 76 | } 77 | } 78 | 79 | 80 | //Boost search with multi match query 81 | GET store/_search 82 | { 83 | "query": { 84 | "multi_match": { 85 | "query": "book", 86 | "fields": ["name^5", "detail"] 87 | } 88 | } 89 | } 90 | 91 | //Prioritize by field 92 | GET store/_search 93 | { 94 | "query": { 95 | "bool": { 96 | "should": [ 97 | { 98 | "match": { 99 | "name": { 100 | "query": "book", 101 | "boost" : 5 102 | } 103 | } 104 | }, 105 | { 106 | "match": { 107 | "detail": { 108 | "query": "book", 109 | "boost": 1 110 | } 111 | } 112 | } 113 | ] 114 | } 115 | } 116 | } 117 | 118 | 119 | http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_boosting_query_clauses.html 120 | http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html 121 | 122 | -------------------------------------------------------------------------------- /car.txt: -------------------------------------------------------------------------------- 1 | //Data 2 | POST /cars/transactions/_bulk 3 | { "index": {}} 4 | { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" } 5 | { "index": {}} 6 | { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } 7 | { "index": {}} 8 | { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" } 9 | { "index": {}} 10 | { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" } 11 | { "index": {}} 12 | { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" } 13 | { "index": {}} 14 | { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } 15 | { "index": {}} 16 | { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" } 17 | { "index": {}} 18 | { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" } 19 | 20 | // Best color 21 | GET /cars/_search 22 | { 23 | "size": 0, 24 | "aggs": { 25 | "colors": { 26 | "terms": { 27 | "field": "color" 28 | } 29 | } 30 | } 31 | } 32 | 33 | //Average price in each color 34 | GET /cars/_search 35 | { 36 | "size": 0, 37 | "aggs": { 38 | "colors": { 39 | "terms": { 40 | "field": "color" 41 | } 42 | , 43 | "aggs": { 44 | "avg_price": { 45 | "avg": { 46 | "field": "price" 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | 54 | //Make in each color 55 | GET /cars/_search 56 | { 57 | "size": 0, 58 | "aggs": { 59 | "colors": { 60 | "terms": { 61 | "field": "color" 62 | }, 63 | "aggs": { 64 | "avg_price": { 65 | "avg": { 66 | "field": "price" 67 | } 68 | }, 69 | "make": { 70 | "terms": { 71 | "field": "make" 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | //Add min and max price in each make 80 | GET /cars/_search 81 | { 82 | "size": 0, 83 | "aggs": { 84 | "colors": { 85 | "terms": { 86 | "field": "color" 87 | }, 88 | "aggs": { 89 | "avg_price": { 90 | "avg": { 91 | "field": "price" 92 | } 93 | }, 94 | "make": { 95 | "terms": { 96 | "field": "make" 97 | }, 98 | "aggs": { 99 | "min_price": { 100 | "min": { 101 | "field": "price" 102 | } 103 | }, 104 | "max_price" : { 105 | "max": { 106 | "field": "price" 107 | } 108 | } 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } 115 | 116 | 117 | //Show using histogram 118 | GET /cars/_search 119 | { 120 | "size": 0, 121 | "aggs": { 122 | "price": { 123 | "histogram": { 124 | "field": "price", 125 | "interval": 20000 126 | } 127 | } 128 | } 129 | } 130 | 131 | 132 | //Top car in each range 133 | GET /cars/_search 134 | { 135 | "size": 0, 136 | "aggs": { 137 | "price": { 138 | "histogram": { 139 | "field": "price", 140 | "interval": 20000 141 | }, 142 | "aggs": { 143 | "make": { 144 | "terms": { 145 | "field": "make", 146 | "size": 1 147 | } 148 | } 149 | } 150 | } 151 | } 152 | } 153 | 154 | 155 | //Car sold overtime 156 | GET /cars/_search 157 | { 158 | "size": 0, 159 | "aggs": { 160 | "sold": { 161 | "date_histogram": { 162 | "field": "sold", 163 | "interval": "month" 164 | } 165 | } 166 | } 167 | } 168 | 169 | //Format date 170 | GET /cars/_search 171 | { 172 | "size": 0, 173 | "aggs": { 174 | "sold": { 175 | "date_histogram": { 176 | "field": "sold", 177 | "interval": "month", 178 | "format": "yyyy-MM-dd" 179 | } 180 | } 181 | } 182 | } 183 | 184 | //Return empty bucket 185 | GET /cars/_search 186 | { 187 | "size": 0, 188 | "aggs": { 189 | "sold": { 190 | "date_histogram": { 191 | "field": "sold", 192 | "interval": "month", 193 | "format": "yyyy-MM-dd", 194 | "min_doc_count": 0 195 | } 196 | } 197 | } 198 | } 199 | 200 | //Return empty bucket in 12 month 201 | GET /cars/_search 202 | { 203 | "size": 0, 204 | "aggs": { 205 | "sold": { 206 | "date_histogram": { 207 | "field": "sold", 208 | "interval": "month", 209 | "format": "yyyy-MM-dd", 210 | "min_doc_count": 0, 211 | "extended_bounds" : { 212 | "min" : "2014-01-01", 213 | "max" : "2014-12-31" 214 | } 215 | } 216 | } 217 | } 218 | } -------------------------------------------------------------------------------- /category_problem.txt: -------------------------------------------------------------------------------- 1 | //Delete index 2 | DELETE /store 3 | 4 | //Create index 5 | PUT /store 6 | 7 | //Create mapping 8 | PUT /store/_mapping/book 9 | { 10 | "properties" : { 11 | "category" : { 12 | "type" : "string", 13 | "index": "not_analyzed" 14 | } 15 | } 16 | } 17 | 18 | //Create 5 documents 19 | 20 | //Test mapping 21 | GET /store/_analyze?field=category&text=test category 22 | 23 | 24 | //Try to query 25 | GET store/_search?search_type=count 26 | { 27 | "aggs": { 28 | "number_of_book": { 29 | "terms": { 30 | "field": "category" 31 | } 32 | } 33 | } 34 | } 35 | 36 | //Second problem :: search + count by category 37 | GET store/_search 38 | { 39 | "query": { 40 | "term": { 41 | "category": { 42 | "value": "computer" 43 | } 44 | } 45 | }, 46 | "aggs": { 47 | "number_of_book": { 48 | "terms": { 49 | "field": "category" 50 | } 51 | } 52 | } 53 | } 54 | 55 | //Solution :: Update mapping with adding field to category fields 56 | PUT /store/_mapping/book 57 | { 58 | "properties" : { 59 | "category" : { 60 | "type" : "string", 61 | "fields": { 62 | "raw" : { 63 | "type": "string", 64 | "index": "not_analyzed" 65 | } 66 | } 67 | } 68 | } 69 | } 70 | 71 | 72 | //Change query 73 | GET store/_search 74 | { 75 | "query": { 76 | "term": { 77 | "category": { 78 | "value": "computer" 79 | } 80 | } 81 | }, 82 | "aggs": { 83 | "number_of_book": { 84 | "terms": { 85 | "field": "category.raw" 86 | } 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /cluster.txt: -------------------------------------------------------------------------------- 1 | //Cluster health 2 | GET _cluster/health 3 | 4 | //Create index with specified shard and replicate 5 | PUT /blog 6 | { 7 | "settings": { 8 | "number_of_shards": 3, 9 | "number_of_replicas": 1 10 | } 11 | } 12 | 13 | //Add 2 node with one master 14 | $ bin/elasticsearch -Des.node.data=true -Des.node.master=true -Des.node.name=First 15 | $ bin/elasticsearch -Des.node.data=true -Des.node.master=false -Des.node.name=Second 16 | 17 | //Add multiple node with 1 index node and 2 data node 18 | $ bin/elasticsearch -Des.node.data=false -Des.node.master=true -Des.node.name=NoData 19 | $ bin/elasticsearch -Des.node.data=true -Des.node.master=false -Des.node.name=DataOne 20 | $ bin/elasticsearch -Des.node.data=true -Des.node.master=false -Des.node.name=DataTwo 21 | 22 | 23 | //Run at local node in elasticsearcg.yml 24 | node.local: true 25 | 26 | -------------------------------------------------------------------------------- /dynamic_template.txt: -------------------------------------------------------------------------------- 1 | https://gist.github.com/deverton/2970285 2 | https://github.com/logstash-plugins/logstash-output-elasticsearch/blob/master/lib/logstash/outputs/elasticsearch/elasticsearch-template.json 3 | 4 | 5 | DELETE test_dynamic 6 | 7 | PUT test_dynamic 8 | { 9 | "mappings": { 10 | "logs": { 11 | "dynamic_templates": [ 12 | { 13 | "string_fields": { 14 | "mapping": { 15 | "index": "analyzed", 16 | "type": "string", 17 | "fields": { 18 | "raw": { 19 | "index": "not_analyzed", 20 | "type": "string" 21 | } 22 | } 23 | }, 24 | "match_mapping_type": "string", 25 | "match": "*" 26 | } 27 | } 28 | ] 29 | } 30 | } 31 | } 32 | 33 | 34 | PUT test_dynamic 35 | { 36 | "mappings": { 37 | "logs": { 38 | "dynamic_templates": [ 39 | { 40 | "string_fields": { 41 | "mapping": { 42 | "index": "analyzed", 43 | "type": "string", 44 | "fields": { 45 | "raw": { 46 | "index": "not_analyzed", 47 | "type": "string" 48 | } 49 | } 50 | }, 51 | "match_mapping_type": "string", 52 | "match": "*" 53 | } 54 | } 55 | ] 56 | } 57 | } 58 | } 59 | 60 | GET test_dynamic 61 | 62 | 63 | PUT test_dynamic/logs/1 64 | { 65 | "name" : "yyyyyyy", 66 | "category" : "xxxx", 67 | "price" : 100000000000 68 | } 69 | 70 | PUT test_dynamic/logs/2 71 | { 72 | "name" : "somkiat puisungnoen", 73 | "category" : "computer & technology", 74 | "price" : "100" 75 | } 76 | 77 | GET test_dynamic/_search 78 | 79 | 80 | 81 | PUT xxx/post/1 82 | { 83 | "test" : "11111111" 84 | } 85 | 86 | PUT xxx/post/2 87 | { 88 | "test" : 111111111 89 | } 90 | 91 | 92 | 93 | 94 | GET olxlogger-2015.02.13/_mapping 95 | 96 | GET olxlogger-2015.02.13/_search?search_type=count 97 | { 98 | "query": { 99 | "filtered": { 100 | "query": { 101 | "terms": { 102 | "reason_id": [ 103 | "1", 104 | "2", 105 | "3", 106 | "6" 107 | ] 108 | } 109 | }, 110 | "filter": { 111 | "range": { 112 | "@timestamp": { 113 | "gte": "now-6H", 114 | "lte": "now" 115 | } 116 | } 117 | } 118 | } 119 | }, 120 | "aggs": { 121 | "item": { 122 | "terms": { 123 | "field": "item_id", 124 | "size": 10 125 | }, 126 | "aggs": { 127 | "details": { 128 | "terms": { 129 | "field": "comment.raw" 130 | } 131 | } 132 | } 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /geo.txt: -------------------------------------------------------------------------------- 1 | 2 | //Create mapping 3 | PUT /my_map 4 | { 5 | "mappings": { 6 | "city": { 7 | "properties": { 8 | "name": { 9 | "type": "string" 10 | }, 11 | "location": { 12 | "type": "geo_point" 13 | } 14 | } 15 | } 16 | } 17 | } 18 | 19 | //Index 1 :: string format (lat, lon) 20 | PUT /my_map/city/1 21 | { 22 | "name" : "Bangkok", 23 | "location" : "13.45, 100.35" 24 | } 25 | 26 | //Index 2 :: Object with lat, lon 27 | PUT /my_map/city/1 28 | { 29 | "name" : "Bangkok", 30 | "location" : { 31 | "lat" : 13.45, 32 | "lon" : 100.35 33 | } 34 | } 35 | 36 | //Index 3 :: Array with [lon, lat] 37 | PUT /my_map/city/1 38 | { 39 | "name" : "Bangkok", 40 | "location" : [100.35,13.45] 41 | } 42 | 43 | 44 | //Filter data with bulding box 45 | GET /my_map/_search 46 | { 47 | "query": { 48 | "filtered": { 49 | "filter": { 50 | "geo_bounding_box": { 51 | "location": { 52 | "top_left": { 53 | "lat": 13.856636, 54 | "lon": 100.328240 55 | }, 56 | "bottom_right": { 57 | "lat": 13.680571, 58 | "lon": 100.677056 59 | } 60 | } 61 | } 62 | } 63 | } 64 | } 65 | } 66 | 67 | //Optimize geo point 68 | PUT /my_map 69 | { 70 | "mappings": { 71 | "city": { 72 | "properties": { 73 | "name": { 74 | "type": "string" 75 | }, 76 | "location": { 77 | "type": "geo_point", 78 | "lat_lon": true 79 | } 80 | } 81 | } 82 | } 83 | } 84 | 85 | //Filter with indexed 86 | GET /my_map/_search 87 | { 88 | "query": { 89 | "filtered": { 90 | "filter": { 91 | "geo_bounding_box": { 92 | "type": "indexed", 93 | "location": { 94 | "top_left": { 95 | "lat": 13.856636, 96 | "lon": 100.328240 97 | }, 98 | "bottom_right": { 99 | "lat": 13.680571, 100 | "lon": 100.677056 101 | } 102 | } 103 | } 104 | } 105 | } 106 | } 107 | } 108 | 109 | 110 | //Filter geo distance 111 | GET /my_map/_search 112 | { 113 | "query": { 114 | "filtered": { 115 | "filter": { 116 | "geo_distance": { 117 | "distance": 100, 118 | "distance_unit": "km", 119 | "location": { 120 | "lat": 18.796143, 121 | "lon": 98.979263 122 | } 123 | } 124 | } 125 | } 126 | } 127 | } 128 | 129 | //Filter geo distance range 130 | GET /my_map/_search 131 | { 132 | "query": { 133 | "filtered": { 134 | "filter": { 135 | "geo_distance_range": { 136 | "from": 0, 137 | "to": 200, 138 | "distance_unit": "km", 139 | "location": { 140 | "lat": 13.736717, 141 | "lon": 100.523186 142 | } 143 | } 144 | } 145 | } 146 | } 147 | } 148 | 149 | 150 | //Workshop :: Sort by distance from some point 151 | GET /my_map/_search 152 | { 153 | "query": { 154 | "filtered": { 155 | "filter": { 156 | "geo_bounding_box": { 157 | "type": "indexed", 158 | "location": { 159 | "top_left": { 160 | "lat": 13.856636, 161 | "lon": 100.328240 162 | }, 163 | "bottom_right": { 164 | "lat": 13.680571, 165 | "lon": 100.677056 166 | } 167 | } 168 | } 169 | } 170 | } 171 | }, 172 | "sort": [ 173 | { 174 | "_geo_distance": { 175 | "location": { 176 | "lat": 13.736717, 177 | "lon": 100.523186 178 | }, 179 | "order": "asc", 180 | "unit": "km", 181 | "distance_type": "plane" 182 | } 183 | } 184 | ] 185 | } 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /homepro.txt: -------------------------------------------------------------------------------- 1 | PUT homepro 2 | { 3 | "mappings": { 4 | "product": { 5 | "dynamic_templates": [ 6 | { 7 | "string_fields": { 8 | "mapping": { 9 | "index": "not_analyzed", 10 | "type": "string" 11 | }, 12 | "match_mapping_type": "string", 13 | "path_match": "attributes.*" 14 | } 15 | } 16 | ], 17 | "properties": { 18 | "category.thai": { 19 | "type": "string", 20 | "index": "not_analyzed" 21 | }, 22 | "category.english": { 23 | "type": "string", 24 | "index": "not_analyzed" 25 | }, 26 | "brand": { 27 | "type": "string", 28 | "index": "analyzed", 29 | "fields" : { 30 | "raw" : { 31 | "type": "string", 32 | "index": "not_analyzed" 33 | } 34 | } 35 | } 36 | } 37 | } 38 | } 39 | } 40 | 41 | 42 | //Dump data 43 | https://github.com/taskrabbit/elasticsearch-dump 44 | 45 | //Export from ES to File 46 | 47 | $elasticdump \ 48 | --all=true \ 49 | --input=http://localhost:9200/homepro \ 50 | --output=homepro.json 51 | 52 | //Import from File to ES(index = homepro) 53 | 54 | $elasticdump \ 55 | --bulk=true \ 56 | --input=homepro.json \ 57 | --output=http://localhost:9200 58 | 59 | 60 | 61 | 62 | //Query 63 | GET _cat/indices 64 | 65 | DELETE * 66 | 67 | PUT homepro 68 | { 69 | "mappings": { 70 | "product": { 71 | "dynamic_templates": [ 72 | { 73 | "string_fields": { 74 | "mapping": { 75 | "index": "not_analyzed", 76 | "type": "string" 77 | }, 78 | "match_mapping_type": "string", 79 | "path_match": "attributes.*" 80 | } 81 | } 82 | ] 83 | } 84 | } 85 | } 86 | 87 | 88 | GET homepro/ 89 | 90 | GET homepro/_count 91 | 92 | GET homepro/_count 93 | { 94 | "query": { 95 | "filtered": { 96 | "filter": { 97 | "missing": { 98 | "field": "attributes" 99 | } 100 | } 101 | } 102 | } 103 | } 104 | 105 | GET homepro/_search 106 | { 107 | "from": 0, 108 | "size": 10, 109 | "query": { 110 | "term": { 111 | "_all": { 112 | "value": "ตู้เย็น" 113 | } 114 | } 115 | } 116 | } 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /hunspell.txt: -------------------------------------------------------------------------------- 1 | PUT /xxx 2 | { 3 | "settings": { 4 | "analysis": { 5 | "analyzer": { 6 | "th_TH": { 7 | "tokenizer": "thai", 8 | "filter": [ 9 | "lowercase", 10 | "th_TH" 11 | ] 12 | } 13 | }, 14 | "filter": { 15 | "th_TH": { 16 | "type": "hunspell", 17 | "language": "th_TH", 18 | "dedup": true 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | PUT /yyy 26 | { 27 | "settings": { 28 | "analysis": { 29 | "analyzer": { 30 | "en_US": { 31 | "tokenizer": "standard", 32 | "filter": [ 33 | "lowercase", 34 | "en_US" 35 | ] 36 | } 37 | }, 38 | "filter": { 39 | "en_US": { 40 | "type": "hunspell", 41 | "language": "en_US", 42 | "dedup": true 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | 50 | http://localhost:9200/yyy/_analyze?analyzer=en_US&text=jumped&pretty 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /imc_ais/main_slide/SPRINT3R-ELASTICSEARCH-AIS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/imc_ais/main_slide/SPRINT3R-ELASTICSEARCH-AIS.pdf -------------------------------------------------------------------------------- /imc_ais/main_slide/book_data.txt: -------------------------------------------------------------------------------- 1 | PUT /shop/book/1 2 | { 3 | "title" : "Elasticsearch: The Definitive Guide", 4 | "author_name" : ["Clinton Gormley", "Zachary Tong"], 5 | "category" : ["Online Searching", "Computer & Technology"], 6 | "isbn" : "978-1449358549", 7 | "price" : 44.30, 8 | "page" : 724, 9 | "published_date" : "2015-01-15", 10 | "active" : 1, 11 | "short_description" : "A Distributed Real-Time Search and Analytics Engine", 12 | "long_description" : "Learn how to use Elasticsearch, an open source, distributed, RESTful search engine built on top of Apache Lucene. Each chapter in this book tackles a particular facet of Elasticsearch with separate sections for beginners more advanced programmers. If you’re a beginner, advanced techniques are not required reading, but you can revisit them once you have a solid understanding of the basics." 13 | } 14 | 15 | PUT /shop/book/2 16 | { 17 | "title" : "Elasticsearch Server: Second Edition", 18 | "author_name" : ["Rafal Kuc", "Marek Rogoziński"], 19 | "category" : ["Online Searching", "Computer & Technology"], 20 | "isbn" : "B00JXLF7AK", 21 | "price" : 20.49, 22 | "page" : 428, 23 | "published_date" : "2015-01-15", 24 | "active" : 1, 25 | "short_description" : "Elasticsearch Server: Second Edition", 26 | "long_description" : "We all want to find the information we are looking for. This demand brings more and more tools that try to make the full-text search topic easier to understand and introduce to applications. ElasticSearch is one of those tools. It is a cluster- and cloud-aware, high-performance, open source search server. It is written in Java and runs as a standalone full-text search server. ElasticSearch uses the Lucene Java search library at its core for full-text indexing and search, and has REST-like HTTP and JSON APIs that make it easy to use from virtually any programming language. ElasticSearch is a fast, very scalable, and easy-to-use search engine that is enjoyable to use and work with." 27 | } 28 | 29 | PUT /shop/book/3 30 | { 31 | "title" : "ElasticSearch Cookbook", 32 | "author_name" : ["Alberto Paro"], 33 | "category" : ["Online Searching", "Computer & Technology"], 34 | "isbn" : "B00HK3VPG8", 35 | "price" : 22.44, 36 | "page" : 424, 37 | "published_date" : "2015-01-16", 38 | "active" : 1, 39 | "short_description" : "ElasticSearch Cookbook", 40 | "long_description" : "ElasticSearch is one of the most promising NoSQL technologies available and is built to provide a scalable search solution with built-in support for near real-time search and multi-tenancy." 41 | } 42 | 43 | PUT /shop/book/4 44 | { 45 | "title" : "Elasticsearch in Action", 46 | "author_name" : ["Alberto Paro"], 47 | "category" : ["Computer & Technology", "Java Programming"], 48 | "isbn" : "978-1617291623", 49 | "price" : 36.66, 50 | "page" : 400, 51 | "published_date" : "2015-01-17", 52 | "active" : 1, 53 | "short_description" : "Elasticsearch in Action", 54 | "long_description" : "Elasticsearch makes it easy to add efficient and scalable searches to enterprise applications. Busy administrators and developers love this open source real-time search and analytics engine because they can simply install it, make a few tweaks, and go on with their work. Elasticsearch is miles deep, so once it's up and running, it can be used to build nearly any custom search solution imaginable." 55 | } 56 | 57 | PUT /shop/book/5 58 | { 59 | "title" : "The Logstash Book", 60 | "author_name" : ["Alberto Paro", "James Turnbull"], 61 | "category" : ["Computer & Technology", "Java Programming"], 62 | "isbn" : "B00B9JQTCO", 63 | "price" : 9.99, 64 | "page" : 262, 65 | "published_date" : "2015-01-17", 66 | "active" : 1, 67 | "short_description" : "The Logstash Book", 68 | "long_description" : "Updated for Logstash 1.4! A new book designed for SysAdmins, Operations staff, Developers and DevOps who are interested in deploying a log management solution using the open source tool Logstash. In this book we will walk you through installing, deploying, managing and extending Logstash. We are going to do that by introducing you to Example.com, where you are going to start a new job as one of its SysAdmins. The first project you will be in charge of is developing its new log management solution. " 69 | } -------------------------------------------------------------------------------- /imc_ais/main_slide/demo_01.txt: -------------------------------------------------------------------------------- 1 | #### Full text query #### 2 | 3 | GET shop/book/_search 4 | { 5 | "query": { 6 | "match": { 7 | "title": "elasticsearch book" 8 | } 9 | } 10 | } 11 | 12 | 13 | GET shop/book/_search 14 | { 15 | "query": { 16 | "match": { 17 | "title": { 18 | "query": "elasticsearch book", 19 | "operator": "or" 20 | } 21 | } 22 | } 23 | } 24 | 25 | GET shop/book/_search 26 | { 27 | "query": { 28 | "query_string": { 29 | "default_field": "title", 30 | "query": "elasticsearch OR book" 31 | } 32 | } 33 | } 34 | 35 | #### Term level query #### 36 | 37 | GET shop/book/_search 38 | { 39 | "query": { 40 | "term": { 41 | "title": { 42 | "value": "elasticsearch" 43 | } 44 | } 45 | } 46 | } 47 | 48 | 49 | GET shop/book/_search 50 | { 51 | "query": { 52 | "term": { 53 | "title": { 54 | "value": "elasticsearch book" 55 | } 56 | } 57 | } 58 | } 59 | 60 | #### Compound query #### 61 | GET shop/book/_search 62 | { 63 | "query": { 64 | "bool": { 65 | "should": [ 66 | { 67 | "term": { 68 | "title": { 69 | "value": "elasticsearch" 70 | } 71 | } 72 | }, 73 | { 74 | "term": { 75 | "title": { 76 | "value": "book" 77 | } 78 | } 79 | } 80 | ] 81 | } 82 | } 83 | } 84 | 85 | GET shop/book/_search 86 | { 87 | "query": { 88 | "bool": { 89 | "should": [ 90 | { "term": { "title" : "elasticsearch" } }, 91 | { "term": { "title" : "book" } }, 92 | { "term": { "title" : "sales" } } 93 | ], 94 | "minimum_should_match" : 2 95 | } 96 | } 97 | } 98 | 99 | GET shop/book/_search 100 | { 101 | "query": { 102 | "bool": { 103 | "filter": { 104 | "term": { "title" : "book" } 105 | }, 106 | "should": [ 107 | { "term": { "title" : "elasticsearch" } }, 108 | { "term": { "title" : "book" } }, 109 | { "term": { "title" : "sales" } } 110 | ], 111 | "minimum_should_match" : "1" 112 | } 113 | } 114 | } 115 | 116 | #### Aggrgation #### 117 | GET shop/book/_search 118 | { 119 | "aggs": { 120 | "book_by_author": { 121 | "terms": { 122 | "field": "author_name" 123 | } 124 | } 125 | } 126 | } 127 | 128 | 129 | GET shop/book/_search 130 | { 131 | "size": 0, 132 | "aggs": { 133 | "book_by_author": { 134 | "terms": { 135 | "field": "author_name" 136 | } 137 | } 138 | } 139 | } 140 | 141 | 142 | //Solution 143 | PUT shop 144 | { 145 | "mappings": { 146 | "book": { 147 | "properties": { 148 | "author_name": { 149 | "index": "not_analyzed", 150 | "type": "string" 151 | } 152 | } 153 | } 154 | } 155 | } 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /imc_ais/workshop/SPRINT3R-ELASTICSEARCH-AGGREGATION-WORKSHOP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/imc_ais/workshop/SPRINT3R-ELASTICSEARCH-AGGREGATION-WORKSHOP.pdf -------------------------------------------------------------------------------- /imc_ais/workshop/car.txt: -------------------------------------------------------------------------------- 1 | //Data 2 | POST /cars/transactions/_bulk 3 | { "index": {}} 4 | { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" } 5 | { "index": {}} 6 | { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } 7 | { "index": {}} 8 | { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" } 9 | { "index": {}} 10 | { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" } 11 | { "index": {}} 12 | { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" } 13 | { "index": {}} 14 | { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" } 15 | { "index": {}} 16 | { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" } 17 | { "index": {}} 18 | { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" } 19 | 20 | // Best color 21 | GET /cars/_search 22 | { 23 | "size": 0, 24 | "aggs": { 25 | "colors": { 26 | "terms": { 27 | "field": "color" 28 | } 29 | } 30 | } 31 | } 32 | 33 | //Average price in each color 34 | GET /cars/_search 35 | { 36 | "size": 0, 37 | "aggs": { 38 | "colors": { 39 | "terms": { 40 | "field": "color" 41 | } 42 | , 43 | "aggs": { 44 | "avg_price": { 45 | "avg": { 46 | "field": "price" 47 | } 48 | } 49 | } 50 | } 51 | } 52 | } 53 | 54 | //Make in each color 55 | GET /cars/_search 56 | { 57 | "size": 0, 58 | "aggs": { 59 | "colors": { 60 | "terms": { 61 | "field": "color" 62 | }, 63 | "aggs": { 64 | "avg_price": { 65 | "avg": { 66 | "field": "price" 67 | } 68 | }, 69 | "make": { 70 | "terms": { 71 | "field": "make" 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | //Add min and max price in each make 80 | GET /cars/_search 81 | { 82 | "size": 0, 83 | "aggs": { 84 | "colors": { 85 | "terms": { 86 | "field": "color" 87 | }, 88 | "aggs": { 89 | "avg_price": { 90 | "avg": { 91 | "field": "price" 92 | } 93 | }, 94 | "make": { 95 | "terms": { 96 | "field": "make" 97 | }, 98 | "aggs": { 99 | "min_price": { 100 | "min": { 101 | "field": "price" 102 | } 103 | }, 104 | "max_price" : { 105 | "max": { 106 | "field": "price" 107 | } 108 | } 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } 115 | 116 | 117 | //Show using histogram 118 | GET /cars/_search 119 | { 120 | "size": 0, 121 | "aggs": { 122 | "price": { 123 | "histogram": { 124 | "field": "price", 125 | "interval": 20000 126 | } 127 | } 128 | } 129 | } 130 | 131 | 132 | //Top car in each range 133 | GET /cars/_search 134 | { 135 | "size": 0, 136 | "aggs": { 137 | "price": { 138 | "histogram": { 139 | "field": "price", 140 | "interval": 20000 141 | }, 142 | "aggs": { 143 | "make": { 144 | "terms": { 145 | "field": "make", 146 | "size": 1 147 | } 148 | } 149 | } 150 | } 151 | } 152 | } 153 | 154 | 155 | //Car sold overtime 156 | GET /cars/_search 157 | { 158 | "size": 0, 159 | "aggs": { 160 | "sold": { 161 | "date_histogram": { 162 | "field": "sold", 163 | "interval": "month" 164 | } 165 | } 166 | } 167 | } 168 | 169 | //Format date 170 | GET /cars/_search 171 | { 172 | "size": 0, 173 | "aggs": { 174 | "sold": { 175 | "date_histogram": { 176 | "field": "sold", 177 | "interval": "month", 178 | "format": "yyyy-MM-dd" 179 | } 180 | } 181 | } 182 | } 183 | 184 | //Return empty bucket 185 | GET /cars/_search 186 | { 187 | "size": 0, 188 | "aggs": { 189 | "sold": { 190 | "date_histogram": { 191 | "field": "sold", 192 | "interval": "month", 193 | "format": "yyyy-MM-dd", 194 | "min_doc_count": 0 195 | } 196 | } 197 | } 198 | } 199 | 200 | //Return empty bucket in 12 month 201 | GET /cars/_search 202 | { 203 | "size": 0, 204 | "aggs": { 205 | "sold": { 206 | "date_histogram": { 207 | "field": "sold", 208 | "interval": "month", 209 | "format": "yyyy-MM-dd", 210 | "min_doc_count": 0, 211 | "extended_bounds" : { 212 | "min" : "2014-01-01", 213 | "max" : "2014-12-31" 214 | } 215 | } 216 | } 217 | } 218 | } -------------------------------------------------------------------------------- /kaidee/02_cluster.txt: -------------------------------------------------------------------------------- 1 | //Head plugin 2 | bin/plugin -install mobz/elasticsearch-head 3 | 4 | //Cluster health 5 | GET _cluster/health 6 | 7 | 8 | //Single node ( master and data ) 9 | PUT /blog 10 | { 11 | "settings": { 12 | "number_of_shards": 3, 13 | "number_of_replicas": 0 14 | } 15 | } 16 | 17 | //Two node with distributed data 18 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/kaidee/software/node01.yml 19 | 20 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/kaidee/software/node02.yml 21 | 22 | //Two node with replica 23 | PUT /blog2 24 | { 25 | "settings": { 26 | "number_of_shards": 4, 27 | "number_of_replicas": 1 28 | } 29 | } 30 | 31 | //Node with single responsibility 32 | Node 01 => Master + Index 33 | Node 02 => Data 34 | Node 03 => Query 35 | 36 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/demo/es_for_developer/kaidee/single_responsibility/node01.yml 37 | 38 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/demo/es_for_developer/kaidee/single_responsibility/node02.yml 39 | 40 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/demo/es_for_developer/kaidee/single_responsibility/node03.yml 41 | 42 | //Create index with replica = 1 43 | PUT /blog3 44 | { 45 | "settings": { 46 | "number_of_shards": 4, 47 | "number_of_replicas": 1 48 | } 49 | } 50 | 51 | //Add node 04 = data only 52 | $ bin/elasticsearch -Des.config=/Users/somkiat/data/slide/Elasticsearch/demo/es_for_developer/kaidee/single_responsibility/node04.yml 53 | 54 | -------------------------------------------------------------------------------- /kaidee/backup_recovery.txt: -------------------------------------------------------------------------------- 1 | //1. Create repository 2 | PUT _snapshot/backup_kaidee 3 | { 4 | "type": "fs", 5 | "settings": { 6 | "location": "/Users/somkiat/data/slide/Elasticsearch/kaidee/backup/xxx", 7 | "compress": true 8 | } 9 | } 10 | 11 | //List of snapshot 12 | GET _snapshot 13 | 14 | GET _snapshot/backup_kaidee 15 | 16 | DELETE _snapshot/backup_kaidee 17 | 18 | //2. Create snapshot 19 | PUT _snapshot/backup_kaidee/snapshot_01 20 | { 21 | "indices": "kaidee", 22 | "ignore_unavailable": "true", 23 | "include_global_state": false 24 | } 25 | 26 | GET _snapshot/backup_kaidee/snapshot_01 27 | 28 | DELETE _snapshot/backup_kaidee/snapshot_01 29 | 30 | 31 | //3. Restore 32 | POST _snapshot/backup_kaidee/snapshot_01/_restore 33 | -------------------------------------------------------------------------------- /kaidee/master_election.txt: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | 3 | discovery.zen.ping.multicast.enabled: false 4 | discovery.zen.ping.unicast.hosts: ["node02:9201", "node04:9203", "node05:9204"] 5 | 6 | discovery.zen.minimum_master_nodes: 2 7 | 8 | #Default values 9 | discovery.zen.ping_timeout: 3s 10 | discovery.zen.join_timeout: 20 -------------------------------------------------------------------------------- /kaidee/replica/node01.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node01 3 | node.master: true 4 | node.data: true 5 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data01 6 | 7 | 8 | -------------------------------------------------------------------------------- /kaidee/replica/node02.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node02 3 | node.master: true 4 | node.data: true 5 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data02 6 | 7 | 8 | -------------------------------------------------------------------------------- /kaidee/s3_backup.txt: -------------------------------------------------------------------------------- 1 | //Config IAM 2 | cloud.aws.access_key: 3 | cloud.aws.secret_key: 4 | 5 | //1. Create repository 6 | PUT _snapshot/backup_s3 7 | { 8 | "type": "s3", 9 | "settings": { 10 | "bucket": "somkiatbackup", 11 | "region": "ap-southeast" 12 | } 13 | } 14 | 15 | 16 | //2. Create snapshot 17 | PUT _snapshot/backup_s3/snapshot_01 18 | { 19 | "indices": "kaidee", 20 | "ignore_unavailable": "true", 21 | "include_global_state": false 22 | } 23 | 24 | //3. Restore 25 | POST _snapshot/backup_s3/snapshot_01/_restore 26 | -------------------------------------------------------------------------------- /kaidee/scripting.txt: -------------------------------------------------------------------------------- 1 | GET blog/_search 2 | { 3 | "script_fields": { 4 | "my_field": { 5 | "script": "1 + my_var", 6 | "params": { 7 | "my_var": 2 8 | } 9 | } 10 | } 11 | } 12 | 13 | 14 | GET blog/_search 15 | { 16 | "script_fields": { 17 | "my_field": { 18 | "script_file": "xxx", 19 | "params": { 20 | "my_var": 3 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /kaidee/single_responsibility/node01.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node01 3 | node.master: true 4 | node.data: false 5 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data01 6 | 7 | 8 | -------------------------------------------------------------------------------- /kaidee/single_responsibility/node02.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node02 3 | node.master: false 4 | node.data: true 5 | http.enabled: false 6 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data02 7 | 8 | 9 | -------------------------------------------------------------------------------- /kaidee/single_responsibility/node03.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node03 3 | node.master: false 4 | node.data: false 5 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data03 6 | 7 | -------------------------------------------------------------------------------- /kaidee/single_responsibility/node04.yml: -------------------------------------------------------------------------------- 1 | cluster.name: kaidee 2 | node.name: node04 3 | node.master: false 4 | node.data: true 5 | http.enabled: false 6 | path.data: /Users/somkiat/data/slide/Elasticsearch/kaidee/software/data04 7 | 8 | -------------------------------------------------------------------------------- /kaidee/slide/SPRINT3R-01-NEW-ELASTICSEARCH-KIBANA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/kaidee/slide/SPRINT3R-01-NEW-ELASTICSEARCH-KIBANA.pdf -------------------------------------------------------------------------------- /kaidee/slide/SPRINT3R-02-CONFIG-CLUSTER-FARM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/kaidee/slide/SPRINT3R-02-CONFIG-CLUSTER-FARM.pdf -------------------------------------------------------------------------------- /kaidee/slide/SPRINT3R-03-SCRIPTING.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/kaidee/slide/SPRINT3R-03-SCRIPTING.pdf -------------------------------------------------------------------------------- /kaidee/slide/SPRINT3R-04-DYNAMIC-MAPPING-TEMPLATE.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/kaidee/slide/SPRINT3R-04-DYNAMIC-MAPPING-TEMPLATE.pdf -------------------------------------------------------------------------------- /kaidee/slide/SPRINT3R-05-BACKUP-RECOVERY.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/github-sprint3r/elasticsearch_workshop/c1fd1e9aed7946334cf2114e3a28c4b595d26697/kaidee/slide/SPRINT3R-05-BACKUP-RECOVERY.pdf -------------------------------------------------------------------------------- /kaidee/stop_node.txt: -------------------------------------------------------------------------------- 1 | GET _cluster/settings 2 | 3 | 4 | //Disable rebalance 5 | PUT _cluster/settings 6 | { 7 | "transient": { 8 | "cluster.routing.allocation.enable": "none" 9 | } 10 | } 11 | 12 | //Shutdown node 13 | 14 | POST _cluster/nodes/_local/_shutdown 15 | 16 | POST _cluster/nodes/_master/_shutdown 17 | 18 | POST _shutdown 19 | 20 | //Enable rebalance 21 | PUT _cluster/settings 22 | { 23 | "transient": { 24 | "cluster.routing.allocation.enable": "all" 25 | } 26 | } 27 | 28 | 29 | 30 | // Rerouting 31 | POST _cluster/reroute 32 | { 33 | "commands": [ 34 | { 35 | "allocate": { 36 | "index": "logstash-2015.04.03", 37 | "shard": 2, 38 | "node": "ek01", 39 | "allow_primary": true 40 | } 41 | } 42 | ] 43 | } -------------------------------------------------------------------------------- /kaidee/template.txt: -------------------------------------------------------------------------------- 1 | //Index data 2 | PUT kaidee/product/1 3 | { 4 | "name": "name 01", 5 | "extra_field": "extra value", 6 | "view": 10 7 | } 8 | 9 | //Create dynamic template 10 | PUT kaidee 11 | { 12 | "mappings": { 13 | "product": { 14 | "dynamic_templates": [ 15 | { 16 | "string_fields": { 17 | "mapping": { 18 | "index": "analyzed", 19 | "type": "string", 20 | "fields": { 21 | "raw": { 22 | "index": "not_analyzed", 23 | "type": "string" 24 | } 25 | } 26 | }, 27 | "match_mapping_type": "string", 28 | "match": "*" 29 | } 30 | } 31 | ], 32 | "properties": { 33 | "extra_field": { 34 | "type": "string", 35 | "index": "not_analyzed" 36 | }, 37 | "view": { 38 | "type": "integer" 39 | } 40 | } 41 | } 42 | } 43 | } 44 | 45 | //Index data with error 46 | PUT kaidee/product/1 47 | { 48 | "name": "name 01", 49 | "extra_field": "extra value", 50 | "view": "ggg" 51 | } 52 | 53 | //Index data 54 | PUT kaidee/product/1 55 | { 56 | "name": "name 01", 57 | "extra_field": "extra value", 58 | "view": 10 59 | } 60 | 61 | //Create template with _template API 62 | GET _template 63 | GET _template/