Day: August 1, 2020

  • [กันลืม] Elasticsearch API พื้นฐาน

    INDEX

    วิธีดูว่ามี index อะไรบ้าง

    GET /_cat

    Response

    =^.^=
    /_cat/allocation
    /_cat/shards
    /_cat/shards/{index}
    /_cat/master
    /_cat/nodes
    /_cat/tasks
    /_cat/indices
    /_cat/indices/{index}
    /_cat/segments
    /_cat/segments/{index}
    /_cat/count
    /_cat/count/{index}
    

    เมื่อทราบว่ามี index อะไรบ้าง ต้องการดูรายละเอียด ใส่ query string parameter (qrs) “v”

    GET /_cat/indices?v

    Response

    health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
    yellow open   logstash-2020.06.20      JeGeb67mSNKN3gCgC9AWCQ   1   1    4573283            0    808.7mb        808.7mb
    yellow open   logstash-2020.05.30      Bg8-hUUAS0m4VbGs0-1lIw   1   1    4124813            0    721.9mb        721.9mb
    yellow open   logstash-2020.05.31      qJX9VbaySG2ssBXMTwmapA   1   1    4333363            0    772.6mb        772.6mb
    yellow open   logstash-2020.06.21      9on104uKQnGllS5QZNyUKg   1   1    4712048            0    869.2mb        869.2mb
    yellow open   logstash-2020.06.22      lVfX4tHjSUeMF640wmIzLw   1   1    8441356            0      1.6gb          1.6gb
    yellow open   logstash-2020.06.01      P_9wbTd7Qo6YNme-NRvFLQ   1   1    7360129            0      1.3gb          1.3gb
    yellow open   logstash-2020.06.23      xvQRTdNHTt2wKfiSlvR87A   1   1    7631987            0      1.4gb          1.4gb
    yellow open   logstash-2020.06.02      H-kPw4FXQ-W1AphBfjtcMw   1   1    7344418            0      1.3gb          1.3gb
    

    ต้องการทราบว่า แต่ละ Fields มีความหมายอย่างไร ใช้ qrs ‘help’

    GET /_cat/indices?help

    Response

    health                           | h                              | current health status                                                                                            
    status                           | s                              | open/close status                                                                                                
    index                            | i,idx                          | index name                                                                                                       
    uuid                             | id,uuid                        | index uuid                                                                                                       
    pri                              | p,shards.primary,shardsPrimary | number of primary shards                                                                                         
    rep                              | r,shards.replica,shardsReplica | number of replica shards                                                                                         
    docs.count                       | dc,docsCount                   | available docs                                                                                                   
    docs.deleted                     | dd,docsDeleted                 | deleted docs                                                                                                     
    creation.date                    | cd                             | index creation date (millisecond value)                                                                          
    creation.date.string             | cds                            | index creation date (as string)                                                                                  
    store.size                       | ss,storeSize                   | store size of primaries & replicas 
    

    ต้องการแสดงเฉพาะบาง Fields ใช้ qrs ‘h=’

    GET /_cat/indices?h=idx,dc,ss&v

    Response

    idx                           dc      ss
    logstash-2020.06.20      4573283 808.7mb
    logstash-2020.05.30      4124813 721.9mb
    logstash-2020.05.31      4333363 772.6mb
    logstash-2020.06.21      4712048 869.2mb
    logstash-2020.06.22      8441356   1.6gb
    logstash-2020.06.23      7631987   1.4gb
    logstash-2020.06.01      7360129   1.3gb
    logstash-2020.06.02      7344418   1.3gb
    logstash-2020.06.24      7300718   1.4gb
    

    ต้องการดูขนาดจัดเก็บ ใช้ qrs ‘bytes=’

    GET /_cat/indices?h=idx,dc,ss&bytes=b&v

    Response

    idx                           dc         ss
    logstash-2020.05.30      4124813  756971768
    logstash-2020.06.20      4573283  848085505
    logstash-2020.05.31      4333363  810175019
    logstash-2020.06.21      4712048  911450929
    logstash-2020.06.22      8441356 1736003983
    logstash-2020.06.01      7360129 1455314526
    logstash-2020.06.23      7631987 1559554324
    logstash-2020.06.24      7300718 1506134380
    logstash-2020.06.02      7344418 1484297643
    logstash-2020.06.25      8409242 1747862513
    logstash-2020.06.03      4424701  860877705
    

    ต้องการเรียงลำดับ ใช้ qrs ‘s=’ และ สามารถกำกับ ‘:desc’, ‘:asc’

    GET /_cat/indices?h=idx,dc,ss&bytes=b&s=ss:desc&v

    ลบ INDEX

    DELETE /kx01

    DOCUMENTS

    Document เป็น JSON ที่มีรายละเอียดเกี่ยวกับการสร้างขึ้นมา เช่น _id, _version และ _source ซึ่ง source หรือ (stored fields)

    create / update if exist

    POST /kx01/_doc/1
    {
      "name": "kanakorn",
      "HN": "1746436"
    }
    

    โดยใน Index เดียวกับ เก็บ Document คนละ Schema กันก็ได้

    POST /kx01/_doc/2
    {
      "HR": "100",
      "RR": "88",
      "age": 10
    }
    

    Check if exist

    HEAD /kx01/_doc/1/

    Response

    200 - OK

    get a source (stored fields)

    GET /kx01/_doc/1/

    Response

    {
      "_index" : "kx01",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "kanakorn",
        "HN" : "1234567"
      }
    }

    get only document value

    GET /kx01/_source/1/

    Response

    {
      "name" : "kanakorn",
      "HN" : "1234567"
    }

    อื่น ๆ

    • Source filtering
      • source_include
      • source_exclude