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 | ########## 인덱스 생성 # 키바나에서는 굳이 pretty 안 붙여도 예쁘게(?) 표시됨, 단 콘솔에서는 pretty붙여야 예쁘게 표현됨. PUT /product?pretty PUT /product ########## 색인에 문서 추가하기 # id를 명시하지 않을 경우 HMTpQmYBnTV7bbirucO0 식으로"_id"가 자동으로 생성됨 # 따라서 일련번호등 별도의 _id를 부여해서 생성하는 게 관리하기 편함. POST /product/default { "name": "Processing Events with Logstash", "instructor": { "firstName": "Bo", "lastName": "Andersen" } } ## 생성한 문서를 보려면 GET /product/default/id ## id를 지정하여 넣는 경우 PUT /product/default/1 { "name": "Complete Guide to Elasticsearch", "instructor": { "firstName": "Bo", "lastName": "Andersen" } } ## 문서를 추가할때 ES는 메타필드를 자동으로 추가한다. _ 밑줄이 붙은 필드가 메타필드이다. 문서자체는 _source에서 찾을 수 있다. ## 메타필드는 일반적으로 문서를 찾을때 사용된다. ## 문서 업데이트- 기존 문서의 ID를 삭제하고 새로운 문서를 넣는다. ## 따라서 문서 추가할때와 쿼리는 동일하다. PUT /product/default/1 { "name": "Complete Guide to Elasticsearch", "price": 195, "instructor": { "firstName": "Bo", "lastName": "Andersen" } } ## 문서 갯수 보기 _count API GET product/_count ## 재인덱싱, 별도의 인덱스 생성 _reindex API POST _reindex { "source": { "index": "product" }, "dest": { "index": "product_re" } } # 자기 스스로 재인덱싱 _update_by_query API POST product/_update_by_query ######### Updating documents ## Updating `price` field and adding `tags` field POST /product/default/1/_update { "doc": { "price": 95, "tags": [ "Elasticsearch" ] } } # 가격을 95로 바꾸고, tags 필드를 추가 ######### Scripted updates ## Updating a document with a script POST /product/default/1/_update { "script": "ctx._source.price += 10" } ## Retrieving the updated document GET /product/default/1 ######### Upserts #업서트는 문서가 존재하지 않으면 upsert안의 내용이 추가가 되고 만약 문서가 존재하면 script 의 내용이 적용됨. ## Deleting the existing document DELETE /product/default/1 ## Inserting or updating a document (upsert) 아래 쿼리를 실행하면 처음엔 price = 100, 두번째 실행하면 price = 105 POST /product/default/1/_update { "script" : "ctx._source.price += 5", "upsert" : { "price" : 100 } } ######### 문서 삭제는 매우 쉽다. DELETE /인덱스/타입/문서id ## Deleting document by ID DELETE /product/default/1 ## Adding test documents POST /product/default { "name": "Processing Events with Logstash", "category": "course" } POST /product/default { "name": "The Art of Scalability", "category": "book" } ## _delete_by_query 를 사용하면 여러개의 문서 삭제 가능 # 검색 GET /product/default/_search # match 문서 검색 GET /product/default/_search { "query": { "match": { "category": "book" } } } # _delete_by_query에 의한 삭제 POST /product/_delete_by_query { "query": { "match": { "category": "book" } } } ######### 인덱스 삭제 DELETE /product ######### Batch processing (일괄처리는 _bulk API 사용) # _bulk API는 추가에만 사용되는 것이 아니라 삭제, 업데이트에도 사용됨 ## Adding documents # 먼저 index에 _id 를 지정하고 다음줄에 추가할 문서를 넣는다 POST /product/default/_bulk { "index": { "_id": "100" } } { "price": 100 } { "index": { "_id": "101" } } { "price": 101 } ## Updating and deleting documents # 첫번째줄에 update 넣고 _id 지정 # 두번째줄 doc 를 넣고 바꿀 부분 지정 # 삭제는 delete 넣고 _id만 지정하면 된다. POST /product/default/_bulk { "update": { "_id": "100" } } { "doc": { "price": 1000 } } { "delete": { "_id": "101" } } ######### json파일 한꺼번에 입력하기 ## Navigating to bulk file directory # cd /path/to/data/file/directory ## Importing test data with cURL # 아래 형식 그대로 Content-Type 지정하고 POST method 파일을 넘기면 됨. --data-binary "@파일명" # curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/product/default/_bulk?pretty' --data-binary "@products-bulk.json" ######### 시스템 상태 확인 _cat # 머리말을 표시하려면 v 옵션 사용 # 노드가 하나일 경우 복사본이 없으므로 yellow로 표시 됨 # green 모든 것이 좋으며 클러스터가 완벽하게 기능한다는 것을 의미합니다. # yellow 완벽하게 작동하지만 데이터의 복제본이 없으므로 데이터 손실의 위험이 있습니다. # red 상태는 공포를 의미합니다. 클러스터의 데이터 중 일부가 사용할 수 없거나 하나 이상의 노드가 다운된 경우, 데이터의 일부를 잃어버고 부분적으로 기능 할 경우 ## Checking the cluster health GET /_cat/health?v ## Listing the cluster's nodes GET /_cat/nodes?v ## Listing the cluster's indices GET /_cat/indices?v ## Checking the shard allocation GET /_cat/allocation?v ## Checking which nodes contain which shards GET /_cat/shards?v ######################################### ######################################### ################ Mapping ################ ######################################### # 데이터베이스의 스키마와 같이 색인의 필드와 속성을 정의하는 것. # 인덱스의 매핑 확인 GET car/_mapping # 필드의 추가는 가능하지만 변경 또는 삭제는 불가능, 잘못 설계시 재인덱싱 # 데이터는 저장하지 않고 토큰만 저장하고자 하는 경우 # 1. 전체를 저장하지 않으려면 _source 필드를 enabled : false로 설정 # 색인시 원본데이터는 저장되지 않고 색인된 토큰들만 저장되므로 통계자료 집계와 같은 목적으로는 사용할 수 있으나 컨텐츠 서비스는 불가능 PUT car3 { "mappings": { "typenews": { "_source" : {"enabled" : false}, "properties": { "datetime": { "type": "date" }, "link": { "type": "keyword" }, "media": { "type": "keyword" }, "summary": { "type": "text", "analyzer": "nori", "fielddata": true }, "timestamp": { "type": "long" }, "title": { "type": "text", "analyzer": "nori", "fielddata": true } } } } } # 검색하면 내장필드만 표현될 뿐 데이터는 없다 GET car3/_search { "query": { "match": { "title": "BMW" } } } # 2. 부분적으로 데이터를 저장하지 않는 경우 exclude 필드명 지정, p* (p로 시작되는 모든 필드) 지정 가능 PUT car4 { "mappings": { "typenews": { "_source": { "excludes": ["summary"] }, "properties": { "datetime": { "type": "date" }, "link": { "type": "keyword" }, "media": { "type": "keyword" }, "summary": { "type": "text", "analyzer": "analyzer_none", "fielddata": true }, "timestamp": { "type": "long" }, "title": { "type": "text", "analyzer": "analyzer_none", "fielddata": true } } } }, "settings": { "index": { "analysis": { "tokenizer": { "nori_user_dict_none": { "type": "nori_tokenizer", "decompound_mode": "none", "user_dictionary": "userdic_sample.txt" } }, "filter": { "my_filter": { "type": "nori_part_of_speech", "stoptags": [ "E", "IC", "J", "MAG", "SP", "SSC", "SSO", "SC", "SE", "XPN", "XSA", "XSN", "XSV", "UNA", "NA", "VSV" ] } }, "analyzer": { "analyzer_none": { "type": "custom", "tokenizer": "nori_user_dict_none", "filter": ["my_filter", "lowercase", "nori_readingform"] } } } } } } PUT car2 { "mappings": { "typenews": { "_source": { "excludes": ["summary", "title"] }, "properties": { "datetime": { "type": "date" }, "link": { "type": "keyword" }, "media": { "type": "keyword" }, "summary": { "type": "text", "analyzer": "nori", "fielddata": true }, "timestamp": { "type": "long" }, "title": { "type": "text", "analyzer": "nori", "fielddata": true } } } } } # 반대로 특정필드만 저장하고자 하는 경우 include PUT car4 { "mappings": { "typenews": { "_source": { "includes": ["title", "media", "link", "timestamp"] }, "properties": { "datetime": { "type": "date" }, "link": { "type": "keyword" }, "media": { "type": "keyword" }, "summary": { "type": "text", "analyzer": "nori", "fielddata": true }, "timestamp": { "type": "long" }, "title": { "type": "text", "analyzer": "nori", "fielddata": true } } } } } ############################## ######################## type ########## 기본타입 # 문자형: text, keyword # 숫자형 # 정수형: byte, short, integer, long # 소수형: float, double # 날짜형: date # boolean: true 나 false를 저장하기 위해 사용 # text: analyze 되어 각각 분리된 토큰으로 인덱싱 되므로 검색에 사용 PUT my_index { "mappings": { "my_type": { "properties": { "my_text_field": { "type": "text" } } } } } # keyword: 메일주소, 링크, 매체명, 우편번호, 상태코드, 태그 등과 같이 analyze 없이 인덱싱된다. # "Hello World" -> Hello 나 World 만으로 검색이 안됨 # 주로 집계에 사용 PUT my_index { "mappings": { "my_type": { "properties": { "my_keyword_field": { "type": "keyword" } } } } } # 검색과 집계에 함께 사용하고자 한다면 PUT my_index { "mappings": { "my_type": { "properties": { "city": { "type": "text", "fields": { "keyword": { "type": "keyword" } } } } } } } # 실제 샘플 # number_of_shards: default 5 # number_of_replicas: default 1 PUT car { "mappings": { "typenews": { "_source": { "excludes": ["summary"] }, "properties": { "datetime": { "type": "date" }, "link": { "type": "keyword" }, "media": { "type": "keyword" }, "summary": { "type": "text", "analyzer": "analyzer_none", "fielddata": true }, "timestamp": { "type": "long" }, "title": { "type": "text", "analyzer": "analyzer_none", "fielddata": true } } } }, "settings": { "number_of_shards" : 1, "number_of_replicas" : 0, "index": { "analysis": { "tokenizer": { "nori_user_dict_none": { "type": "nori_tokenizer", "decompound_mode": "none", "user_dictionary": "userdic_sample.txt" } }, "filter": { "my_filter": { "type": "nori_part_of_speech", "stoptags": [ "E", "IC", "J", "MAG", "MAJ", "MM", "SP", "SSC", "SSO", "SC", "SE", "XPN", "XSA", "XSN", "XSV", "UNA", "NA", "VSV" ] }, "my_stop": { "type": "stop", "stopwords_path": "stopwords.txt" }, "my_synonym" : { "type" : "synonym", "synonyms_path" : "synonyms.txt" } }, "analyzer": { "analyzer_none": { "type": "custom", "tokenizer": "nori_user_dict_none", "filter": ["my_filter", "my_stop", "my_synonym", "lowercase", "nori_readingform"] } } } } } } # index open close POST /my_index/_close POST /my_index/_open | cs |
'Elasticsearch6' 카테고리의 다른 글
elasticsearch 6.* 기본기-4 한국어형태소분석기-nori (0) | 2018.10.26 |
---|---|
elasticsearch 6.* 기본기-3 Aggregations (0) | 2018.10.25 |
elasticsearch 6.* 기본기-1 Search (0) | 2018.10.19 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 4 (search) (0) | 2018.10.17 |
elasticsearch 6.* elasticsearch-php 를 이용한 검색엔진 개발 - 3 (bulk insert) (0) | 2018.10.17 |